#2120 How to create a List from a Type

SlimerDude Mon 1 Apr 2013

What's the best way to create a parametrised List from a parametrised Type? The only way I can think of is:

listType := Str[]#
list := listType.params["V"].emptyList.rw

Which leads me to my next question...

How do you create a parametrised Map from a parametrised Type?

mapType := Str:Int
map := ...???

The obvious Str[]#.make or [Str:Int]#.make gives

sys::Err: Type missing 'make' or 'defVal' slots: sys::Str[]

andy Mon 1 Apr 2013

You mean like?

List.make(Str#, 10)
Map.make(Str:Int#)

SlimerDude Mon 1 Apr 2013

Yeah, like that!

: )

Cheers.

SlimerDude Sat 11 May 2013

Following on, use Type.parameterize to create your map type.

static Obj:Obj makeMap(Type keyType, Type valType) {
  mapType := Map#.parameterize(["K":keyType, "V":valType])
  return Map.make(mapType)
}

brian Sat 11 May 2013

BTW, simplest way to create list from type is Type.toListOf:

fansh> Str#.toListOf
sys::Str[]

Login or Signup to reply.