Closed Jason2605 closed 7 months ago
I don't think this is a good idea imho because it makes code a lot less readable and introduces pitfalls of scopes when "reading" code. Personally i don't really mind writing this code explicitly but something zig or rust(i think?) does is something like
var someVariable = if (condition) {
...;
} else {
...;
};
var contents = with ("file.txt", "r") {
file.read();
}
there could also be a explicit return rather then implicit capture of the last expression. not returning a value should be a compile time error()
We wouldn't want the with statement (or I guess now expression) block to have to return a value as you wouldn't always have to (think of writing to a file (we also wouldn't be able to work out the file opening mode either until runtime))
Maybe I was trying to solve an issue that wasn't there, but I got a little annoyed at having to wrote var contents
to then change it in the with block.
Appreciate the challenge on this though, good to bounce ideas!
I can def see it getting annoying with the with
statement, maybe it would be worth considering another way of directly getting file content, like python allows to call open
without with
, with the tradeof of having to explicitly close the fd
I can def see it getting annoying with the
with
statement, maybe it would be worth considering another way of directly getting file content, like python allows to callopen
withoutwith
, with the tradeof of having to explicitly close the fd
Oops sorry missed this response - yeah potentially a good shout! I'm probably with you though that the hoist keyword isn't the way to solve that issue so gonna close this out, thanks for the spitball!
Is there an existing issue for this?
Is your feature request related to a problem?
This is kind of me just splurging my thoughts into this issue (haven't really looked into the technicalities of it yet) but I often find myself writing this code (ignoring ternaries exist for a second):
I wonder what thoughts would be on a new
hoist
keyword which works the same asvar
except it is available to the scope above it was defined in, it means we are able to get rid of the initial declaration (in theory)Describe the solution you'd like
Open to other ideas as well
Describe alternatives you've considered
No response
Additional context
No response