3 ms·
Wouldn't this be better served with defaults? // constructor func init (a, b, c) { if (!a) a = 0; if (!b) b = 0; if (!c) c = 0;
by Entangled 10y ago
Wouldn't this be better served with defaults?
// constructor
func init (a, b, c) {
if (!a) a = 0;
if (!b) b = 0;
if (!c) c = 0;
x = a; y = b; z = c;
}
Like
func init (a=0, b=0, c=0) {
x = a; y = b; z = c;
}
- pmontra 10y agoYes, but I guess it's only an example to demonstrate one liners and anything would do. I prefer not to have to surround conditions with () and terminate statements with semicolons. Plenty of languages demonstrated that it's ok. However I don't know how that impacts the complexity of the compiler. I used to know how to write one and reason about it but I spent my last 25 years doing other things. Nice touch to be able to do func + (v) { ... }
- spc476 10y agoIn Lua, this would be: function init(a,b,c) local x = a or 0 local y = b or 0 local z = c or 0 end "and" and "or" short circuit evaluation in Lua; nil and false are false, everything else (including 0) is considered true.