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

abstract val with type Unit

4 replies
Antoras
Joined: 2010-05-23,
User offline. Last seen 1 year 19 weeks ago.

Hello,

following code does not compile:

abstract class X {
val x
}

But this does:

abstract class X {
val x: Unit
}

I know, a value of type Unit doesn't make any sense. This is more a
theoretical question. Why have I explicitly declare a value as type
Unit? Is is because it makes no sense?

Thanks
Antoras

Alex Repain
Joined: 2010-07-27,
User offline. Last seen 1 year 31 weeks ago.
Re: abstract val with type Unit
abstract class X {
 val x
}

does not compile because you need to be able to infer a type for x. Since it is an abstract value, you need to annotate it to allow the type inferer to find it. Now :

abstract class X {
 val x: Unit
}

compiles fine, since you provide a type for x. Unit is a valid type in Scala. a Unit type value has probably no interest at all, but if you turn it into a lazy val, it becomes a one-time evaluation with no return value, which is something for which some functional gurus could kill you, but otherwise, still a possibility in Scala. A random example :

scala> abstract class X {
  val x: Unit
}

-> defined class X

scala> class Y extends X {
 lazy val x = println("using x for first time")
 def useX = {x ; println("lunch time") ; x}
}
-> defined class Y

scala> val y = new Y

-> y: Y = Y@1a4d5c6

scala> y.useX

->
using x for first time
lunchtime
 
2011/8/6 Antoras <mail [at] antoras [dot] de>
Hello,

following code does not compile:

abstract class X {
 val x
}

But this does:

abstract class X {
 val x: Unit
}

I know, a value of type Unit doesn't make any sense. This is more a theoretical question. Why have I explicitly declare a value as type Unit? Is is because it makes no sense?

Thanks
Antoras



--
Alex REPAIN
ENSEIRB-MATMECA - student
TECHNICOLOR R&D - intern
BORDEAUX I      - master's student
SCALA           - enthusiast


Antoras
Joined: 2010-05-23,
User offline. Last seen 1 year 19 weeks ago.
Re: abstract val with type Unit

On 06.08.2011 01:15, Alex Repain wrote:
> abstract class X {
> val x
> }
>
> does not compile because you need to be able to infer a type for x.
That dos not explain why we are allowed to write `def` instead of `val`:

abstract class X {
def x
}

Here, the compiler recognizes successfully type Unit for method x.

Alex Repain
Joined: 2010-07-27,
User offline. Last seen 1 year 31 weeks ago.
Re: abstract val with type Unit


2011/8/6 Antoras <mail [at] antoras [dot] de>
On 06.08.2011 01:15, Alex Repain wrote:
abstract class X {
 val x
}

does not compile because you need to be able to infer a type for x.
That dos not explain why we are allowed to write `def` instead of `val`:

abstract class X {
 def x
}

Here, the compiler recognizes successfully type Unit for method x.


Well, that's a surprise to me since I never tried to use abstract defs without type annotation. The inferred type for x is truly '=> Unit'. However, it makes more sense to default a function type to "=> Unit" rather than a val type to 'Unit'. Not sure if there is a more rational, solid argument in favor of the current behavior. Some category theory on the function space behind that, maybe ?


--
Alex REPAIN
ENSEIRB-MATMECA - student
TECHNICOLOR R&D - intern
BORDEAUX I      - master's student
SCALA           - enthusiast


Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: abstract val with type Unit

On Friday 05 August 2011, Alex Repain wrote:
> abstract class X {
> val x
> }
>
> does not compile because you need to be able to infer a type for x.
> ...

For what it's worth, both of these compile:

abstract class X {
type Y
val y: Y
}

trait Z {
type Q
val q: Q
}

> >
> > Thanks
> > Antoras

Randall Schulz

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