DEV Community

gumi TECH for gumi TECH Blog

Posted on

6 1

Elixir: Elixir 1.9に備わった新機能

2019年4月29日付でPlataformatec blogに公開された「What’s new in Elixir – Apr/19」で、v1.9.0-devに加えられた新機能が紹介されました。本稿は、その記事に公式ドキュメントなどの情報を補ってご説明します。

mix releaseの追加

新たにmix releaseが備わりました。基本的なリリースに加え、ランタイム設定、テンプレートファイルによるカスタマイズ、カスタムステップなどがサポートされます。

mix releaseは現行のプロジェクトの自己完結したリリースを組み立てます。

MIX_ENV=prod mix release
MIX_ENV=prod mix release NAME
Enter fullscreen mode Exit fullscreen mode

リリースがつくられると、ターゲットに向けてパッケージ化し、デプロイできます。ターゲットは、同じオペレーティングシステム(OS)のディストリビューションおよびバージョンで、mix releaseコマンドが実行されたマシンで動かしておかなければなりません。

リリースは、mix.exsファイルのdef projectにおいて:releasesキーにより設定できます。

def project do
  [
    releases: [
      demo: [
        include_executables_for: [:unix],
        applications: [runtime_tools: :permanent]
      ],

      ...
    ]
  ]
end
Enter fullscreen mode Exit fullscreen mode

mix newで設定ファイルをつくらない

まず、mix newconfig/config.exsをつくらなくなりました。設定ファイルに依存することは、ライブラリやその作者にとって望ましくないとされてきたからです(「Avoid application configuration」)。つぎに、mix new --umbrellaは、子アプリケーションごとの設定はつくりません。すべての設定は、アンブレラプロジェクトのルートで宣言することになります。理由については「No longer generate config for mix new」をご参照ください。

Elixirに新たなConfigモジュールを追加

リリースは独自の設定がされるかもしれません。また、ビルドツールのMixに依存するのは望ましくないでしょう。そこで、use Mix.Configはやわらかな非推奨(soft-deprecated)となります(やわらかな非推奨については「Elixir: バージョン間の互換性と非推奨」参照)。これからは、Elixirに新たに備わったConfigモジュールimport Configとしてお使いください。

つぎのふたつの部分で互換性が破られます(「Add config providers」参照)。

  • 設定モジュールの追加
    • Config
    • Config.Reader
    • Config.Provider
  • リリースにConfig.Providerのサポートを追加
use Mix.Config import Config
Mix.Config.merge/2 Config.Reader.merge/2
Mix.Config.read!/2 Config.Reader.read!/2
Mix.Config.eval!/2 Config.Reader.read_imports!/2
Mix.Config.persist/1 Application.put_all_env/2

ロガーのパフォーマンス向上

ロガーは、メッセージが破棄されるときの処理を効率化しました。また、Erlang/OTP v21.2以降により、全体のパフォーマンスが向上しています。

その他の新機能

~Uシギルの追加

UTC DateTimeをつくる~Uが加わりました(「Add ~U sigil for UTC date times」参照)。なお、~uはありません。

新たな関数

設定ファイルを簡単にできるように、つぎの3つの関数が新たに加わりました(「Add support for a default value in System.get_env/1」参照)。

細かな変更

細かな変更については、「Changelog for Elixir v1.9」をご参照ください。

Neon image

Serverless Postgres in 300ms (❗️)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free →

Top comments (0)

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay