Open3

Gleamを触ってみる

Yoshi YamaguchiYoshi Yamaguchi

Erlang VM(BEAM)言語としてはElixirがメジャーだけれども、Gleamに興味が前からあったのでちょっとだけ触ってみる。

公式ウェブサイトはここ。

まずはGetting startedをするためにドキュメントを参照。

macOS SonomaでHomebrewを入れているので、そこからインストール。

$ brew update
$ brew install gleam
$ gleam --version
gleam 0.31.0

簡単に入った。Erlangは前に入れてからずっと更新していたはずだけど、念のため入っているか確認。

$ erl -version
Erlang (SMP,ASYNC_THREADS) (BEAM) emulator version 14.0.2

またrebar3はHomebrewでgleamを入れるときに依存関係で一緒に入っていたはずだけど、念のため確認。

$ rebar3 version
rebar 3.18.0 on Erlang/OTP 26 Erts 14.0.2
Yoshi YamaguchiYoshi Yamaguchi

次はエディタ周りの設定。VS Codeを使っているので、拡張を探す。

簡単にインストールできました。

Yoshi YamaguchiYoshi Yamaguchi

次はHello worldをしてみる。まず拡張子がわからないので、レポジトリを見てみる。gleamのGitHub orgを見て、標準ライブラリらしきもののレポジトリを確認してみる。

と思ってよく見たら言語ツアーのドキュメントではなく、プロジェクトの作成のドキュメントが下にあった。そちらを上に載せるべきでは...?と思いつつ見る。

gleam new というコマンドでskaffoldができるらしい。大変良い。

$ gleam new gleam-getting-started
error: Invalid project name

We were not able to create your project as `gleam-getting-started` does not
have the correct format. Project names must start with a lowercase letter
and may only contain lowercase letters, numbers and underscores.

Please try again with a different project name.

ハイフンは使えないらしい。学びがあった。

$ gleam new gleam_getting_started
error: Invalid project name

We were not able to create your project as `gleam_getting_started` has
the reserved prefix `gleam_`. This prefix is intended for official Gleam
packages only.

Please try again with a different project name.

gleam という接頭辞は使えないらしい。さすがに余計なお世話では?

$ gleam new getting_started_gleam
Your Gleam project getting_started_gleam has been successfully created.
The project can be compiled and tested by running these commands:

        cd getting_started_gleam
        gleam test

言われるがままにコマンドを打ってみる。

$ cd getting_started_gleam
$ gleam test
  Resolving versions
Downloading packages
 Downloaded 2 packages in 0.03s
  Compiling gleam_stdlib
  Compiling gleeunit
  Compiling getting_started_gleam
   Compiled in 1.15s
    Running getting_started_gleam_test.main
.
Finished in 0.006 seconds
1 tests, 0 failures

なにやらテストができた模様。あらためて、プロジェクト内の src/getting_started_gleam.gleam というコードを見てみる。

import gleam/io

pub fn main() {
  io.println("Hello from getting_started_gleam!")
}

どうやらhello worldのコードが勝手に gleam new で作られていた模様。これを実行するには gleam run で実行するらしい。プロジェクトルートで実行する。

$ gleam run
  Compiling gleam_stdlib
  Compiling gleeunit
  Compiling getting_started_gleam
   Compiled in 0.01s
    Running getting_started_gleam.main
Hello from getting_started_gleam!

ビルドをしてるので、 gleam run 以外の起動コマンドが生えるかなと思ったけども、特にそういうことはなさそう。

ソースをbeamにコンパイルして、起動時に関連パッケージを読み込んでから実行するためのラッパーを用意しているだけっぽい。gleam shelll でプロジェクトで必要なライブラリ(をbeamにしたもの)を読み込みつつErlangのシェルを起動できる。

$ gleam shell
  Compiling gleam_stdlib
  Compiling gleeunit
  Compiling getting_started_gleam
   Compiled in 0.01s
    Running Erlang shell
Erlang/OTP 26 [erts-14.0.2] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit] [dtrace]

Eshell V14.0.2 (press Ctrl+G to abort, type help(). for help)
1> getting_started_gleam:main().
Hello from getting_started_gleam!
nil
2>
BREAK: (a)bort (A)bort with dump (c)ontinue (p)roc info (i)nfo
       (l)oaded (v)ersion (k)ill (D)b-tables (d)istribution
a