LoginSignup
3
1

More than 5 years have passed since last update.

Warning: Orphan instanceを回避する

Posted at

Haskellの型クラスを実装する際、Orphan instanceという警告が出ることがある。以下が警告の例。

Foundation.hs:204:10: Warning:
    Orphan instance: instance PersistUserCredentials User
    To avoid this
        move the instance declaration to the module of the class or of the type, or
        wrap the type with a newtype and declare the instance on the new type.

この警告が出るのは、instanceを宣言しているモジュールが、型が宣言されているモジュールでも型クラスが宣言されているモジュールでもない場合(orphan=孤児)。

これを解消するにはinstance宣言を移動するか、あるいはnewtypeで新しい型を作り出して、その型に対してinstance宣言を追加する。

しかし、このどちらの解決策も取れないケースが、存在するかもしれない。

熟考の末、Orphan instance警告を無理に消すのが不適切だという結論が出た場合、以下のプラグマを書くことで抑制できる。

{-# OPTIONS_GHC -fno-warn-orphans #-}
3
1
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
3
1