LoginSignup
3
0

More than 3 years have passed since last update.

はじめに

  • Elixir楽しんでいますか:bangbang::bangbang::bangbang:
  • <>/2のおもしろい使い方をご紹介します
    • いつかそのうち使う機会があるだろうと個人的におもっています
  • 2021/02/15(月)に開催したautoracex #10というElixirの純粋なもくもく会での成果です

"foo" <> x = "foobar"

iex> "foo" <> x = "foobar"
"foobar"

iex> x
"bar"
iex> "foo" <> "bar" = "foobar"
"foobar"

iex> "f" <> "o" <> "o" <> "bar" = "foobar"
"foobar
  • あまり意味は無いかもしれませんが:point_up::point_up_tone1::point_up_tone2::point_up_tone3::point_up_tone4::point_up_tone5:こういう使いかたもできます
  • ちなみに<>/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 :lgtm::lgtm::lgtm::lgtm::lgtm:

3
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0