この記事は、Elixir Advent Calendar 2024 シリーズ8 の12日目です
This article is the 12th day of the Elixir Advent Calendar 2024 Series 8.
昨日は、私で 「19時間前にリリースされたElixir 1.18に搭載の「型推論」を試してみたが … 上手く動かせていない」 でした
Yesterday, I wrote about "I tried the type inference feature in Elixir 1.18 released 19 hours ago, but I couldn’t get it to work properly."
piacere です、ご覧いただいてありがとございます
This is piacere, thank you for visiting
前回、上手く動かなかったElixir 1.18の「型推論」が上手く動いたので、その続報です
I’d like to share a follow-up on Elixir 1.18’s “type inference,” which wasn’t working properly before but is now functioning as expected.
そこに辿り着くまでの間、 @RyoWakabayashi さんの素晴らしいコラムと、 @zacky1972 さんによる御大José Valimとのやり取りがあり、ちょっとしたステキ体験でした
Getting to this point was a delightful journey, including the excellent column by @RyoWakabayashi and an inspiring exchange with the great José Valim, facilitated by @zacky1972. It was truly a wonderful experience!
There are 13 Elixir Advent Calendar, making for a hot winter!
Over the past 7 years, Elixir has consistently been a top-ranking language in the programming category of the Advent Calendar, claiming the #1 spot last year. This year, too, Elixir is sure to stay at the top.
We’re feeling the excitement again this year… Please support or subscribe!
https://qiita.com/advent-calendar/2024/elixir
https://qiita.com/advent-calendar/2024/ranking/feedbacks/categories/programming_languages
再度、「型推論」を試してみる
CHANGELOG の関数呼出を、モジュール定義で包むことで、型推論が効きます
By wrapping the function call in the CHANGELOG with a module definition, type inference works.
defmodule User do
defstruct [:age, :car_choice]
def drive(%User{age: age, car_choice: car}, car_choices) when age >= 18 do
if car in car_choices do
{:ok, car}
else
{:error, :no_choice}
end
end
def drive(%User{}, _car_choices) do
{:error, :not_allowed}
end
end
defmodule A do
def a() do
car_choices = []
User.drive({:ok, %User{}}, car_choices)
end
end
上記をiexに貼ると、CHANGELOG同様、型チェックが走りました(ちなみに、iexではチェック時に行番号しか出ませんが、mix PJ等で同様のことを行うと、該当コードも出ます)
When you paste the above into iex, type checking runs just like in the CHANGELOG (by the way, in iex, only the line number is displayed during the check, but when you do the same thing with mix PJ or similar, the corresponding code is also shown).
このように、Elixir 1.18からは、モジュール定義時に型推論に基づく型チェックが、型定義をコードに一切書いていないのに可能となります
動的型付言語であったElixirが、静的型付までカバーするようになった訳です … これはワクワクしますねー
With Elixir 1.18, type checking based on type inference is now possible during module definition, even without writing type definitions in the code.
Elixir, which was a dynamically typed language, has now extended its coverage to include static typing… This is exciting!
p.s.このコラムが、面白かったり、役に立ったら…
明日は、 私で 「2024年のElixir④クラウドライブラリ編」 です
Tomorrow, I will present "2024 Elixir ④Cloud Libraries Edition."
The overall structure of this series is as follows:
①New Libraries/Frameworks of 2024 Edition
②Updated Libraries/Frameworks of 2024 Edition
③Livebook + Plugins Edition of 2024
④Cloud Libraries Edition of 2024
⑤Web/Smartphone/Microservices Edition of 2024
⑥Predicting Elixir's 2025 from AI/LLM Trends of 2024
⑦Reviewing What Elixir Can Do by the End of 2024