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
Welcome to Scala version 2.10.3 (JavaHotSpot(TM) 64-BitServerVM, Java1.7.0_45).
Type in expressions to have them evaluated.
Type:help for more information.
scala>classC { traitT }
defined classC
scala>valc=newC
c:C=C@1130ed80
scala>valt1: c.T= (new { defapply(c: C) =new c.T {} })(c)
t1: c.T= $anon$2$$anon$1@23309add
scala>valt2: c.T= ({c: C=>new c.T {} })(c)
<console>:9:error: typemismatch;
found : $anon where type$anon<: c.Trequired: c.Tvalt2: c.T= ({c: C=>new c.T {} })(c)
^
scala>valf1: { defapply(c: C): c.T } = {c: C=>new c.T {} }
<console>:9:error: typemismatch;
found : C=> $anon forSome { type$anon<: c.T; valc:C }
required: AnyRef{defapply(c: C): c.T}
valf1: { defapply(c: C): c.T } = {c: C=>new c.T {} }
^
The function literal is inferred to existential type {code} C => $anon forSome { type $anon <: c.T; val c: C } {code}, which looks complex, but it is no more special than simple C => C#T.
Could you infer the function literal to {code} (C => C#T) with { def apply(c: C): c.T } {code}
The text was updated successfully, but these errors were encountered:
@retronym said:
The type of a a function literal is always of the form: FunctionN[A, B]
In your example B is an existential type.
Scala doesn't support dependent function types. There is a feature request for this in #4751. I will merge this ticket with that one, by adding your example to the comments of that ticket.
@Atry said (edited on Apr 18, 2014 1:01:21 PM UTC):
No, dependent function types are not required to pass this example.
I assume the function literal is a shortcut of {code}new Function1[C, ] { { def apply(c: C): = new c.T {} } }{code}. I feel surprise when I found the different behavior between function literals and anonymous classes.
I guess referring the function literal to a compound type would suppress the compiler error.
@retronym said:
Section 6.3 of the spec explains how anonymous functions are type checked:
The type of the anonymous function is scala.Functionn[S1, ..., Sn, T ], where T is the packed type (§6.1) of e. T must be equivalent to a type which does not refer to any of the formal parameters xi .
The expansion to an anonymous subclass of FunctionN is only mentioned as the evaluation model:
The anonymous function is evaluated as the instance creation expression
new scala.Functionn[T1, ..., Tn, T] { def apply(x1: T1,...,xn: Tn): T = e
}
The function literal is inferred to existential type {code} C => $anon forSome { type $anon <: c.T; val c: C } {code}, which looks complex, but it is no more special than simple
C => C#T
.Could you infer the function literal to {code} (C => C#T) with { def apply(c: C): c.T } {code}
The text was updated successfully, but these errors were encountered: