teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.03k stars 101 forks source link

Suggestion: if local syntax #672

Closed ggoraa closed 1 year ago

ggoraa commented 1 year ago

Basically something that Swift has for safely unwrapping nil values. An example of such syntax in Swift:

let value: String? = nil

if let value {
    // value is now of type String, not String?
    // if value is nil, this block won't even be executed
    print(value)
}

if let otherName = value {
    // Basically the same thing, just the name of the variable being changed
}

This syntax is basically syntax sugar for this:

if value != nil {
    print(value!) // The ! is force unwrapping, as in crash if there is a nil
}

It can look in Teal like this:

if local value then 
    print(value)
end
lenscas commented 1 year ago

I don't get why teal would need special syntax for this?

Right now, teal doesn't have a T? type so it doesn't help with that, and in lua the only falsy types are nil and false so 99% of the time it is ok to just do if value then.

ggoraa commented 1 year ago

Oh, wow, I didn't know about such syntax in Lua I will close it then