1
votes

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
The error message seems pretty explicit, a value class cannot be an inner class. - Marth
Sorry, still open question for me. I am compiling this from command promt like: scala value.scala . Should I apply any class modifier in example above? - Pavel
Error messages don't get much clearer than this... - Jasper-M
Should I specify any options to compiler? - Pavel
Seems to be related to how the scala command loads files. Compiling with scalac works fine, same with simply typing the line in the REPL or using :load within it. - Marth

1 Answers

1
votes

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