How to create custom infix operators

Ok first thing first, I am fairly new to elm.

Looking at the elm/core source code (elm/core) I noticed ( :: ) infix operator and I thought it is cool to have such a thing in my code as well, I went out to look for some documentation explaining this feature but all I have found was some blog post from people complaining why this feature is removed from elm version 0.19.

now I am wondering if this feature is removed from elm how is it possible that some libraries like elm/core and elm/url are able to use them, is there something that I am missing.

2 Likes

Libraries in elm namespace have some special powers like creating custom infix operators, but that is not possible generally.

2 Likes

Just a little more on this. Elm is frequently criticised for a perceived kind of closed development model. Basically library authors are mostly not allowed to do special things such as create infix operators, but a few specially anointed libraries/authors are afforded these “powers”. Most such arguments essentially boil down to “we’re all adults, let us do whatever we think is correct and the good stuff will rise to the top and be popular whilst the bad stuff will be allowed to sink to the bottom and eventually become unused/abandoned”. A kind of reddit style for libraries. I think this kind of argument sounds pretty compelling to many developers.

There are many arguments against this (ie. in favour of Elm’s style), however, the one I find most compelling is that this is not really a counter-factual situation, there are many languages that take the opposite approach from Elm. If you want to experience the results of that you can. One obvious alternative is Purescript, in that it’s quite a similar language, has similar goals, but has the development style of “everything goes let’s see what becomes popular”. It is also much less conservative when it comes to including language features than is Elm. Another thing you can do is go look at the Python eco-system. One would be hard-pressed to suggest that Python has not been successful with this model, however, the package eco-system does have problems. This is easily searchable.

So in other words, do not try to look at the Elm style as right/wrong, see it as alternative. Looked at this way, arguments for/against are kind of beside the point. See Elm’s style as experimental.

7 Likes

I personally advocate advocate for the opposite model, where library authors are not granted these special powers, and core libraries are also not granted these powers. One of elm’s language philosophies is not to introduce redundant ways to do things, and I think that following that principle more strictly in the core libraries would be valuable too.

elm and elm-explorations packages have special privileges: kernel code, infix operators, and effect modules.

@evancz’s explanation of why defining infix ops became reserved for these blessed packages:

In case you want to learn about infix operators themselves (not the restriction them)

they can use these characters: +-/*=.<>:&|^?%! and they can’t be one of: . | -> = :

they are called “binop” in the compiler source code

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.