scala: how to define a value class

2020-03-31 02:11发布

问题:

Quick question regrading how to define a value class in scala

Here is typical example:

class Wrapper(val underlying: Int) extends AnyVal

I am getting next error:

/usr/home/User/scala2/scala_lerning/src3/val.scala:1: error: value class may not be a member of another class
class Wrapper(val underlying: Int) extends AnyVal
      ^
one error found

OS: FreeBSD 10.2 Scala code runner version 2.11.7 -- Copyright 2002-2013, LAMP/EPFL

Could anyone help with this?

Thanks

回答1:

It seems you're doing

class Foo {
  class Wrapper(val underlying: Int) extends AnyVal
}

which you can't. You have to do

class Foo {
}
class Wrapper(val underlying: Int) extends AnyVal


标签: scala