为什么符号不是冻结字符串? [英] Why are symbols not frozen strings?

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

问题描述

我理解字符串和符号之间的理论差异.我知道符号是用来表示一个概念、一个名字、一个标识符、一个标签或一个键,而字符串是一袋字符.我知道字符串是可变的和瞬态的,而符号是不可变的和永久的.我什至喜欢我的文本编辑器中符号看起来与字符串的不同之处.

困扰我的是,实际上,符号与字符串非常相似,以至于它们没有实现作为字符串这一事实引起了很多头痛.与另一个著名的相同但不同"的组合 Float 和 Fixnum 不同,它们甚至不支持鸭子类型或隐式强制.

当然,最大的问题是从其他地方进入 Ruby 的哈希,比如 JSON 和 HTTP CGI,使用字符串键,而不是符号键,所以 Ruby 程序必须向后弯腰,要么预先转换,要么在查找时间.HashWithIndifferentAccess 的存在,以及它在 Rails 和其他框架中的猖獗使用,表明这里存在一个问题,一个需要抓挠的痒.

谁能告诉我为什么符号不应该被冻结字符串的实际原因?除了因为它一直都是这样做的"(历史)或因为符号不是字符串"(乞求问题).

考虑以下惊人的行为:

:apple == "apple" #=>假的,应该是真的:apple.hash == "apple".hash #=>假的,应该是真的{苹果:10}[苹果"] #=>零,应该是 10{苹果"=>10}[:apples] #=>零,应该是 10:apple.object_id == "apple".object_id #=>假的,但实际上很好

让下一代 Rubyists 不那么困惑的唯一方法是:

类符号<细绳def 初始化 *args极好的自我冻结结尾

(以及许多其他库级别的黑客攻击,但仍然不太复杂)

另见:

更新:我认为 Matz 为 class Symbol < 提供了理由.字符串在这里很好:http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/9192(感谢 Azolo 对此进行挖掘,以及 Matz 最终撤回).

解决方案

这个答案与我的原始答案截然不同,但我遇到了几个有趣 线程 Ruby 邮件列表.(两个都不错)

因此,在 2006 年的某个时候,matz 将 Symbol 类实现为 Symbol <;字符串.然后 Symbol 类被剥离以消除任何可变性.所以 Symbol 实际上是一个不可变的 String.

然而,它被还原了.给出的原因是<块引用>

尽管它非常反对 DuckTyping,但人们还是倾向于使用 case在类上,并且 Symbol <字符串经常会导致严重的问题.

所以你的问题的答案仍然是:Symbol 就像一个 String,但它不是.
问题不在于 Symbol 不应该是 String,而是历史上不是.

I understand the theoretical difference between Strings and Symbols. I understand that Symbols are meant to represent a concept or a name or an identifier or a label or a key, and Strings are a bag of characters. I understand that Strings are mutable and transient, where Symbols are immutable and permanent. I even like how Symbols look different from Strings in my text editor.

What bothers me is that practically speaking, Symbols are so similar to Strings that the fact that they're not implemented as Strings causes a lot of headaches. They don't even support duck-typing or implicit coercion, unlike the other famous "the same but different" couple, Float and Fixnum.

The biggest problem, of course, is that hashes coming into Ruby from other places, like JSON and HTTP CGI, use string keys, not symbol keys, so Ruby programs have to bend over backwards to either convert these up front or at lookup time. The mere existence of HashWithIndifferentAccess, and its rampant use in Rails and other frameworks, demonstrates that there's a problem here, an itch that needs to be scratched.

Can anyone tell me a practical reason why Symbols should not be frozen Strings? Other than "because that's how it's always been done" (historical) or "because symbols are not strings" (begging the question).

Consider the following astonishing behavior:

:apple == "apple"  #=> false, should be true

:apple.hash == "apple".hash #=> false, should be true

{apples: 10}["apples"]  #=> nil, should be 10

{"apples" => 10}[:apples]  #=> nil, should be 10

:apple.object_id == "apple".object_id #=> false, but that's actually fine

All it would take to make the next generation of Rubyists less confused is this:

class Symbol < String
  def initialize *args
    super
    self.freeze
  end

(and a lot of other library-level hacking, but still, not too complicated)

See also:

Update: I think Matz makes the case for class Symbol < String very well here: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/9192 (thanks to Azolo for digging this up, and also Matz' eventual retraction).

解决方案

This answer drastically different from my original answer, but I ran into a couple interesting threads on the Ruby mailing list. (Both good reads)

So, at one point in 2006, matz implemented the Symbol class as Symbol < String. Then the Symbol class was stripped down to remove any mutability. So a Symbol was in fact a immutable String.

However, it was reverted. The reason given was

Even though it is highly against DuckTyping, people tend to use case on classes, and Symbol < String often cause serious problems.

So the answer to your question is still: a Symbol is like a String, but it isn't.
The problem isn't that a Symbol shouldn't be String, but instead that it historically wasn't.

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

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