Program > JavaScript > AltJS > Elm > Elmガイド > 型
型 †
- 型 · An Introduction to Elm https://guide.elm-lang.jp/types/
Elm の主な利点の 1 つは、ランタイムエラーが実際に起きないことです。
Elm コンパイラが非常に素早くソースコードを分析して、値がプログラム中でどのように使われているか解析できるためです。
値を間違った方法で使用すると、コンパイラがフレンドリーなエラーメッセージで警告してくれます。
これは 型推論 と呼ばれています。
コンパイラは、全ての関数の引数と返り値の型を解析します。
JavaScriptの代わりにElmを使うメリットの一つは、コンパイラーの型推論の機能が使えることですね!
HaskellなどのML系言語に慣れている人にとっては、型推論の恩恵が得られることは便利なのではないかと思います。
1 2 3 4 5 |
|
「first」と書くべきところを間違えて「fist」と書いていた場合、Elmコンパイラーはエラーを検出して警告してくれるんですね!(すごい!)
-- TYPE MISMATCH ----------------------------------------------- Jump To Problem The 1st argument to `toFullName` is not what I expect: 5| toFullName { fistName = "Hermann", lastName = "Hesse" } ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This argument is a record of type: { fistName : String, lastName : String } But `toFullName` needs the 1st argument to be: { a | firstName : String, lastName : String } Hint: Seems like a record field typo. Maybe firstName should be fistName? Hint: Can more type annotations be added? Type annotations always help me give more specific messages, and I think they could help a lot in this case!