type Point = range[0 .. 360]
...
type MyArray = array[Point(0) .. Point(360), int]
but not like this:type Point = range[0 .. 360]
const AllPoints = Point(0) .. Point(360)
...
type MyArray = array[AllPoints, int]
>>> Error: ordinal type expected
I don't understand the difference between the two, and why the former is allowed and the latter is not.try
type
Point = range[0 .. 360]
AllPoints = Point(0) .. Point(360)
MyArray = array[AllPoints, int]
for pt in AllPoints:
do_sth(pt)
No matter, I've just realized that I can use low and high with a type, I'll use this:type MyArray = array[Point.high .. Point.low, int]
@adrien79 Actually, if you tried to iterate over Points as you illustrated, it would break too:
for pt in Points:
do_sth(pt)
You should use explicit low and high:
for pt in Points.low .. Points.high:
do_sth(pt)
type Point = range[0 .. 360]
const AllPoints = Point(0) .. Point(360)
for pt in AllPoints:
do_sth(pt)
What I can't do is insert the same AllPoints in the array type declaration:type MyArray = array[AllPoints, int]
I guess this is because the array declaration expects a type whereas my const AllPoints is a slice, but I don't fully understand the details.I guess this is because the array declaration expects a type whereas my const AllPoints is a slice, but I don't fully understand the details.
Correct, when you use type you define a subrange, but with const it's a slice. As you can read in the docs, array declaration expects an ordinal type for its index, which subrange fulfills but slice doesn't. OTOH, for .. in can loop over a slice, but not a subrange. Using x.low .. x.high you created a slice, so for .. in works with it.