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

この記事は、[Elixir Advent Calendar 2024 シリーズ8](https://qiita.com/advent-calendar/2024/elixir) の12日目です

> This article is the 12th day of the Elixir Advent Calendar 2024 Series 8.

昨日は、私で [「19時間前にリリースされたElixir 1.18に搭載の「型推論」を試してみたが … 上手く動かせていない」](https://qiita.com/piacerex/items/2a1ee747a28733a24cbb) でした

> 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](https://twitter.com/piacere_ex) です、ご覧いただいてありがとございます :bow:
> This is piacere, thank you for visiting

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/155423/8b1cfb13-16c2-7a34-1a56-4a0288ab0be2.png)

[前回](https://qiita.com/piacerex/items/2a1ee747a28733a24cbb)、上手く動かなかった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!

https://x.com/zacky1972/status/1867080888531779927

https://x.com/josevalim/status/1867155058653069761

### :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](https://speakerdeck.com/piacerex/7nian-jian-sok-ke-totupurankaru-risitaelixirniyoukoso?slide=13) in the programming category of the Advent Calendar, [claiming the #1 spot last year](https://qiita.com/advent-calendar/2023/ranking/feedbacks/categories/programming_languages). 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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/155423/272a1a34-c2d9-6716-d88f-46076bdb9557.png)](https://qiita.com/advent-calendar/2024/ranking/feedbacks/categories/programming_languages)

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

[CHANGELOG](https://github.com/elixir-lang/elixir/blob/v1.18/CHANGELOG.md) の関数呼出を、モジュール定義で包むことで、型推論が効きます

> By wrapping the function call in the CHANGELOG with a module definition, type inference works.

```diff_elixir
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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/155423/46a9b048-83af-c10f-0165-da0cb247f755.png)

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

<font color="red">**動的型付言語であったElixirが、静的型付までカバーするようになった訳です … これはワクワクしますねー**</font> :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.

> <font color="red">**Elixir, which was a dynamically typed language, has now extended its coverage to include static typing… This is exciting!**</font> :laughing: 


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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/155423/907a8695-4d83-1b5c-8052-d0ce52de5f66.png) にて、どうぞ応援よろしくお願いします :bow:

---

明日は、 私で 「2024年のElixir④クラウドライブラリ編」 です

> Tomorrow, I will present "2024 Elixir ④Cloud Libraries Edition."

The overall structure of this series is as follows:

:::note warn
[①New Libraries/Frameworks of 2024 Edition](https://qiita.com/piacerex/items/17c5cab114f89f11ff68)
[②Updated Libraries/Frameworks of 2024 Edition](https://qiita.com/piacerex/items/78fc907c0b44cf3dda15)
[③Livebook + Plugins Edition of 2024](https://qiita.com/piacerex/items/9f2d465af90f76d08ade)
④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
:::