====== {-# OPTIONS_GHC -fno-warn-orphans #-} ======
~~DISCUSSION~~
* prevents warnings of orphan instance, meaning:
* instance declaration of a type is not declared in the module of the class
* NOTE: Orphan instances may be useful.
* see also [[https://wiki.haskell.org/Orphan_instance|Haskell wiki - Orphan instance]]
* NOTE: The purpose of the examples here is only to demonstrate the impact on the compiler. The examples may not make sense as a use case of type classes.
* example that may make more sense: [[codesnippets:datastructuresfromsymboltrees|Creating data structures from symbol trees]]
* example:
* code of module Lib:
module Lib
(
MyClass(..)
) where
class Num x => MyClass x where
fMy :: x -> x
* code of module Main:
import Lib (MyClass(..))
main :: IO ()
main = print x1
instance MyClass Double where
fMy x = (x * x) + x + 1
x1 :: Double
x1 = fMy 3.4
* compiler warning, when using -Wall:
app\Main.hs:6:1: warning: [-Worphans]
Orphan instance: instance MyClass Double
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.
|
6 | instance MyClass Double where
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...
* example, with GHC option:
* code:
{-# OPTIONS_GHC -fno-warn-orphans #-}
import Lib (MyClass(..))
main :: IO ()
main = print x1
instance MyClass Double where
fMy x = (x * x) + x + 1
x1 :: Double
x1 = fMy 3.4
* compiles, error and warning free, with compiler: GHC 8.10.4, using compiler option -Wall
* executes, with output:
15.959999999999999
===== ✎ =====
~~DISCUSSION~~