expr icon indicating copy to clipboard operation
expr copied to clipboard

Unexpected map key type conversion

Open halvfigur opened this issue 1 month ago • 1 comments

It seems map keys that aren't strings get implicitly converted to string. For example, the following expressions fail unexpectedly

1 in { 1: 0 }

Error:
cannot use int as type string in map key (1:3)
 | 1 in {1: 0}
 | ..^
{ 1: 0 }[1]

Error:
cannot use int to get an element from map[string]interface {} (1:8)
 | {1: 0}[1]
 | .......^

This expression evaluates to true

{1: 0} | keys() | first() | type() == "string"

halvfigur avatar Nov 17 '25 09:11 halvfigur

Yes, all {} within Expr created as map[string]any for uniformity.

{ 1: 0, "str": 2 }

But you can use in with an array.

1 in [1,2,3]

Expr will compile array to object map[int]struct{}

0  OpPush  <0>  1
1  OpPush  <1>  map[1:{} 2:{} 3:{}]
2  OpIn

antonmedv avatar Nov 17 '25 16:11 antonmedv