Skip to content

Expression syntax and semicolon #1712

New issue

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

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

Already on GitHub? Sign in to your account

Closed
xjia1 opened this issue Jan 31, 2012 · 6 comments
Closed

Expression syntax and semicolon #1712

xjia1 opened this issue Jan 31, 2012 · 6 comments

Comments

@xjia1
Copy link

xjia1 commented Jan 31, 2012

Hi, I'm a Rust language newbie so what I mention below may be non-sense. :-)

I think it is not that wise to make use of semicolon (;) as the separator between statements along with the expression syntax provided in the language, because it will potentially lead to many typo-caused bugs. For example, for the following snippet from the Rust language tutorial:

let x = if the_stars_align() { 4 }
        else if something_else() { 3 }
        else { 0 };

If the expressions inside the braces are function calls, it would be harder to decide:

let x = if the_stars_align() { my_pretty_print_function(123) }
        else if something_else() { your_pretty_print_function(456) }
        else { our_error_print_function(789) };

and

let x = if the_stars_align() { my_pretty_print_function(123); }
        else if something_else() { your_pretty_print_function(456); }
        else { our_error_print_function(789); };
@catamorphism
Copy link
Contributor

I'm not sure I understand the question. If x is used in a context that expects a type other than nil, the second version of the code would be a type error, because all of the if branches have type nil (the trailing semicolon says "do this and throw away the result", so the blocks all have type nil).

Maybe I'm missing something, though?

@lht
Copy link
Contributor

lht commented Jan 31, 2012

@catamorphism It is actually not an error if x is assigned to nil type like the last example above. Or, are we going to change this?

@catamorphism
Copy link
Contributor

@lht Yes, it's not an error, but it would be an error to use x (except by, say, passing it to a function that had an argument of type nil, but you probably would never write such a function). I'm still not exactly sure what the situation is that you're worried about that would be confusing -- can you explain further, maybe with a code example for the part of the code that uses x?

@xjia1
Copy link
Author

xjia1 commented Jan 31, 2012

  • Would it be complicated if x is passed across abstraction boundaries?
  • Would it be much harder to locate where the exact problem source is (in this case the semicolons inside the braces of the let x statement) if x is later used in many other lets and expressions inside an abstraction boundary?
  • What if the previous two cases mixing together?

I think semicolons in the expression syntax should be forbidden (maybe in the phase of semantics analysis) since let x = nil doesn't seem useful from my perspective. And it may confuse some not-that-experienced programmers. Such kind of easy typos may also fail an integrated compilation and test quickly which I think is not that good since it will possibly make everyone jittery.

@lht
Copy link
Contributor

lht commented Jan 31, 2012

See also #1195, which suggests adding better compiler warning or error messages.

@nikomatsakis
Copy link
Contributor

I don't see this as a problem. We are certainly not going to prohibit semicolons in blocks. They are very useful. I could see a warning if some branches of an if or alt have no tail expression while others do, though I am inclined against it as any real problem will be caught by type checker. I am going to close this issue but feel free to add to #1195 concerning a warning if you feel that would be appropriate.

Sent via Hubroid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants