Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Last active August 29, 2015 14:14
Show Gist options
  • Save TheSeamau5/f8dc9909c8c3eca22cc7 to your computer and use it in GitHub Desktop.
Save TheSeamau5/f8dc9909c8c3eca22cc7 to your computer and use it in GitHub Desktop.
Opinion on how typeclasses would look in Elm
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