4 ms·
These are good ideas. we also need alternatives for these : %defer %return
by AndyKelley 9y ago
These are good ideas. we also need
alternatives for these :
%defer
%return
- hawski 9y agoSome may fit previous propositions. For %defer: error defer deallocateFoo(foo); but defer deallocateFoo(foo); ex defer deallocateFoo(foo); failsafe deallocateFoo(foo); bail defer deallocateFoo(foo); For %return: fn doAThing(str: []u8) -> %void { const number = try parseU64(str, 10); // ... } "try" could mean "return an error in case of an error" aka "%return". Then "dare" could mean "panic in case of an error" aka "%%foo();". Or use "ex" or "but" everywhere: ex foo(); const num = foo() ex 42; const num = ex foo(); ex defer bar(baz); but foo(); const num = foo() but 42; const num = but foo(); but defer bar(baz);
- ant6n 9y agoEx sounds a lot like example, rather than exception. I really like the use of keywords, leveraging our extensive understanding and intuition of language to make a language more clear and intuitive. Symbolic operators work well if they are borrowed from math (e.g. third grade), making up new ones often results in a painful learning phase.
- iainmerrick 9y agoHow about "catch"? Zig doesn't have the usual try/catch blocks but the meaning is similar. catch defer dealloc(foo);
- e12e 9y agoAm I right in assuming that single ampersand is taken (like in C and/or for bitwise ops)? I think things like "a|b" and "a&b" for or/and is fine - but when you need to stack them two and three deep, keywords starts to look more attractive... I think this holds true even for the relatively benign tripple-equal (===) used as a kind of stand-in for "a is b". BTW, did I miss an obvious short intro to what these things are supposed to mean in zig? I feel I've overlooked an obvious quick-start document? [ed: never mind - the post is just a bit hard to read on a cellphone - but nothing reader mode in Firefox can't fix]
- iainmerrick 9y agoFrom the docs, the prefix %return is a shortcut for: const number = parseU64(str, 10) %% |err| return err; Keeping the %% syntax, how about: const number = parseU64(str, 10) %% throw; (Or "raise" maybe) I think that ordering is more readable than having the %return tucked away in the middle of the line (even more so if it becomes a plain keyword, no % sign). Edit to add: a "throw" keyword could also be used to shorten normal error returns too: return error.InvalidChar; Becomes: throw InvalidChar;