Introduce Sum Types

136 views
Skip to first unread message

maen...@joshmartin.ch

unread,
Feb 16, 2018, 5:33:21 AM2/16/18
to elixir-lang-core
Hi all,

First I want to thank you all for your great work on the elixir language!

Have there been some thoughts to introduce Tagged Unions into elixir?
I would really love to use sum types like Either or simple enums in my daily work and have some support of the language in handling them.

What do you think about this?

Cheers,
Jony

Louis Pilfold

unread,
Feb 16, 2018, 5:57:00 AM2/16/18
to elixir-l...@googlegroups.com
Hey Jony

Would this involve some form of compile time type checking for these values? If not we already have them in the form of tuples.

@type result :: {:ok, string()} | {:error, string()}

If you want compile time checks this would be more difficult. I've experimented a little with compile time checks with macros, check it out here -> https://github.com/lpil/sum

Cheers,
Louis

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/4052d8d9-6eda-46c9-b259-fb0f2e041120%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

maen...@joshmartin.ch

unread,
Feb 16, 2018, 6:38:07 AM2/16/18
to elixir-lang-core
Hey Louis,

I think it should include compile time checks. Mostly I would expect those for function headers, case blocks and with blocks.

Dialyzer already checks many of those conditions, but I would like to go a step further.
Your mentioned library looks really interesting. Making it work together with existing type specifications would be really cool.

With for example structs, I can achieve safety really easy by adding %Struct{} = my_input to the function header.

I would like to have a mechanism that works similarly, but additionally checks uncovered types of a sum. (If you would for example not cover the None of a Maybe)

I think that this could tremendously improve the code quality.

Cheers,
Jony

Ben Wilson

unread,
Feb 16, 2018, 8:35:03 AM2/16/18
to elixir-lang-core
Can you elaborate on the safety provided by doing `%Struct{} = ` on a function? There are no compile time checks that calls to that function are actually of that struct type, just runtime checks on the data. Put another way this isn't really a type check, but an assertion.

OvermindDL1

unread,
Feb 23, 2018, 1:09:29 PM2/23/18
to elixir-lang-core
There are some library that add sum types to Elixir that do 'some' compile-time checks (like case's and so forth).  It would be useful built in to elixir (though not any of the current library implementations) but the libraries already do support much of it.

Louis Pilfold

unread,
Feb 24, 2018, 8:46:05 AM2/24/18
to elixir-lang-core

Hi

I started to implement such a library, but I would much prefer to use an existing one. I was unable to find any, wild you be able to share some links?

Thanks,
Louis


OvermindDL1

unread,
Feb 25, 2018, 11:00:34 AM2/25/18
to elixir-l...@googlegroups.com
There are at least 2 others that I know of, and I can find those for you too tomorrow if you want them, they have different features and focuses, but the one that immediately comes to mind is:  https://github.com/expede/algae

Expede on GitHub has a lot of cool and useful little 'core' elixir libraries in my opinion.

To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CABu8xFC0kWY2ncPW0tm5MbyMj8WFa%2BD2v6bBamdB-1fyk7qu5A%40mail.gmail.com.

Louis Pilfold

unread,
Feb 25, 2018, 11:58:44 AM2/25/18
to elixir-l...@googlegroups.com

If I recall correctly this library wraps the values and does not offer compile time checks, which is the inverse of what I'm after.

I want unwrapped values and compile time checks :)

Cheers,
Louis


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

OvermindDL1

unread,
Feb 26, 2018, 5:02:41 PM2/26/18
to elixir-lang-core
So something more like https://github.com/lpil/sum or so (undocumented, but it looks right, it's the first thing I found via a github search).

There is one specific one I'm thinking of but can't for the life of me remember it's name...  I wish hex.pm had the ability to search module and function names in a libraries public API...  >.>

Either way, it's not hard to write a library to support it though.  :-)

Louis Pilfold

unread,
Feb 26, 2018, 5:52:32 PM2/26/18
to elixir-lang-core

That would be the library I started writing that I was looking for an alternative to finishing :D

Guess I should finish it!


OvermindDL1

unread,
Feb 26, 2018, 5:57:55 PM2/26/18
to elixir-lang-core
Lol!  I thought that name looked familiar.  ^.^

You should, I know there are a couple of 'incomplete' implementations around (though that one that I cannot remember the name of was the closest...).  :-)
Reply all
Reply to author
Forward
0 new messages