mward-sudo

mward-sudo

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?

First Post! Switch mode

mward-sudo

mward-sudo

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.

Most Liked

NobbZ

NobbZ

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
Post #2
NobbZ

NobbZ

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

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement