简体   繁体   English

elixir函数签名中的“<>”运算符是什么?

[英]What is the “<>” operator in an elixir function signature?

I see this function signature sometimes and can't find anything on it: 我有时看到这个函数签名,但在其上找不到任何东西:

def handle("user:" <> id), do: Repo.get(User, id)

I am new to erlang/elixir so I assume this is some sort of pattern matching for user:1 and user:2 , but I haven't seen any articles or information about this specifically in the Elixir guides. 我是erlang / elixir的新手,所以我认为这是user:1一种模式匹配user:1user:2 ,但我没有在Elixir指南中看到任何关于此的文章或信息。

What exactly is going on here? 到底发生了什么?

Yes, it's a pattern in this case. 是的,在这种情况下,这是一种模式。 "user:" <> id will match any binary starting with user: , and assign the part of the string after user: to id . "user:" <> id将匹配以user:开头的任何二进制文件,并在user:之后将字符串的一部分分配给id

iex(1)> "user:" <> id = "user:"
"user:"
iex(2)> id
""
iex(3)> "user:" <> id = "user:123"
"user:123"
iex(4)> id
"123"
iex(5)> "user:" <> id = "user"
** (MatchError) no match of right hand side value: "user"

Note that the same operator is also used to concatenate binaries when used as an expression: 请注意,当用作表达式时,同一运算符也用于连接二进制文件:

iex(1)> id = "123"
"123"
iex(2)> "user:" <> id
"user:123"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM