Inconsistency between Erlang and Elixir with :filename.rootname/2

I’m working on the Hologram project which aims to transpile Elixir code to Javascript.

To give context, I’m working on a Javascript implementation of the :filename.rootname/2 function. This should be implemented to strictly follow the Erlang implementation.

This function is then tested using javascript tests, along with corresponding consistency tests that look for the same results using ExUnit.

However, I am experiencing an inconsistency between Erlang and Elixir when using this function with an empty list as the first argument.

If I use erl, passing an empty list returns an empty list.:

1> filename:rootname([], “.erl”). 
[]

If I do the same in Elixir, passing an empty list returns an empty Binary.

iex(1)> :filename.rootname([], “.ext”)
“”

This is mirrored in ExUnit. the following passes:

assert :filename.rootname([], ".erl") == ""

But asserting an empty list fails:

assert :filename.rootname([], ".erl") == [ ]

This means I either cannot maintain strict compatibility with Erlang, or I cannot keep my javascript and ExUnit tests consistent as JS would test for an empty list (a boxed type in Hologram) and an empty Binary in ExUnit.

Is there any way I can coerce Elixir to return the same empty list type that Erlang does, or am I going to have to accept the inconsistency between tests?

1 Like

In the end, I think the solution is to maintain compatibility with Elixir’s implementation. But I’m surprised at the difference when calling an Erlang function.

If you do equivalent calls, then the behavior is consistent, list in, list out, binary in, binary out.

Remember, "foo" in erlang is ~c"foo" in elixir!

$ nix shell nixpkgs#erlang -c erl
Erlang/OTP 28 [erts-16.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]

Eshell V16.2 (press Ctrl+G to abort, type help(). for help)
1> filename:rootname([], <<".erl">>).
<<>>
2> filename:rootname([], ".erl").
[]
3>
User switch command (enter 'h' for help)
 --> q                                                                                                                                                                                                                                                                                                                       $ nix shell nixpkgs#elixir -c iex
Erlang/OTP 27 [erts-15.2.7.4] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.18.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> :filename.rootname([], ".erl")
""
iex(2)> :filename.rootname([], ~c".erl")
[]

edit

You should crosscheck all combinations of inputs and respective behaviour, with empty, non-empty but matching, and non-empty but not matching to ensure full compatibility.

Especially as I don’t know how you treat the difference between binary and charlist on your JS site.

10 Likes

Thank you, you’ve given me something to look in to so I can iron out the details.

This is a great example where single quote for charlist was far superior to double quotes. But in JS everything is stringy. :face_exhaling:

I do not see how '…' vs ~c"…" changes anything here, as the confusion was on the erlang side, rather than on the elixir side.

1 Like