Skip to content

Instantly share code, notes, and snippets.

@halcat0x15a
Created March 30, 2016 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halcat0x15a/0b451ff7c597646067d84c0d54a0bfc3 to your computer and use it in GitHub Desktop.
Save halcat0x15a/0b451ff7c597646067d84c0d54a0bfc3 to your computer and use it in GitHub Desktop.

一つの型に対して2つのインスタンスを提供する

  • 値のwrap/unwrapがない
  • インスタンスの定義が単純
  • インスタンスが一意に定まらない

コンパニオンオブジェクトの同一階層に複数のimplicit valueを定義した場合

  • importが必須
  • エラーがわかりづらい(could not find implicit value)

importによりコンテキストを切り替えることが本当に良いのか

newtypeを使ってインスタンスを提供する

  • インスタンスが一意に決まる
  • importが必要ない
  • 値のwrapが必要

newtypeの実装候補

  • wrapper class
  • tagged type

value classは抽象化が不可能である(インスタンス化されてしまう)ため採用できない

wrapper classを使う場合

  • インスタンスの定義が簡単
  • unwrapが必要
  • wrap/unwrapにコストがかかる

tagged typeを使う場合

  • wrapのコストがない
  • プリミティブ型のboxing/unboxingのコストはある
  • 黒魔術的

subtypingを使うtagged type

  • unwrapが不要

subtypingを使わないtagged type

  • unwrapが必要
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment