New optional array access operator may fail on compilation with nil arrays #593
Answered
by
antonmedv
yoavj-arpeely
asked this question in
General
|
If the compiler knows a variable is nil, the (very cool!) ?.[] operator fails. The error is: This one, however, succeed: |
Answered by
antonmedv
Mar 4, 2024
Replies: 1 comment
|
Compiler don't know the resulting type of If type(x) == type(y), compiler assumes return type of whole expression And in your example: type(nil) != type([1]), so compiler assigns |
0 replies
Answer selected by
antonmedv
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compiler don't know the resulting type of
cond ? x : yexpression on compilation step.If type(x) == type(y), compiler assumes return type of whole expression
cond ? x : yis type(x).If type(x) != type(y), compiler assumes retutn type of whole expression
cond ? x : yis any.And in your example: type(nil) != type([1]), so compiler assigns
arrany type. And any may support indexingarr[0].