This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Why type inference fails here?

5 replies
gkossakowski
Joined: 2010-03-11,
User offline. Last seen 33 weeks 5 days ago.

Hello,

Short REPL sesssion explains my problem:

Welcome to Scala version 2.7.7.r0-b20100311091045 (Java HotSpot(TM)
64-Bit Server VM, Java 1.6.0_19).
Type in expressions to have them evaluated.
Type :help for more information.

scala> abstract class Node[T]
defined class Node

scala> class NodeHolder[U, W <: Node[U]]
defined class NodeHolder

scala> class NodeHolder[U, W <: Node[U]](val n: W) {
| def grab: U = error("some implementation not important here")
| }
defined class NodeHolder

scala> def node2holder[U, W <: Node[U]](n: W) = new NodeHolder[U, W](n)
node2holder: [U,W <: Node[U]](W)NodeHolder[U,W]

scala> object StringNode extends Node[String]
defined module StringNode

scala> node2holder(StringNode)
:9: error: inferred type arguments [U,object StringNode] do
not conform to method node2holder's type parameter bounds [U,W <:
Node[U]]
node2holder(StringNode)

Why types are not inferred properly even if it looks like all
necessary information is already available.

If this is not going to work any tips how to overcome this problem? I
should probably mention that I want node2holder to be implicit but
without working type inference there is no chance that implicit
conversion will be applied.

Xie Xiaodong
Joined: 2010-03-22,
User offline. Last seen 42 years 45 weeks ago.
Re: Why type inference fails here?
Hello, 

Hope this could help. You could just lend your little help to the Scala compiler. :)


  abstract class Node[T]     class NodeHolder[U, W <: Node[U]](val n: W) {     def grab: U = error("some implementation not important here")  }    def node2holder[U, W <: Node[U]](n: W) = new NodeHolder[U, W](n)     object StringNode extends Node[String]     node2holder[String, Node[String]]( StringNode)



On Fri, Apr 9, 2010 at 9:51 PM, Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com> wrote:
Hello,

Short REPL sesssion explains my problem:

Welcome to Scala version 2.7.7.r0-b20100311091045 (Java HotSpot(TM)
64-Bit Server VM, Java 1.6.0_19).
Type in expressions to have them evaluated.
Type :help for more information.

scala> abstract class Node[T]
defined class Node

scala> class NodeHolder[U, W <: Node[U]]
defined class NodeHolder

scala> class NodeHolder[U, W <: Node[U]](val n: W) {
    | def grab: U = error("some implementation not important here")
    | }
defined class NodeHolder

scala> def node2holder[U, W <: Node[U]](n: W) = new NodeHolder[U, W](n)
node2holder: [U,W <: Node[U]](W)NodeHolder[U,W]

scala> object StringNode extends Node[String]
defined module StringNode

scala> node2holder(StringNode)
<console>:9: error: inferred type arguments [U,object StringNode] do
not conform to method node2holder's type parameter bounds [U,W <:
Node[U]]
      node2holder(StringNode)

Why types are not inferred properly even if it looks like all
necessary information is already available.

If this is not going to work any tips how to overcome this problem? I
should probably mention that I want node2holder to be implicit but
without working type inference there is no chance that implicit
conversion will be applied.

--
Best regards,
Grzegorz Kossakowski



--
Sincerely yours and Best Regards,
Xie Xiaodong
gkossakowski
Joined: 2010-03-11,
User offline. Last seen 33 weeks 5 days ago.
Re: Why type inference fails here?

2010/4/10 Xie Xiaodong :
> Hello,
>
> Hope this could help. You could just lend your little help to the Scala
> compiler. :)
>
>
>   abstract class Node[T]
>
>   class NodeHolder[U, W <: Node[U]](val n: W) {
>      def grab: U = error("some implementation not important here")
>   }
>
>   def node2holder[U, W <: Node[U]](n: W) = new NodeHolder[U, W](n)
>
>   object StringNode extends Node[String]
>
>   node2holder[String, Node[String]]( StringNode)

I think this would have to be something like:

node2holder[String, StringNode]( StringNode)

but I don't know how to refer to type of the object so that code won't compile.

Anyway this is besides the point because I don't want to help compiler
as eventually I would like to make the whole call to 'node2holder'
implicit. In order to do that I need type inference working correctly.

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Why type inference fails here?


On Sat, Apr 10, 2010 at 12:22 PM, Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com> wrote:
2010/4/10 Xie Xiaodong <xxd82329@gmail.com>:
> Hello,
>
> Hope this could help. You could just lend your little help to the Scala
> compiler. :)
>
>
>   abstract class Node[T]
>
>   class NodeHolder[U, W <: Node[U]](val n: W) {
>      def grab: U = error("some implementation not important here")
>   }
>
>   def node2holder[U, W <: Node[U]](n: W) = new NodeHolder[U, W](n)
>
>   object StringNode extends Node[String]
>
>   node2holder[String, Node[String]]( StringNode)

I think this would have to be something like:

 node2holder[String, StringNode]( StringNode)

but I don't know how to refer to type of the object so that code won't compile.

node2holder[String, StringNode.type]( StringNode )
 


Anyway this is besides the point because I don't want to help compiler
as eventually I would like to make the whole call to 'node2holder'
implicit. In order to do that I need type inference working correctly.

--
Best regards,
Grzegorz Kossakowski



--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall

Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
Xie Xiaodong
Joined: 2010-03-22,
User offline. Last seen 42 years 45 weeks ago.
Re: Why type inference fails here?
Then try using this: 

node2holder[String,StringNode.type](StringNode)


On Sat, Apr 10, 2010 at 12:22 PM, Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com> wrote:
2010/4/10 Xie Xiaodong <xxd82329@gmail.com>:
> Hello,
>
> Hope this could help. You could just lend your little help to the Scala
> compiler. :)
>
>
>   abstract class Node[T]
>
>   class NodeHolder[U, W <: Node[U]](val n: W) {
>      def grab: U = error("some implementation not important here")
>   }
>
>   def node2holder[U, W <: Node[U]](n: W) = new NodeHolder[U, W](n)
>
>   object StringNode extends Node[String]
>
>   node2holder[String, Node[String]]( StringNode)

I think this would have to be something like:

 node2holder[String, StringNode]( StringNode)

but I don't know how to refer to type of the object so that code won't compile.


Anyway this is besides the point because I don't want to help compiler
as eventually I would like to make the whole call to 'node2holder'
implicit. In order to do that I need type inference working correctly.

--
Best regards,
Grzegorz Kossakowski



--
Sincerely yours and Best Regards,
Xie Xiaodong
gkossakowski
Joined: 2010-03-11,
User offline. Last seen 33 weeks 5 days ago.
Re: Why type inference fails here?

2010/4/10 Xie Xiaodong :
> Then try using this:
>
> node2holder[String,StringNode.type](StringNode)

Thanks Xie and Viktor, I learned something :-)

Anyway my original problem seems to be more complicated...

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland