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

inferring Unit

1 reply
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.

This is relevant to how to fix a bug I'm looking at. Is this behavior correct? It seems wrong to me. I would
think new Foo with no parameters should be an error, not interpreted as passing Unit.

$ scala273
Welcome to Scala version 2.7.3.final (Java HotSpot(TM) Client VM, Java 1.5.0_16).
Type in expressions to have them evaluated.
Type :help for more information.

scala> implicit def u2i(x: Unit): Int = 55
u2i: (Unit)Int

scala> class Foo(val x: Int)
defined class Foo

scala> val y = new Foo
y: Foo = Foo@a6bcec

scala> y.x
res0: Int = 55

Erik Engbrecht
Joined: 2008-12-19,
User offline. Last seen 3 years 18 weeks ago.
Re: inferring Unit
I think the question is: Does the blank following Foo constitute an empty expression, which would evaluate to Unit and thereby be able to trigger the implicit, or is the blank just a blank and mean there is no expression?

On Sun, Feb 22, 2009 at 7:25 PM, Paul Phillips <paulp@improving.org> wrote:
This is relevant to how to fix a bug I'm looking at.  Is this behavior correct? It seems wrong to me.  I would
think new Foo with no parameters should be an error, not interpreted as passing Unit.

$ scala273
Welcome to Scala version 2.7.3.final (Java HotSpot(TM) Client VM, Java 1.5.0_16).
Type in expressions to have them evaluated.
Type :help for more information.

scala> implicit def u2i(x: Unit): Int = 55
u2i: (Unit)Int

scala> class Foo(val x: Int)
defined class Foo

scala> val y = new Foo
y: Foo = Foo@a6bcec

scala> y.x
res0: Int = 55

--
Paul Phillips      | Every election is a sort of advance auction sale
Everyman           | of stolen goods.
Empiricist         |     -- H. L. Mencken
i pull his palp!   |----------* http://www.improving.org/paulp/ *----------



--
http://erikengbrecht.blogspot.com/

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