Capability Constraints

The type parameter constraint for a generic class or method can constrain to a particular capability as seen previously:

  1. class Foo[A: Any val]

Without the constraint, the generic must work for all possible capabilities. Sometimes you don’t want to be limited to a specific capability and you can’t support all capabilities. The solution for this is generic constraint qualifiers. These represent classes of capabilities that are accepted in the generic. The valid qualifiers are:

Capabilities allowedDescription
#readref, val, boxAnything you can read from
#sendiso, val, tagAnything you can send to an actor
#shareval, tagAnything you can send to more than one actor
#anyiso, trn, ref, val, box, tagDefault of a constraint
#aliasref, val, box, tagSet of capabilities that alias as themselves (used by compiler)

In the previous section, we went through extra work to support iso. If there’s no requirement for iso support we can use #read and support ref, val, and box:

  1. class Foo[A: Any #read]
  2. var _c: A
  3. new create(c: A) =>
  4. _c = c
  5. fun ref get(): this->A => _c
  6. fun ref set(c: A) => _c = c
  7. actor Main
  8. new create(env:Env) =>
  9. let a = Foo[String ref](recover ref "hello".clone() end)
  10. env.out.print(a.get().string())
  11. let b = Foo[String val]("World")
  12. env.out.print(b.get().string())