"foo" <> x = "foobar" (Elixir)
7132 ワード
はじめに
-
Elixir楽しんでいますか
-
<>/2のおもしろい使い方をご紹介します
- いつかそのうち使う機会があるだろうと個人的におもっています
- 2021/02/15(月)に開催したautoracex #10というElixirの純粋なもくもく会での成果です
"foo" <> x = "foobar"
iex> "foo" <> x = "foobar"
"foobar"
iex> x
"bar"
-
<>/2は上のような使い方ができます
- 上の例は公式のExamplesを拝借しました
- 私はElixir実践ガイドという本で知りました
iex> "foo" <> "bar" = "foobar"
"foobar"
iex> "f" <> "o" <> "o" <> "bar" = "foobar"
"foobar
- あまり意味は無いかもしれませんがこういう使いかたもできます
- ちなみに<>/2の左側は、literal binaryである必要があります
- 要は次のように左側に変数をもってきてマッチさせようとしても動かなくて、CompileErrorとなってしまいます
iex> x <> "bar" = "foobar"
** (ArgumentError) the left argument of <> operator inside a match should always be a literal binary because its size can't be verified. Got: x
(elixir 1.11.3) lib/kernel.ex:1857: Kernel.invalid_concat_left_argument_error/1
(elixir 1.11.3) lib/kernel.ex:1829: Kernel.wrap_concatenation/3
(elixir 1.11.3) lib/kernel.ex:1808: Kernel.extract_concatenations/2
(elixir 1.11.3) expanding macro: Kernel.<>/2
iex:10: (file)
Wrapping Up
-
Pattern matching、奥が深いですね
- Enjoy Elixir
- いつかそのうち使う機会があるだろうと個人的におもっています
iex> "foo" <> x = "foobar"
"foobar"
iex> x
"bar"
-
<>/2は上のような使い方ができます
- 上の例は公式のExamplesを拝借しました
- 私はElixir実践ガイドという本で知りました
iex> "foo" <> "bar" = "foobar"
"foobar"
iex> "f" <> "o" <> "o" <> "bar" = "foobar"
"foobar
- あまり意味は無いかもしれませんがこういう使いかたもできます
- ちなみに<>/2の左側は、literal binaryである必要があります
- 要は次のように左側に変数をもってきてマッチさせようとしても動かなくて、CompileErrorとなってしまいます
iex> x <> "bar" = "foobar"
** (ArgumentError) the left argument of <> operator inside a match should always be a literal binary because its size can't be verified. Got: x
(elixir 1.11.3) lib/kernel.ex:1857: Kernel.invalid_concat_left_argument_error/1
(elixir 1.11.3) lib/kernel.ex:1829: Kernel.wrap_concatenation/3
(elixir 1.11.3) lib/kernel.ex:1808: Kernel.extract_concatenations/2
(elixir 1.11.3) expanding macro: Kernel.<>/2
iex:10: (file)
Wrapping Up
-
Pattern matching、奥が深いですね
- Enjoy Elixir
Author And Source
この問題について("foo" <> x = "foobar" (Elixir)), 我々は、より多くの情報をここで見つけました https://qiita.com/torifukukaiou/items/d9ff24e4518cd89f15e1著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .