【问题标题】:What does the hash sign do in erlang?哈希符号在 erlang 中有什么作用?
【发布时间】:2011-08-29 22:14:58
【问题描述】:

哈希符号在 erlang 中有什么作用?

record_to_string(#roster{us = {User, _Server},
         jid = JID,
         name = Name,
         subscription = Subscription,
         ask = Ask,
         askmessage = AskMessage}) ->
Username = ejabberd_odbc:escape(User).
....
.

【问题讨论】:

    标签: erlang


    【解决方案1】:

    它们与records 一起使用。

    【讨论】:

    【解决方案2】:

    只是为了完整性(以防有人用谷歌搜索“Erlang Hash”):

    井号也可用于定义integer with an arbitrary base,如

    16#deadbeef = 3735928559.
    

    【讨论】:

      【解决方案3】:

      它们与 Erlang 中的记录有关。事实上,在 Erlang 中创建、访问和更新记录等每个操作都是使用 #http://20bits.com/articles/erlang-an-introduction-to-records/ 完成的

      【讨论】:

        【解决方案4】:

        如果一条记录是这样定义的:

        -record(record_name, {first_field, second_field}).
        

        您可以使用哈希以多种方式访问​​记录,其中:

        % create a new record and put it in a variable
        Record = #record_name{first_field = 1, second_field = 2},
        
        % get only the second_field of Record
        Field = Record#record_name.second_field,
        
        % create a new record from Record, but with a different first_field
        Record2 = Record#record_name{first_field = 5}.
        

        【讨论】:

        • 我知道这是一个已回答的问题,但我把它放在这里以供将来参考,因为我前段时间需要这个并且只找到了链接,而不是独立的答案。链接很有用,但正如另一条评论指出的那样,不应构成完整的答案。
        【解决方案5】:

        正如先前的答案所指出的那样,它们不仅是记录语法的一部分和数字中的基本表示,从 Erlang R17 开始,它们也用于地图。 Map 是 R17 中引入的一种新的键值数据类型,它们表示为:#{ Key => Value, ... }

        我认为地图信息的最佳来源是this link。但是,在候选版本 1 中,似乎并未实现其中描述的所有功能。

        【讨论】:

          【解决方案6】:

          哈希符号用于处理 erlang 中的记录,如其他答案所述。这是一篇更详细地解释语法的文章。 http://www.techtraits.com/Programming/2011/06/11/records-in-erlang/

          【讨论】:

          • 欢迎来到 SO!虽然这在理论上确实回答了这个问题,但it would be preferable 在此处包含答案的基本部分,并提供链接仅供参考。
          猜你喜欢
          • 2014-09-13
          • 2012-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-23
          • 1970-01-01
          • 2015-11-23
          • 2017-01-27
          相关资源
          最近更新 更多