generics - Why can't Scala infer the type parameter in this example? -
Suppose I have two squares, input
and output
, which That output
is designed to be connected to each other, produces some types of values, and consumes input
. Class input [T] {var output: option [output [& lt;: t]] = none} square output [t] {var input: option [input [_ & gt; ; T]] = none
This is fine if a I have a If I tell this method below, I get a type of error: error: I can solve it by typing the type of parameter (in this case, Connect [string] Edit: For now, I have I still am interested in why the compiler displays this behavior. I think this type parameter should be able to guess. ? input and
output pair type parameter
output Type parameters Note that the type of parameters in both classes is irreversible; In actual versions it is used in both co-and contaminated conditions.
connect method that connects to a
, but I think the compiler should be able to figure it out for me. How can I change the input def [T] (output: output [T], input: input [_ & Gt; T]) = {output.input added: = Some input (input) input Output = some (output)}
val out = new Output [string] val = new input [AnyRef] connect (out, in)
test.scala: 17: error: type Unmatched; Found: output [string] requirement: output [AnyRef] connect (out, in) ^
connect
method so that the type parameters are automatically estimated ?
connect
to Output
, so it automatically receives the type parameter. Also there is an added advantage that I can use infix notation to connect
, but the design It seems a bit weird.
You can sometimes get better results while using multiple parameter lists Will receive:
def connect [out] (out): (Output: output [T]) (Input: input [_ & gt; T]) = {output.input = some (input) input Output = some (output)} connect (out)
... and in fact in this case, it works.
Comments
Post a Comment