-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Refine types according to their constructor val’s singleton types #1262
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
Comments
trait Quux[T <: Foo](val foo: T) Since this seems to be what you're trying to express. |
As a workaround, it seems that we can also use the same trick as for val quux: Quux { val foo: FooInt.type } = new Quux { val foo: FooInt.type = FooInt } |
You’re right, it should apply to class too. I only mentioned traits because that’s what I currently use in Scala to model existential types (because of SI-5700 and SI-5712). Your version works of trait OneMore[Q <: Quux[F], F <: Foo](val quux: Q)
trait AnotherOne[O <: OneMore[Q], Q <: Quux[F], F <: Foo](val oneMore: O)
… Sometimes it is more convenient to work with type members rather than type parameters… |
|
I’m wondering if we can have something similar to what we currently have with dependent methods. Consider the following: def f(foo: Foo): foo.Bar = … We define a method that takes a And we can use that feature as follows: object Quux {
def apply(_foo: Foo): Quux { val foo: _foo.type } =
new Quux { override val foo: _foo.type = foo }
} So that It seems to me that it would be great to just write |
I see how this could be useful, but inferring types which are "too precise" should not be the default because it can force the user to write a lot more type signatures, imagine: val foo1 = new Foo { ... }
val foo2 = new Foo { ... }
var x = new Quux(foo1) // x is inferred to have type `Quux { val foo: foo1.type }`
x = new Quux(foo2) // error: foo2.type does match foo1.type
// To solve this, write "var x: Quux = ..." For similar reasons Dotty does not infer union types ( |
OK, I understand your arguments. |
I'm not saying we couldn't provide some nicer syntaxic sugar to do what you want, but you'll have to make a more detailed proposal :). |
Being able to have constructor parameters for traits is great, however using constructor parameters in the following situation is not very useful because the type
Quux
will not be refined according to the value’s singleton type of thefoo
constructor parameter:And then:
It works if I replace the constructor’s val parameter
foo
with an abstract member and refine its type in the instantiated anonymous class:A workaround proposed in SI-5700 consists of the following:
But I find it a bit cumbersome to use, and anyway this workaround is limited because we can not anymore mix a refined
Quux
in some other class or trait (ie. we can not writenew Something with newQuux(…)
).Do you think it would be possible to refine traits type that have val constructor parameters according to the singleton type of the actual value being passed during the constructor invocation? That would make dependently-typed programming and existential types a lot more convenient.
The text was updated successfully, but these errors were encountered: