ponylang/ponyc

Dependent default types

tetotechy opened this issue · 4 comments

I also have a question about the possibility of using default types referencing other generic types in the same object. This code won't compile.

The rest of the code apparently is fine, as explicitly associating the type in the function body shows.

Thanks!

This isn't an actionable bug. In keeping with our getting help documentation, please ask "how do I" questions in the Zulip.

Closing

Apologies for not making myself clear. What I meant is that the following code compiles (I changed the example a bit):

actor Main
  new create(env: Env) =>
    Foo[U8, Array[U8]](env, 10)(3)
  

class Foo[T: Stringable val, Arr: Seq[T] ref = Array[T]]
  let _env: Env
  var arr: Arr = Arr
  
  new create(env: Env, v: T) =>
    _env = env
    arr.push(v)
    
  fun ref apply(v: T) =>
    arr.push(v)
    for el in arr.values() do
      _env.out.print(el.string())
    end

whilst omitting the second generic type when instantiating - to leverage the default type, the compiler gives an error:

actor Main
  new create(env: Env) =>
    Foo[U8](env, 10)(3)
  

class Foo[T: Stringable val, Arr: Seq[T] ref = Array[T]]
  let _env: Env
  var arr: Arr = Arr
  
  new create(env: Env, v: T) =>
    _env = env
    arr.push(v)
    
  fun ref apply(v: T) =>
    arr.push(v)
    for el in arr.values() do
      _env.out.print(el.string())
    end

No problems to revert to Zulip, happy to do so if this is expected behavior. Thanks!

jemc commented

This is definitely something that I would have expected to work as desired.

The key issue here is that the compiler is comparing the constraint Seq[U8] to the default arg Array[T]. But to make this compile correctly it would either need to compare Seq[T] withArray[T] or alternatively, Seq[U8] with Array[U8].

We discussed this a bit in the sync call today, but haven't done any triaging yet into why this doesn't work as expected today. Needs further troubleshooting.