LoginSignup
33
11

More than 5 years have passed since last update.

ElmでHelloWorld

Posted at

@jinjor さんが書いたElm本がついに発売されました!うきうきしています。
https://www.amazon.co.jp/dp/B07P253FPG/ref=cm_sw_r_tw_awdb_c_x_9TBDCbY7VKWY7

ガイドにはなかったような気がするHelloWorldを見て、コンパイラ母さんはどこまで親切に教えてくれるのか気になり始めて試しました。

意味のない文字列だけのソース

adgjpmtwp
make結果
$ elm make src/hello.elm                 Detected errors in 1 module.
-- PARSE ERROR --------------------------------------------------- src/hello.elm

Something went wrong while parsing your code.

2|
   ^
I was expecting to see more indentation? I was not done with that last thing
yet.

「パースできなかったんだけどぉ。これインデント?」(適当翻訳)

 mainだけは書いてあるソース

main = 1
make結果
$ elm make src/hello.elm
Detected errors in 1 module.
-- BAD MAIN TYPE ------------------------------------------------- src/hello.elm

I cannot handle this type of `main` value:

4| main =
   ^^^^
The type of `main` value I am seeing is:

    number

I only know how to handle Html, Svg, and Programs though. Modify `main` to beone of those types of values!

「俺はこんな型のmainはしらねー。俺にはnumber型に見えるんだけど。俺が知ってるのはHtml、Svg、Programs型だけだ。直して!」(適当翻訳)

じゃあHtml型を代入してみるか...

ということで、リファレンスのhtmlを参照。
https://package.elm-lang.org/packages/elm/html/latest/Html

本に出てきたtext関数が載っています。
文字列を引数にとってHtmlを返す。これだ。

importつけないとHtmlを認識してくれないのでつけた
import Html
main = Html.text "Hell World!"
実行結果(ブラウザにて確認)
Hell World!

というわけで...

mainさえ書いてあればなんとか動かすところまで行ける!親切!ありがとう!

33
11
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
33
11