12
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ElixirAdvent Calendar 2024

Day 12

【再戦】19時間前にリリースされたElixir 1.18に搭載の「型推論」が上手くいった(御大José Valimとのやり取りも)

Last updated at Posted at 2024-12-12

この記事は、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 です、ご覧いただいてありがとございます :bow:

This is piacere, thank you for visiting

image.png

前回、上手く動かなかった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!

:xmas-tree: There are 13 Elixir Advent Calendar, making for a hot winter! :xmas-tree:

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! :bow:

https://qiita.com/advent-calendar/2024/elixir
https://qiita.com/advent-calendar/2024/ranking/feedbacks/categories/programming_languages
image.png

再度、「型推論」を試してみる

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).

image.png

このように、Elixir 1.18からは、モジュール定義時に型推論に基づく型チェックが、型定義をコードに一切書いていないのに可能となります

動的型付言語であったElixirが、静的型付までカバーするようになった訳です … これはワクワクしますねー :laughing:

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! :laughing:

p.s.このコラムが、面白かったり、役に立ったら…

image.png にて、どうぞ応援よろしくお願いします :bow:


明日は、 私で 「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

12
1
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
12
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?