Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Function scope, indentation and if-else create very confusing code #2734

Closed
aleator opened this issue Feb 27, 2013 · 9 comments
Closed

Function scope, indentation and if-else create very confusing code #2734

aleator opened this issue Feb 27, 2013 · 9 comments

Comments

@aleator
Copy link

aleator commented Feb 27, 2013

Consider the following functions

 test1 = ->
           examples = if true
                        "yes"
                      else 
                        "no"
           console.log(examples)

test2 = ->
           examples = if true
                        "yes"
                      else "no"
           console.log(examples)

Either they should be the same, or there should be an error for the latter. Now, what happens is:

 var test1, test2;
 test1 = function() {
   var examples;
   examples = true ? "yes" : "no";
   return console.log(examples);
 };

and

 test2 = function() {
   var examples;
   return examples = true ? "yes" : "no";
};

console.log(examples);

I would hazard to say that the latter is quite confusing.

using version 1.5.0

@vendethiel
Copy link
Collaborator

The else is far too much indented (13)

@michaelficarra
Copy link
Collaborator

This is another occurrence of #1275.

@vendethiel
Copy link
Collaborator

uh?

@aleator
Copy link
Author

aleator commented Feb 27, 2013

Sorry about the duplicate! I thought this was if-else specific.

@Nami-Doc

You mean that it should be

 variable = if foo
             "something"
 else "else"

Perhaps that is the correct syntax. I've no experience with coffee yet.
But it really shouldn't generate the code it currently does. There is no case where that will ever make any sense.

@vendethiel
Copy link
Collaborator

what doesn't make sense is to indent by 13 when it should be by 4. The compiler shouldn't let you do that, but that result seems expectable when you know coffee is a whitespace-sensitive language

@aleator
Copy link
Author

aleator commented Feb 27, 2013

@Nami-Doc

I wonder if the code example is actually rendering correctly on my end. I intended the else to be indented with the i of the if. This is exactly how you often see things like this written in ml/miranda-descendant languages like ocaml and haskell, so I'd argue that the current way is not that universally expectable.

But atleast we agree that it should be an error.

@vendethiel
Copy link
Collaborator

They're not whitespace-sensitive languages. Their blocks do not depend on the indentation.
CoffeeScript does, so it must match.

@epidemian
Copy link
Contributor

They're not whitespace-sensitive languages.

At lest Haskell is. It has some "weird" rules about indentation, but they are very consistent IMO.

@aleator
Copy link
Author

aleator commented Feb 27, 2013

My bad. Ocaml had the indentation-based syntax only as an extension. Haskell blocks certainly are denoted by indentation

@xixixao xixixao mentioned this issue Jan 22, 2014
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

4 participants