- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
abstract val with type Unit
Fri, 2011-08-05, 23:48
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
Sat, 2011-08-06, 00:37
#2
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.
Sat, 2011-08-06, 01:37
#3
Re: abstract val with type Unit
2011/8/6 Antoras <mail@antoras.de>
On 06.08.2011 01:15, Alex Repain wrote:
abstract class X {That dos not explain why we are allowed to write `def` instead of `val`:
val x
}
does not compile because you need to be able to infer a type for x.
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
Sat, 2011-08-06, 04:27
#4
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
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@antoras.de>
--
Alex REPAIN
ENSEIRB-MATMECA - student
TECHNICOLOR R&D - intern
BORDEAUX I - master's student
SCALA - enthusiast