mward-sudo

mward-sudo

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?

Showing Posts 1 to 5

mward-sudo

mward-sudo OP

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.

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
mward-sudo

mward-sudo OP

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

Schultzer

Schultzer

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

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews