Last active
August 29, 2015 14:14
-
-
Save TheSeamau5/f8dc9909c8c3eca22cc7 to your computer and use it in GitHub Desktop.
Opinion on how typeclasses would look in Elm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Numerical a = { | |
(+) : a -> a -> a, | |
(-) : a -> a -> a, | |
(*) : a -> a -> a, | |
negate : a -> a, | |
abs : a -> a | |
} | |
implementation Numerical Float = { | |
(+) = Native.Float.add, | |
(-) = Native.Float.sub, | |
(*) = Native.Float.mul, | |
negate = Native.Float.negate, | |
abs = Native.Float.abs | |
} | |
square : a -> a where a : Numerical | |
a : Numerical | |
square : a -> a | |
square numerical = | |
numerical * numerical | |
-- If a type only requires one interface, it can be inlined in the type annotation | |
square : Numerical -> Numerical | |
-- You can have types that implement multiple interfaces | |
a : Numerical, Comparable | |
compareSquares : a -> a -> Bool | |
compareSquares : a -> a -> Bool where a : Numerical, Comparable | |
a : Numerical, StringConvertible | |
b : Sequential, StringConvertible | |
concatNumberAndListAsString : a -> b -> String | |
concatNumberAndListAsString : a -> b -> String where a : Numerical, StringConvertible, b : Sequential, StringConvertible |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment