You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
sealedtraitNatcaseobjectZextendsNatcaseclassS[N<:Nat](valpred:N) extendsNat@main defTest1=classPartially[M](m: M)
defresult(m1: M) = println(s"$m and $m1")
valx=Partially(S(S(Z))).result(S(Z))
// Typer:// val x: Unit =// new Partially[S[(Nat & Product & Serializable)]](// S.apply[(Nat & Product & Serializable)](S.apply[Z$](Z))// ).result(S.apply[(Nat & Product & Serializable)](Z))
I would expect result to be callable only on S(S(Z)). Note the typer output (as reported by-Xprint:frontend): the type parameter of new Partially is S[(Nat & Product & Serializable)] though should be S[S[Z]].
The following reports an error properly:
@main defTest2=classPartially[M](m: M)
defresult(m1: M) = println(s"$m and $m1")
valx=Partially(S(S(Z)))
x.result(S(Z))
// Typer:// val x: Partially[S[S[Z$]]] =// new Partially[S[S[Z$]]](S.apply[S[Z$]](S.apply[Z$](Z)))// x.result(S.apply[S[Z$]](Z))
Error:
-- [E007] TypeMismatchError:/Users/anatolii/Projects/dotty/pg/nats/Test.scala:19:1319| x.result(S(Z))
|^|Found:Z.type|Required:S[objectZ]
result of /Users/anatolii/Projects/dotty/pg/nats/Test.scala after frontend:
Evidently type inference tries its best to compile as wide range of expressions as possible here, readily giving up type precision (and hence type safety) as shown above.
Probably what happens here is the easiest to resolve Nat is resolved and the type parameter to f is generalised to fit both the passed and the resolved argument.
Works without givens as well – the below passes the compilation:
I can imagine this behaviour to be a problem for type-safe programming: it is way to easy to bargain with the type system here to get the types that will make the program compile.
/cc @AleksanderBG
The text was updated successfully, but these errors were encountered:
Evidently type inference tries its best to compile as wide range of expressions as possible here, readily giving up type precision (and hence type safety) as shown above.
Type inference job is to find type arguments that make the expression typecheck. If such arguments exist, you cannot assume that type inference will not find them (and you can't assume that the user won't write them himself either), so this is not a type safety issue.
The following works though shouldn't:
I would expect
result
to be callable only onS(S(Z))
. Note the typer output (as reported by-Xprint:frontend
): the type parameter ofnew Partially
isS[(Nat & Product & Serializable)]
though should beS[S[Z]]
.The following reports an error properly:
Error:
Evidently type inference tries its best to compile as wide range of expressions as possible here, readily giving up type precision (and hence type safety) as shown above.
Another example:
Outputs:
Probably what happens here is the easiest to resolve
Nat
is resolved and the type parameter tof
is generalised to fit both the passed and the resolved argument.Works without givens as well – the below passes the compilation:
The type is inferred precisely if both arguments have the same type (see typer output below):
I can imagine this behaviour to be a problem for type-safe programming: it is way to easy to bargain with the type system here to get the types that will make the program compile.
/cc @AleksanderBG
The text was updated successfully, but these errors were encountered: