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 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.

I have a connect method that connects to a input def [T] (output: output [T], input: input [_ & Gt; T]) = {output.input added: = Some input (input) input Output = some (output)}

If I tell this method below, I get a type of error:

  val out = new Output [string] val = new input [AnyRef] connect (out, in)  

error:

  test.scala: 17: error: type Unmatched; Found: output [string] requirement: output [AnyRef] connect (out, in) ^  

I can solve it by typing the type of parameter (in this case, Connect [string] , but I think the compiler should be able to figure it out for me. How can I change the connect method so that the type parameters are automatically estimated ?


Edit: For now, I have 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.

I still am interested in why the compiler displays this behavior. I think this type parameter should be able to guess. ?

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

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -