Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with "nested" type parameters #1921

Closed
mfelsche opened this issue May 19, 2017 · 4 comments
Closed

Problems with "nested" type parameters #1921

mfelsche opened this issue May 19, 2017 · 4 comments

Comments

@mfelsche
Copy link
Contributor

I am creating an instance of a type-parameterized class from a type parameterized trait.
Both type parameters are unconstrained.
I feed the class a type parameter that is also an unconstrained type parameter.
the compiler complains:
CLASS_TYPE_PARAMETER #any is not a subtype of TRAIT_TYPE_PARAMETER #any: the subtype has no constraint.

In my superficial understanding of the type system, it should be possible to use an unconstrained type param for another unconstrained type param.

This small snippet illustrates my problem:

trait Generator[T]
  fun box generate(): T
  
  fun map[U](mapFn: {(T!): U} val): Generator[U] =>
    MappedGenerator[T, U](this, mapFn)


class MappedGenerator[S, V] is Generator[V]

  let _source: Generator[S] box
  let _mapFn: {(S!): V} val
  
  new create(source: Generator[S] box, mapFn: {(S!): V} val) =>
    _source = source
    _mapFn = mapFn

  fun box generate(): V =>
    _mapFn(_source.generate())


class TestGenerator is Generator[Bool val]
    fun box generate(): Bool val => true
    
    
actor Main
  new create(env: Env) =>
    let mapped = TestGenerator.map[Bool]({(b: Bool): Bool => not b})
    env.out.print("does it work? " + mapped.generate().string())
    

This is what the compiler says:

0.14.0 [release]
compiled with: llvm 3.9.1 -- cc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Error:
main.pony:5:20: type argument is outside its constraint
    MappedGenerator[T, U](this, mapFn)
                   ^
    Info:
    main.pony:8:42: argument: V #any
    class MappedGenerator[S, V] is Generator[V]
                                             ^
    main.pony:8:23: constraint: U #any
    class MappedGenerator[S, V] is Generator[V]
                          ^
    main.pony:8:42: V #any is not a subtype of U #any: the subtype has no constraint
    class MappedGenerator[S, V] is Generator[V]
                                             ^
@jemc
Copy link
Member

jemc commented May 20, 2017

Somebody can correct me if I'm wrong, but it seems like this should be allowed.

Especially because it compiles okay if the Generator.map method is removed, and the MappedGenerator is created "by hand". This suggests it has something to do with the mutually recursive relationship between Generator and MappedGenerator.

@plietar
Copy link
Contributor

plietar commented May 21, 2017

I believe this is the same bug as #1875. It builds fine with my fix from PR #1888.

@mfelsche
Copy link
Contributor Author

I can confirm that this particular case is fixed with #1888
Great stuff! :) can't wait for the release

@plietar
Copy link
Contributor

plietar commented May 21, 2017

Unfortunately this PR is blocked by another issue (#1887), and I'm a bit too busy at the moment to implement the fix required for that one. Maybe we should just merge PR #1888 with the included workaround, and revert it later.
I'll close this since it's confirmed to be the same as #1875, and mark the PR for discussion on this week's sync.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants