Scala符号的目的是什么? [英] Purpose of Scala's Symbol?

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

问题描述

可能重复:
在斯卡拉(Scala)?

Symbol的目的是什么,为什么它应具有某些特殊的 literal 语法e. G. 'FooSymbol?

解决方案

在您具有一组希望快速比较的封闭标识符的情况下使用符号.当您有两个String实例时,不能保证它们是interned [1],因此要比较它们,您必须经常通过比较长度甚至检查每个字符是否相同来检查它们的内容.对于Symbol实例,比较是一个简单的eq检查(即Java中的==),因此它们是固定时间(即O(1))进行查找.

这种结构倾向于在动态语言中使用(特别是Ruby和Lisp代码倾向于大量使用符号),因为在静态类型的语言中,通常希望按类型限制项目集. /p>

话虽如此,如果您有一个键/值存储,其中存储了一组受限制的键,而使用静态类型对象将变得笨拙,那么Map[Symbol, Data]样式的结构可能很适合你.

关于String在Java(因此也是Scala)上进行实习的说明:Java String在某些情况下还是可以实习的;特别是字符串文字会被自动插入,您可以在String实例上调用intern()方法以返回插入的副本.但是,并非所有的String都被阻止,这意味着运行时仍然必须执行完整检查,除非它们是同一实例. interning可以更快地比较两个相等的实习生字符串,但是并不能改善比较不同字符串的运行时间. Symbol可以通过保证进行实习而受益,因此在这种情况下,一次引用相等性检查就足以证明相等性或不平等性.

[1]实习是一个过程,通过该过程可以在创建对象时检查是否已存在相等对象,如果存在则使用该对象.这意味着如果您有两个相等的对象,则它们恰好是同一对象(即,它们引用相等).这样做的缺点是查找需要使用的对象可能会很昂贵,并且允许对象被垃圾回收可能需要复杂的实现.

Possible Duplicate:
What are some example use cases for symbol literals in Scala?

What's the purpose of Symbol and why does it deserve some special literal syntax e. g. 'FooSymbol?

解决方案

Symbols are used where you have a closed set of identifiers that you want to be able to compare quickly. When you have two String instances they are not guaranteed to be interned[1], so to compare them you must often check their contents by comparing lengths and even checking character-by-character whether they are the same. With Symbol instances, comparisons are a simple eq check (i.e. == in Java), so they are constant time (i.e. O(1)) to look up.

This sort of structure tends to be used more in dynamic languages (notably Ruby and Lisp code tends to make a lot of use of symbols) since in statically-typed languages one usually wants to restrict the set of items by type.

Having said that, if you have a key/value store where there are a restricted set of keys, where it is going to be unwieldy to use a static typed object, a Map[Symbol, Data]-style structure might well be good for you.

A note about String interning on Java (and hence Scala): Java Strings are interned in some cases anyway; in particular string literals are automatically interned, and you can call the intern() method on a String instance to return an interned copy. Not all Strings are interned, though, which means that the runtime still has to do the full check unless they are the same instance; interning makes comparing two equal interned strings faster, but does not improve the runtime of comparing different strings. Symbols benefit from being guaranteed to be interned, so in this case a single reference equality check is both sufficient to prove equality or inequality.

[1] Interning is a process whereby when you create an object, you check whether an equal one already exists, and use that one if it does. It means that if you have two objects which are equal, they are precisely the same object (i.e. they are reference equal). The downsides to this are that it can be costly to look up which object you need to be using, and allowing objects to be garbage collected can require complex implementation.

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

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