asoffer / Icarus

An experimental general-purpose programming language
Apache License 2.0
9 stars 2 forks source link

How to: get the size of an array #22

Closed perimosocordiae closed 3 years ago

perimosocordiae commented 3 years ago

How does one do it?

foo := [1,2,3]
n := foo.size  // doesn't compile
perimosocordiae commented 3 years ago

Appears to still be broken:

$ cat demo.ic 
foo := [1,2,3]
foo.length

$ icarus demo.ic 
Expressions of type `[3; int64]` have no member named `length`.

  2 | foo.length
asoffer commented 3 years ago

.length applies to the type, not the value of that type. Given a value of a type, you can use :? to get the type. So you could write foo:?.length. (As an aside, I don't love this typeof operator and am very open to changing the syntax.)

The reason we have it only as a property of the type is to avoid decisions about whether some_array.length evaluates the side-effects of some_array.