Re:Humans should think of sizeof() as a function, says Linus...
by sedeki on Saturday May 30, 2015 @07:16AM
(#9629778)
Doesn't `if` always require parenthesizes?
Re:Humans should think of sizeof() as a function, says Linus...
by Udo on Saturday May 30, 2015 @07:25AM
(#9629800)
No, not always. Not in Go, or in Rust, or Ruby, for example. The assumption being in these languages that if is followed by an expression and that expression is parsed to its natural end anyway - but this only works if the syntax allows for that end to be found without ambiguity.
Re:Humans should think of sizeof() as a function, says Linus...
by krylon on Saturday May 30, 2015 @07:44AM
(#9629842)
In Go, however, go fmt will remove the unnecessary parentheses. I was very annoyed at first, when I saw this, but I got used to it.
Re:Humans should think of sizeof() as a function, says Linus...
by mcguire on Saturday May 30, 2015 @11:24AM
(#9630439)
Does it generally do this in expressions, based on operator precedence, or is it limited to the outer parentheses on selected statements?
Re:Humans should think of sizeof() as a function, says Linus...
by krylon on Saturday May 30, 2015 @11:39AM
(#9630497)
I am not an expert, but I think it only removes the outermost ones. The major task of the tools is indentation and placement of curly braces.
Re:Humans should think of sizeof() as a function, says Linus...
by elisee on Saturday May 30, 2015 @07:22AM
(#9629795)
Yes, in C, but I believe Udo was talking about other languages (like Go for instance)
> and I also do if() even in cases where the language doesn't require it
This sentence could be parsed in multiple ways though :)
Re:Humans should think of sizeof() as a function, says Linus...
by nikai on Saturday May 30, 2015 @07:41AM
(#9629830)
Not even in C. You can have an if condition that is actually a macro that expands with parentheses. For example the macros in ctype.h, so you can actually write
if islower(c) {
(..)
}
Re:Humans should think of sizeof() as a function, says Linus...
by cygx on Saturday May 30, 2015 @08:57AM
(#9630019)
That's not portable, though: A comforming implementation may very well implement islower as a function call, and you should treat is as such.
Re:Humans should think of sizeof() as a function, says Linus...
by raverbashing on Saturday May 30, 2015 @07:48AM
(#9629845)
Yeah, but macros are not really part of the language, they're more a writing aid than anything really.
So, for the sake of good practices, write if with parenthesis.
The thought that if() could just be a lazily-evaluated function taking a code block argument, and that return() could be a way of marking the end result of an expression somehow pleases me.
I think the different expectations about sizeof() come from the artificial distinction between operators and functions, the implication being that in a compiled language the sizeof operator would be a compile-time construct, or barring that, at least a behavior of the type system. On the other hand, there are tons of compiler intrinsics in C/C++ that look exactly like functions but aren't.