dictu-lang / Dictu

Dictu is a high-level dynamically typed, multi-paradigm, interpreted programming language.
https://dictu-lang.com
MIT License
268 stars 53 forks source link

[FEATURE] *hoist* keyword #720

Closed Jason2605 closed 7 months ago

Jason2605 commented 8 months ago

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):

var someVariable;

if (condition) {
    someVariable = ...;
} else {
    someVariable = ...;
}

print(someVariable);

// OR
var contents;

with ("file.txt", "r") {
    contents = file.read();
}

print(contents);

I wonder what thoughts would be on a new hoist keyword which works the same as var 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)

if (condition) {
    hoist someVariable = ...;
} else {
    hoist someVariable = ...;
}

print(someVariable);

with ("file.txt", "r") {
    hoist contents = file.read();
}

print(contents);

Describe the solution you'd like

Open to other ideas as well

Describe alternatives you've considered

No response

Additional context

No response

liz3 commented 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()

Jason2605 commented 7 months ago

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!

liz3 commented 7 months ago

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

Jason2605 commented 7 months ago

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

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!