Erlang 中的伪输入是获取类型的方法吗?

发布于 2024-08-25 10:31:35 字数 268 浏览 3 评论 0原文

例如,要表示一个字符串,我可以使用:

{string,"hjggjhhggJ"}

,列表将是:

{list, [1,2,3]}

:我想我发现我遇到了我需要的情况类型,例如区分字符串和列表,我不知道如何继续。然而,我确实想在 Erlang 应用程序中的任何地方使用我选择的任何技术来保持一致性,而不仅仅是字符串和列表。有什么建议吗?

更新: 我使用此功能的一个示例是当我将数据值存储在 Riak 数据存储中时,它允许您存储列表或字符串。

For example, to denote a String I could use:

{string,"hjggjhhggJ"}

and a list would be:

{list, [1,2,3]}

: I guess I have found that I am running into situations where I need types, for example to distinguish between strings and lists and I am not sure how to proceed. I do however want to use whatever technique I choose everywhere in my Erlang application for consistency, and not just for strings and lists. Any advice?

Update:
An example of where I use this is when I store data values in the Riak datastore which lets you store either lists or strings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花开雨落又逢春i 2024-09-01 10:31:35

如果你需要这样区分——是的,你可以这样做。不过,动态类型的总体思想是不区分类型,除非绝对必要。 (不过,我发现这在多大程度上适用于像 erlang 这样的非 oop 语言是有争议的——我很想听听其他人对此主题的看法)

然而,有时,进行区分可能非常有用。在我的一个项目中,我有一个字符串,它会经历不同的转义阶段,具体取决于输入字符串应该发生的情况。输出未正确转义的字符串可能会带来安全风险。为了使这一点更安全,我将其设置为标记元组:

{my_string, false, false, ActualString}

当发生转义/处理的一个阶段时,我可以切换布尔值:

{my_string, true, false, ActualString}

然后接收字符串的输出函数可以匹配某些条件:

output_html_escaped_string({my_string, true, _, ActualString}) -> ...

这样该函数将如果我意外地向它传递了一个未处理的字符串,则会引发异常(我记得确实发生过一两次:)。

If you need to distinguish that way -- Yes, you can do that. Though, the general idea of dynamic typing is not to discriminate for types, unless absolutely necessary. (I find it debatable, though, how much this applies to a non-oop language like erlang -- I would love to hear what other people think about that topic)

Sometimes, however, it can be quite useful to have a distinction. In one of my projects, I had a string, which would go through different phases of escaping, depending on what was supposed to happen with the input string. Outputting a String that wasn't escaped properly, could pose a security risk. To make this safer, I made it a tagged tuple:

{my_string, false, false, ActualString}

And when one stage of escaping/processing has happened, I could switch the boolean:

{my_string, true, false, ActualString}

and then the output function that receives the string, can match for certain criteria:

output_html_escaped_string({my_string, true, _, ActualString}) -> ...

This way the function would raise an exception if I pass it an unprocessed string by accident (And I remember that did happen once or twice :).

邮友 2024-09-01 10:31:35

是的。这也用于区分同一类型的多个构造函数(并不是说 Erlang 中确实存在差异)。例如,标准库中的许多函数都指定返回 {error, Reason}{ok, Value}

Yes. This is also used for distinguishing multiple constructors of the same type (not that there is really a difference in Erlang). For example, many functions in the standard library are specified to return either {error, Reason} or {ok, Value}.

紫瑟鸿黎 2024-09-01 10:31:35

不确定这是否是您所需要的,但您可以使用保护表达式来区分类型。请提供更多关于您的观点的意见/示例,以便我可以提供更多帮助。

Not sure it's what you need but you can use guard expressions in order to distinct between types. Please provide more input/example on your point so I could perhaps help more.

挽梦忆笙歌 2024-09-01 10:31:35

一般来说,您可能不想做 {string, "ABC"} 可能更有用的是 {username, "ABC"} 或类似的东西,其中标签不是它的数据类型字符串或列表,但您的应用程序将如何使用该数据。这样你就可以对数据的含义进行模式匹配,而不是它是什么

你可以使用防护(尽管erlang中的字符串是字符列表)

in general you probably don't want to do {string, "ABC"} what is probably more useful would be something like {username, "ABC"} or the like where the tag is not what kind of data it is in terms of string or list, but how your application is going to use that data. That way you can pattern match on what the data means, not what it is

You can use guards (though a string in erlang is a list of chars)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文