Open jon4hz opened 1 month ago
i am confused. why should this code work? result
is clearly Text
here.
you want an Any
type, right? or something like OneOf<[Text, Int]>
i am confused. why should this code work? result is clearly Text here.
Yeah in this example result
is Text
but result
could be also a Num
, so this example shouldn't cause a compiler/transpiler error. I mean, that's the entire point of the type condition...
It should work like so @b1ek:
fun foo(val: Text) {
if val is Text: val = "hello"
else: val = 12
}
foo("test")
The function should not throw error. Because the value will always be Text
so the else
clause will never run.
Right now it errors that val = 12
is incorrect as it's unable to assign Num
to Text
I think this issue should only cover type evaluations that can be known at compile time. We fallback to the regular behavior when some arithmetic comparison is detected.
Do types even exist at runtime?
I mean, this code:
let var = "hi!";
if var is Text {
echo var
}
compiles to this:
__0_var="hi!"
if [ 1 != 0 ]; then # the "if var is Text" part
echo "${__0_var}"
fi
and 1
is obviously not 0
@b1ek Types do not exist at runtime. All var is Type
resolve at the compile time
Currently type condition's aren't lazy evaluated which causes the following code to fail:
Error:
Cannot assign value of type 'Num' to a variable of type 'Text'