In the examples presented previously we’ve explicitly set the reference capability to val:

  1. class Foo[A: Any val]

If the capability is left out of the type parameter then the generic class or function can accept any reference capability. This would look like:

  1. class Foo[A: Any]

It can be made shorter because Any is the default constraint, leaving us with:

  1. class Foo[A]

This is what the example shown before looks like but with any reference capability accepted:

  1. // Note - this won't compile
  2. class Foo[A]
  3. var _c: A
  4. new create(c: A) =>
  5. _c = c
  6. fun get(): A => _c
  7. fun ref set(c: A) => _c = c
  8. actor Main
  9. new create(env:Env) =>
  10. let a = Foo[U32](42)
  11. env.out.print(a.get().string())
  12. a.set(21)
  13. env.out.print(a.get().string())

Unfortunately, this doesn’t compile. For a generic class to compile it must be compilable for all possible types and reference capabilities that satisfy the constraints in the type parameter. In this case, that’s any type with any reference capability. The class works for the specific reference capability of val as we saw earlier, but how well does it work for ref? Let’s expand it and see:

  1. // Note - this also won't compile
  2. class Foo
  3. var _c: String ref
  4. new create(c: String ref) =>
  5. _c = c
  6. fun get(): String ref => _c
  7. fun ref set(c: String ref) => _c = c
  8. actor Main
  9. new create(env:Env) =>
  10. let a = Foo(recover ref String end)
  11. env.out.print(a.get().string())
  12. a.set(recover ref String end)
  13. env.out.print(a.get().string())

This does not compile. The compiler complains that get() doesn’t actually return a String ref, but this->String ref. We obviously need to simply change the type signature to fix this, but what is going on here? this->String ref is an arrow type. An arrow type with “this->” states to use the capability of the actual receiver (ref in our case), not the capability of the method (which defaults to box here). According to viewpoint adaption this will be ref->ref which is ref. Without this arrow type we would only see the field _c as box because we are in a box method.

So let’s apply what we just learned:

  1. class Foo
  2. var _c: String ref
  3. new create(c: String ref) =>
  4. _c = c
  5. fun get(): this->String ref => _c
  6. fun ref set(c: String ref) => _c = c
  7. actor Main
  8. new create(env:Env) =>
  9. let a = Foo(recover ref String end)
  10. env.out.print(a.get().string())
  11. a.set(recover ref String end)
  12. env.out.print(a.get().string())

That compiles and runs, so ref is valid now. The real test though is iso. Let’s convert the class to iso and walk through what is needed to get it to compile. We’ll then revisit our generic class to get it working:

An iso specific class

  1. // Note - this won't compile
  2. class Foo
  3. var _c: String iso
  4. new create(c: String iso) =>
  5. _c = c
  6. fun get(): this->String iso => _c
  7. fun ref set(c: String iso) => _c = c
  8. actor Main
  9. new create(env:Env) =>
  10. let a = Foo(recover iso String end)
  11. env.out.print(a.get().string())
  12. a.set(recover iso String end)
  13. env.out.print(a.get().string())

This fails to compile. The first error is:

  1. main.pony:5:8: right side must be a subtype of left side
  2. _c = c
  3. ^
  4. Info:
  5. main.pony:4:17: String iso! is not a subtype of String iso: iso! is not a subtype of iso
  6. new create(c: String iso) =>
  7. ^

The error is telling us that we are aliasing the String iso - The ! in iso! means it is an alias of an existing iso. Looking at the code shows the problem:

  1. new create(c: String iso) =>
  2. _c = c

We have c as an iso and are trying to assign it to _c. This creates two aliases to the same object, something that iso does not allow. To fix it for the iso case we have to consume the parameter. The correct constructor should be:

  1. new create(c: String iso) =>
  2. _c = consume c

A similar issue exists with the set method. Here we also need to consume the variable c that is passed in:

  1. fun set(c: String iso) => _c = consume c

Now we have a version of Foo that is working correctly for iso. Note how applying the arrow type to the get method also works for iso. But here the result is a different one, by applying viewpoint adaptation we get from ref->iso (with ref being the capability of the receiver, the Foo object referenced by a) to iso. Through the magic of automatic receiver recovery we can call the string method on it:

  1. class Foo
  2. var _c: String iso
  3. new create(c: String iso) =>
  4. _c = consume c
  5. fun get(): this->String iso => _c
  6. fun ref set(c: String iso) => _c = consume c
  7. actor Main
  8. new create(env:Env) =>
  9. let a = Foo(recover iso String end)
  10. env.out.print(a.get().string())
  11. a.set(recover iso String end)
  12. env.out.print(a.get().string())

A capability generic class

Now that we have iso working we know how to write a generic class that works for iso and it will work for other capabilities too:

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

It’s quite a bit of work to get a generic class or method to work across all capability types, in particular for iso. There are ways of restricting the generic to subsets of capabilities and that’s the topic of the next section.