9

According to What does comparable mean in Elm? comparable is built-in type constraint that can be used to limit type variables to those built-in types that are, well, comparable. The following questions comes to mind (and are not so easy to find out):

  • what are the other type constraints that currently exists in Elm? Is there a list somewhere?
  • Is there a way how I can create my own type constraints? I presume that the answer is no, since Elm does not have any mechanism such as Haskell Typeclasses right now?
  • If the above is true (no custom type constraints), that does mean that the polymorphism is quite limited in Elm, doesn't it? For example, it seems that the following is not achievable right now: I want to create a function f that accepts List of elements (of a same but uncertain type) each of which can be used as an argument to some function g (i.e. f accepts List a and moreover inst being of type a implies that g(inst) makes sense)
Community
  • 1
  • 1
Tomas Kulich
  • 14,388
  • 4
  • 30
  • 35
  • 1
    Have you seen [these](https://github.com/elm-lang/elm-compiler/issues/38) [two](https://github.com/elm-lang/elm-compiler/issues/1039) issues regarding type classes? – pdexter Jun 29 '16 at 13:04
  • 1
    Yes, I saw both these. OTOH, I saw there is a concept such as `comparable`, so I wasn't sure, what's the status of this in Elm. – Tomas Kulich Jun 30 '16 at 06:17

2 Answers2

10

Besides comparable (ints, floats, chars, strings, lists, and tuples) there are also appendable (strings, text, and lists) and number (ints and floats). I have not seen an authoritative list (outside of the compiler source).

There is no way to define similar typeclasses of your own.

Yes, this limits what functions you can write. No one has convinced Evan that this limit is a problem.

Fred Yankowski
  • 684
  • 1
  • 8
  • 12
  • 2
    Thank you, that's pretty clear now. Also, looking at all the stuff that Haskell developers have built to make typeclasses powerful enough (and still, many rant about it!), I understand that Evan is reluctant about this :) – Tomas Kulich Jun 30 '16 at 06:21
1

From Elm's official guide:

The full list of constrained type variables is:

  • number permits Int and Float
  • appendable permits String and List a
  • comparable permits Int, Float, Char, String, and lists/tuples of comparable values
  • compappend permits String and List comparable

Elm's Guide > Reading Types > Constrained Type Variables

Stefano
  • 21
  • 1
  • 5