LoginSignup
5
5

More than 5 years have passed since last update.

Elixir1.3でwithスペシャルフォームに追加されそうな機能

Last updated at Posted at 2016-03-22

Elixir1.3でwithスペシャルフォームに追加されそうな機能

@hayabusa333 さんがElixir MeetUp #2 in Drecom にて発表されたスライドの中でwithの拡張が気になったので調べました。(Slideshareのコメント欄汚してすみません…あそこ書き方が分かんないです)

withにelse句が追加されそう

https://github.com/elixir-lang/elixir/issues/4085
caseっぽくelseが書けます。

iex(76)> v=1; with  1 <- v, do: v, else: (2 -> 20; 3 -> 300; _ -> "?")
1
iex(77)> v=2; with  1 <- v, do: v, else: (2 -> 20; 3 -> 300; _ -> "?")
20
iex(78)> v=3; with  1 <- v, do: v, else: (2 -> 20; 3 -> 300; _ -> "?")
300
iex(79)> v=4; with  1 <- v, do: v, else: (2 -> 20; 3 -> 300; _ -> "?")
"?"

withにガードが追加されそう

https://github.com/elixir-lang/elixir/issues/4015
forへのガード追加に伴い、一貫性の為にという感じの機能追加です。あまり意味はなさげ。

iex(75)> v = 1; with (x when is_integer(x) <- v), do: x, else: (_ -> :error)
1
iex(76)> v = "!"; with (x when is_integer(x) <- v), do: x, else: (_ -> :error)
:error

余談ですが、現時点で唯一のElixir和書と名高いアランビックの錬金術士買いました。薄い(薄くない)本購入は初体験です。

5
5
0

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
5
5