User Tools

Site Tools


codesnippets:typesynonyminstances

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
codesnippets:typesynonyminstances [2021/04/12 19:14] โ€“ created f2b216codesnippets:typesynonyminstances [2025/10/08 00:48] (current) โ€“ external edit 127.0.0.1
Line 1: Line 1:
 ====== {-# LANGUAGE TypeSynonymInstances #-} ====== ====== {-# LANGUAGE TypeSynonymInstances #-} ======
  
 +  * allows to use type synonyms as instances
 +  * example:
 +    * code:<code Haskell>
 +main :: IO ()
 +main = print x1
 +
 +class MyClass x where
 +    fMy :: x -> x -> x
 +
 +type Speed = Double
 +
 +instance MyClass Speed where
 +    fMy xA xB = xA + xB
 +
 +x1 :: Speed
 +x1 = fMy 1.3 2.5
 +</code>
 +    * compiler error:<code>
 +app\Main.hs:9:10: error:
 +    * Illegal instance declaration for `MyClass Speed'
 +        (All instance types must be of the form (T t1 ... tn)
 +         where T is not a synonym.
 +         Use TypeSynonymInstances if you want to disable this.)
 +    * In the instance declaration for `MyClass Speed'
 +  |
 +9 | instance MyClass Speed where
 +  |          ^^^^^^^^^^^^^
 +</code>
 +  * example, with GHC option:
 +    * code:<code Haskell>
 +{-# LANGUAGE TypeSynonymInstances #-}
 +
 +main :: IO ()
 +main = print x1
 +
 +class MyClass x where
 +    fMy :: x -> x -> x
 +
 +type Speed = Double
 +
 +instance MyClass Speed where
 +    fMy xA xB = xA + xB
 +
 +x1 :: Speed
 +x1 = fMy 1.3 2.5
 +</code>
 +    * compiles, error and warning free, with compiler: GHC 8.10.4, using compiler option -Wall
 +    * executes, with output:<code>
 +3.8
 +</code>
 +
 +
 +===== โœŽ =====
 +~~DISCUSSION~~
codesnippets/typesynonyminstances.1618247649.txt.gz ยท Last modified: (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki