- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why type inference fails here?
Fri, 2010-04-09, 20:51
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.
Sat, 2010-04-10, 11:27
#2
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.
Sat, 2010-04-10, 11:27
#3
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
Sat, 2010-04-10, 11:37
#4
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:
--
Sincerely yours and Best Regards,
Xie Xiaodong
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
Sat, 2010-04-10, 11:47
#5
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...
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:
--
Sincerely yours and Best Regards,
Xie Xiaodong