Stenway Developer Network

Generics

Examples:

package MyLib

class List<T>
  
end

class StringList extends List<String>

end

class KeyValuePair<TKey,TValue>
  key TKey
  value TValue
end

Constraints

Restrict to class type:

package MyLib

class ItemBase
  method doSomething()
    
  end
end

class ConcreteItem extends ItemBase
  
end

class MyList<T is ItemBase>
  method add(item T)
    item.doSomething()
  end
end

class App
  static method main()
    var list MyList<ConcreteItem>
    list.add(new ConcreteItem())
  end
end

Restrict to interface type:

package MyLib

interface IItem
  method doSomething()
end

class ConcreteItem implements IItem
  method doSomething()
    
  end
end

class MyList<T is IItem>
  method add(item T)
    item.doSomething()
  end
end

class App
  static method main()
    list MyList<ConcreteItem>
    list.add(new ConcreteItem())
  end
end

Nullable

Nullable type arguments must be explicitly allowed.

package MyLib

class KeyValuePair<TKey,TValue?>
  key TKey
  value TValue?
  
  constructor(key TKey, value TValue?)
    this.key = key
    this.value = value
  end
end

class App
  static method main()
    kvp1 KeyValuePair<String,Int>("Key", 123)
    kvp2 KeyValuePair<String,Int?>("Key", null)
  end
end