erde-lang / erde

A programming language that compiles to Lua.
https://erde-lang.github.io
MIT License
38 stars 4 forks source link

Request for assignment operator for variables if they are `nil` #39

Open wauterboi opened 4 months ago

wauterboi commented 4 months ago

I think that it would be nice to have an operator for assigning if any only if the identifier on the left hand side has no value.

x ?= y could compile to if x == nil then x = y end.

bsuth commented 4 months ago

Hmm this is similar to another idea I had in the earlier stages of Erde, which actually supported null coalescing operators (and thus were a superset of this feature request, as one could do x ??= y). However, similar to optional chaining, this was removed before v1.0.0 was hit, since the only way to support it for all cases was to have IIFEs in the compiled code (something I really didnt want to introduce, as it makes error rewriting a nightmare, introduces the overhead of an IIFE, and the compiled code started to get too far away from "intuitive" for Lua developers).

If there is a better way to support null coalescing operators, I would love to have it but if not this may be the best we can do. However, there may be some other more general syntaxes that would include this case as well as others (not sure if there are, just dont want to jump to adding features), so do you mind if i keep this issue open to gauge interest / ideas from others who also want a similar improvement?

wauterboi commented 4 months ago

You're definitely free to do that. Any reason why you couldn't do this?

-- x ?= y
if x == nil {
x = y
}

I'm trying to think of a situation where having that expansion would cause problems.

-------- Original Message -------- On 7/16/24 02:21, Brian Sutherland wrote:

Hmm this is similar to another idea I had in the earlier stages of Erde, which actually supported null coalescing operators (and thus were a superset of this feature request, as one could do x ??= y). However, similar to optional chaining, this was removed before v1.0.0 was hit, since the only way to support it for all cases was to have IIFEs in the compiled code (something I really didnt want to introduce, as it makes error rewriting a nightmare, introduces the overhead of an IIFE, and the compiled code started to get too far away from "intuitive" for Lua developers).

If there is a better way to support null coalescing operators, I would love to have it but if not this may be the best we can do. However, there may be some other more general syntaxes that would include this case as well as others (not sure if there are, just dont want to jump to adding features), so do you mind if i keep this issue open to gauge interest / ideas from others who also want a similar improvement?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

bsuth commented 4 months ago

ope sorry, rereading my last message I was not clear at all.

You definitely could do that, I just wanted to wait and see if maybe there are other constructs that are more powerful, as this would be a new language feature / syntax with a somewhat specific use case (something im trying to avoid, since I want to keep the feature of Erde rather minimal). For example, null coalescing operators can be used in many other places (arbitrary expressions) and also get this behavior for free when combined with operator assignments (i.e. ??=)