[erlang-questions] New project: ZJ - A tiny JSON encoder/decoder

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Thu Jun 28 13:15:10 CEST 2018

On Thu, Jun 28, 2018 at 6:50 AM Dmitry Belyaev <be.dmitry@REDACTED> wrote:

> I could never understand where this conversion JSON null <-> Erlang
> undefined was coming from.
> Of course we generally don't use null atom in the code, but I always
> considered "undefined" value in Erlang meaning "absent" value. For example
> when a record is created and some field is not populated explicitly, it is
> set undefined.

​Both variants are somewhat valid: Common Lisp generally treats NIL as the
false value, the empty list, null, ... whereas other languages makes the
distinction: [], false and null are different things in Erlang for instance.

In a statically typed language, you probably would split them. In
particular you would have

type void
type unit = ()
type json = Int of int | String of string | ...  | Null
​Where 'void' is the uninhabitable type which can never be realized in a
program​, 'unit' is the type of one result (and only that result) and a
Null is something of type json.

In Erlang, undefined often corresponds to the unit type. It isn't the same
as void, because in Erlang every term has to return a value, and you cannot
return something of type void.

The tradeoff in the end is that of convenience: null == undefined means you
can share some data, but you then lost the ability to discriminate null and
undefined which from a type/class perspective might be desirable.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180628/18c6d499/attachment.htm>

More information about the erlang-questions mailing list