do record_info和tuple_to_list在Erlang中返回相同的密钥顺序? [英] do record_info and tuple_to_list return the same key order in Erlang?

查看:374
本文介绍了do record_info和tuple_to_list在Erlang中返回相同的密钥顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即,如果我有记录

-record(one, {frag, left}).




  • record_info(fields,one)转到总是返回 [frag,
    left]

  • tl(tuple_to_list(#one {frag =Frag,left =Left}))
    总是 [Frag,Left]

    • Is record_info(fields, one) going to always return [frag, left]?
    • Is tl(tuple_to_list(#one{frag = "Frag", left = "Left"})) always gonna be ["Frag", "Left"]?
    • 这是一个实现详情?
      非常感谢!

      Is this an implementation detail? Thanks a lot!

      推荐答案

      简短的答案是:是的,在写作的时候它会奏效。最好的答案是:它可能不会在未来这样工作,问题的性质涉及我。

      The short answer is: yes, as of this writing it will work. The better answer is: it may not work that way in the future, and the nature of the question concerns me.

      使用 record_info是安全的/ 2 ,虽然依靠订单可能是有风险的,坦白说,我不能想到这样做有道理的情况,这意味着你正在以错误的方式解决问题。您可以分享更多关于您要完成的工作的详细信息,以便我们可以帮您选择更好的方法吗?可以这样简单的模式匹配是你需要的。

      It's safe to use record_info/2, although relying on the order may be risky and frankly I can't think of a situation where doing so makes sense which implies that you are solving a problem the wrong way. Can you share more details about what exactly you are trying to accomplish so we can help you choose a better method? It could be that simple pattern matching is all you need.

      对于 tuple_to_list / 1 的示例,我Cesarini和Thompson的Erlang Programming引用:

      As for the example with tuple_to_list/1, I'll quote from "Erlang Programming" by Cesarini and Thompson:


      ...无论你做什么,从来没有使用元组表示你的程序中的记录,如果你这样做,这本书的作者将会拒绝你,并拒绝任何参与帮助你学习Erlang。

      "... whatever you do, never, ever use the tuple representations of records in your programs. If you do, the authors of this book will disown you and deny any involvement in helping you learn Erlang."

      有几个很好的理由,其中包括:

      There are several good reasons why, including:


      1. 您的代码将变脆 - 如果您以后更改字段数或其顺序,您的代码将中断。

      2. 不保证内容的记录将继续以此方式在以后的erlang版本中运行。

      这篇关于do record_info和tuple_to_list在Erlang中返回相同的密钥顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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