Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type constraints in Elm

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)
like image 950
Tomas Kulich Avatar asked Jun 29 '16 12:06

Tomas Kulich


People also ask

What are constrained type variables in Elm?

Type variables a and b are used by convention in many places, but some type annotations benefit from more specific names. There is a special variant of type variables in Elm called constrained type variables. The most common example is the number type. The negate function uses it:

Are there any runtime errors in Elm?

As a result, it’s extremely rare to have runtime errors in Elm. At this point you may be wondering where exactly in our code do type and data constructors get used. Type constructors are mainly used either in a type declaration or a type annotation, whereas data constructors are used inside a function body or when we define a top-level constant.

What is a recursive list in Elm?

In the How List Works Behind the Scenes section, we learned that List in Elm is defined as a recursive type. At the time, we didn’t know enough about types to fully grasp the idea of recursive types. Now that we know what types are, we’re better positioned to understand what they are. Let’s say we have a list of numbers: [ 16, 5, 31, 9 ].

What is the most important feature in Elm?

Note: Custom types are the most important feature in Elm. They have a lot of depth, especially once you get in the habit of trying to model scenarios more precisely. I tried to share some of this depth in Types as Sets and Types as Bits in the appendix.


1 Answers

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.

like image 177
Fred Yankowski Avatar answered Oct 14 '22 02:10

Fred Yankowski