AnyVal 的目的是什么? [英] What is the purpose of AnyVal?

查看:54
本文介绍了AnyVal 的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想不出任何情况下 AnyVal 类型会有用,尤其是添加了 Numeric 类型以抽象 IntLong 等.AnyVal 是否有任何实际用例,或者它只是一个使类型层次结构更漂亮的工件?

I can't think of any situation where the type AnyVal would be useful, especially with the addition of the Numeric type for abstracting over Int, Long, etc. Are there any actual use cases for AnyVal, or is it just an artifact that makes the type hierarchy a bit prettier?

澄清一下,我知道AnyVal 是什么,我只是想不出任何时候我会在Scala中真正需要它.我什么时候需要包含 IntCharacterDouble 的类型?似乎它只是为了使类型层次结构更漂亮(即,将 AnyValAnyRef 作为兄弟姐妹而不是 Int 看起来更好,Character 等直接继承自 Any).

Just to clarify, I know what AnyVal is, I just can't think of any time that I would actually need it in Scala. When would I ever need a type that encompassed Int, Character and Double? It seems like it's just there to make the type hierarchy prettier (i.e. it looks nicer to have AnyVal and AnyRef as siblings rather than having Int, Character, etc. inherit directly from Any).

推荐答案

让我们去看录像带,呃,规范 12.2:

Let's go to the videotape, er, the spec 12.2:

值类是其实例不表示为的类底层主机系统的对象.所有值类都继承自类 AnyVal.

Value classes are classes whose instances are not represented as objects by the underlying host system. All value classes inherit from class AnyVal.

所以,也许问题是,如果一切都是对象,我为什么要关心某些东西是否没有被表示,即作为对象实现?这是实现细节中的实现.

So, maybe the question is, if everything is an object, why do I care if something is not represented i.e. implemented as an object? That's the implementation in implementation detail.

但我们不要假装,你当然关心.你从来不专攻吗?

But let's not pretend, of course you care. Do you never specialize?

规范还在继续:

Scala 实现需要提供值类 Unit、Boolean、Double、Float、Long、Int、Char、Short 和 Byte(但可以自由选择也提供其他人).

Scala implementations need to provide the value classes Unit, Boolean, Double, Float, Long, Int, Char, Short, and Byte (but are free to provide others as well).

因此,对 AnyVal 的测试是有意义的,超出了所需值类的枚举.

Therefore a test for AnyVal is meaningful, over and above an enumeration of the required value classes.

也就是说,您必须接受@drexin 的回答,因为如果您没有将值类用于扩展方法,那么您就不是真的活着.(从某种意义上说,活出它.)

That said, you must accept @drexin's answer because if you're not using value classes for extension methods, then you're not really living. (In the sense of, living it up.)

SIP 的动机:

... Scala 中的类可以完全内联,因此操作与外部方法相比,这些类的开销为零.一些内联类的用例是:

...classes in Scala that can get completely inlined, so operations on these classes have zero overhead compared to external methods. Some use cases for inlined classes are:

  1. 内联隐式包装器.这些包装器上的方法是转换为扩展方法.
  2. 新的数字类,例如无符号整数.不会再有需要成为此类类的拳击开销.所以这类似于.NET 中的值类.
  3. 表示度量单位的类.再次,没有拳击开销这些课程会产生费用.

您可以将扩展方法本身标记为 @inline 并且所有内容都是内联的:没有对象包装器并且您的小方法是内联的.

You can mark the extension method itself as @inline and everything is inlined: no object wrapper and your little method is inlined.

我每天都使用此功能.昨天我遇到了一个错误.该错误已经修复.也就是说,这是一个很酷的功能,Scala 的人会从 Coursera 抽出时间来修复它的一个小错误.

I use this feature every day. Yesterday I hit a bug in it. The bug is already fixed. What that says is that it's such a cool feature, the Scala folks will take time out from Coursera to quash a little bug in it.

这让我想起了,我忘了问,这不是 Coursera 测验问题,是吗?

That reminds me, I forgot to ask, this isn't a Coursera quiz question, is it?

这篇关于AnyVal 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆