Unicode and other first impressions

A programmer decides to check out Erlang, and takes these steps:

  1. go to erlang.org
  2. install Erlang
  3. read one of the prominent recommended books from 2007, 2009, 2013
  4. do anything with unicode:
1> "тест".  % not rendered as a string by default
2> <<"тест">>.  % mangled binary literals by default
3> [0|binary_to_list(<<"тест">>)].
4> io:fwrite("~s", ["тест"]).  % unprintable by default
** exception error: bad argument
     in function  io:fwrite/2
        called as io:fwrite("~s",[[1090,1077,1089,1090]])
        *** argument 1: failed to format string
5> io:fwrite("~s", [<<"тест"/utf8>>]).  % mangled output by default
ÑеÑÑ   ok</verbatim></div>

Of course if you already know the language very well, you know what to do instead:

$ erl +pc unicode
Erlang/OTP 27 [RELEASE CANDIDATE 3] [erts-15.0] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Eshell V15.0 (press Ctrl+G to abort, type help(). for help)
1> "тест".
2> ~"тест".
3> io:fwrite("~ts\n~ts\n", ["тест", ~"тест"]).

But people picking up the language don’t already know it well. Where can they find guidance?

erlang.org’s books? “Erlang and OTP in Action” mentions Unicode once, to say that $x syntax also works for Unicode characters. Programming Erlang 2nd ed. and Learn You Some Erlang don’t mention it at all.

erlang.org’s documentation? It has exactly the right information at Erlang -- Using Unicode in Erlang, and you can find that from the front page’s search-engine search, but going further into docs and searching will use the Algolia search that shows incidental references and the ‘unicode’ module. I only found this documentation from the link at Creating a unicode string from a list or bitstring? - #2 by mmin

In particular, Erlang/OTP 26.2.5 links to Erlang -- Frequently Asked Questions about Erlang, which has very good information in it but is awkwardly navigated and ctrl-F resistant (how quickly can you tell if it addresses unicode?) and somewhat stale:

Many people learn on their own, using a book or an online tutorial, often in conjunction with posting to the erlang-questions mailing list or erlang IRC channel on irc.freenode.net.

But a good FAQ is precisely the document that erlang.org should have to counter bad first impressions, apart from a series of “erlang for X programmers” guides.

erlang.org’s communities? StackOverflow is immediately useful. The Slack hides messages older than 90 days, so older discussion can’t be found. This forum was useful.

ChatGPT 3.5 is not that helpful here, but has been helpful in general for Erlang. Bing’s AI is very helpful here and provides citations and related questions. Rather than the current text search, an LLM trained on the documentation that would provide links into it would be great.

For my part, I’m picking Erlang back up after more than a decade-long absence, and I’ve mostly been frustrated with guides that assume tool knowledge that I don’t have, or that describe an elaborate workflow but not a simple one. For an example of the first, GitHub - erlog-lang/erlog: Prolog interpreter in and for Erlang in the first paragraph says that a REPL is available. The first thing I want to do is interact with that REPL. How can I do that?

$ make
$ erl -noshell -pa ebin -s erlog_shell start
Erlog Shell V15.0 (abort with ^G)
| ?- assert(man(socrates)).
| ?- assert((mortal(X) :- man(X))).
X = _0
: mortal(socrates).
| ?-

I figured it out while writing this post, but a couple of days ago I was completely baffled. The natural series of commands from looking at the repo is “make; bin/erlog”, which yields an error.

For an example of the second, GitHub - AdRoll/rebar3_format: Erlang Formatter for Rebar3 describes how to add a formatter to a rebar project but not how to reformat standalone .erl files outside of a rebar project. With go, D, Rust formatters, and more, I can format a single file without any ceremony, but for Erlang it was only after getting hints from other plugin documentation that I saw I could add rebar3_format to ~/.config/rebar3/rebar.config.


One quick comment about erlog and that is that in the bin directory there is an erlog script which would allow you to do:

bin/erlog -pa ebin

Also in the doc/erlog_shell.txt it describes hoe to start and erlog shell from within Erlang. Not so good perhaps but it has been a while since I have worked with it.

1 Like

Well… rebar3_format is a rebar3 plugin. It’s designed to work on rebar3 projects. Nevertheless, its README does say how to format any file that you want (as long as you have a rebar3 project where you can use the plugin, of course):

By default, this will format every Erlang file (including hrls, erls and app.srcs) under src, include and test, together with every .config file, in your root directory and any nested Erlang apps’ directories within your project. You can specify the directory/file to format as following:

$ rebar3 format --files 'src/my_subdir/*.erl'
$ rebar3 format --files src/other_subdir/my_file.erl
$ rebar3 format --files 'test/**/*.erl' --files 'include/*.hrl'

To save the formatted files in a different directory you have to pass it as a parameter:

$ rebar3 format --output formatted/
1 Like

Welcome back Jrfondren!
Your posting is one of the best-researched I have seen on this forum in while.
Your impressions, suggestions and questions are eminently relatable by beginners.
Thank you for your valued contribution.

1 Like