boo

Typed list comprehensions in Boo?


I can't seem to grok the correct way to get a properly typed list comprehension in boo. Since the compiler works by inference I'd expect that in this example:

fred as (int)
fred = (1,2,3)   # fred is an array of ints
barney = [i for i in fred]

the barney would be a list[of int], since the comprehension is running off of a typed array. However the actual value of barney is just an untyped boo.lang.list: it happens to contain only int's but it won't complain, for example, if I try:

barney.Add("A")

which I would expect to fail but which actually succeeds.

Is there a way to use the comprehension syntax to generate a typed list?


Solution

  • Rodrigo, the creator of boo, provided the answer

    fred = (1,2,3)   # fred is an array of ints
    barney = List[of int](i for i in fred)
    

    The parenthesized expression generates the arguments for the creation of the typed list.