codesnippets:typesynonyminstances
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| codesnippets:typesynonyminstances [2021/04/12 19:14] โ created f2b216 | codesnippets: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:< | ||
| + | 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 | ||
| + | </ | ||
| + | * compiler error:< | ||
| + | app\Main.hs: | ||
| + | * 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 | ||
| + | | ^^^^^^^^^^^^^ | ||
| + | </ | ||
| + | * example, with GHC option: | ||
| + | * code:< | ||
| + | {-# 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 | ||
| + | </ | ||
| + | * compiles, error and warning free, with compiler: GHC 8.10.4, using compiler option -Wall | ||
| + | * executes, with output:< | ||
| + | 3.8 | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== โ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/typesynonyminstances.1618247649.txt.gz ยท Last modified: (external edit)
