简体   繁体   English

Scala:如何定义值类

[英]scala: how to define a value class

Quick question regrading how to define a value class in scala 快速问题解答如何在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 操作系统:FreeBSD 10.2 Scala代码运行程序版本2.11.7-版权所有2002-2013,LAMP / EPFL

Could anyone help with this? 有人可以帮忙吗?

Thanks 谢谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM