Is there a native way to create a guid/uuid in Elixir?

I’ve been searching for how to create a GUID / UUID in Elixir without needing to bring in a dependency, but I can’t seem to find any docs on it other than using the uuid library. It seems to me that using GUIDs are a fairly common place practice in many many different types of applications and other languages provide the ability to create them in their standard libraries. Am I missing something in my searches for a way to do this without a library?

Well if all else fails you can just copy-paste the main file of uuid here: elixir-uuid/uuid.ex at master · zyro/elixir-uuid · GitHub

But I am not sure I understand: what is the problem with using the library? It’s extremely small.

If you don’t care much about the format, you can use an erlang ref:

iex(2)> to_string(:erlang.ref_to_list(:erlang.make_ref()))
"#Ref<0.3277125387.4011851777.221466>"

See the doc:

Returns a unique reference. The reference is unique among connected nodes.

I don’t mind using libraries for things at all, but it seems like relying on a library for something that’s a standard thing in the industry is prone to introducing a problem later. If something in the language changes later and that library isn’t compatible and not updated often then it can lead to issues.

Valid, but implementing UUID generation is easy enough so even if something under our feet changes, I bet somebody will fork uuid and fix it in a matter of a day or two.

That, plus committing to include this in the standard library is very risky. Imagine if there’s another version of the standard tomorrow – that would require a new release of Elixir just to fix that one thing.

Using libraries in languages like Ruby and Python can have the problems you describe depending on how they implement their solution. Usually things like UUID libraries aren’t that complicated so I don’t think you need to worry. But I haven’t found the same to be true in Elixir. Since it’s a functional language it seem like it’s easier to write libraries that don’t need that much upkeep. I have added dependencies to simple libraries that haven’t been updated in a couple of years in Elixir where I would not think of doing the same thing in other languages I’ve used. Except maybe Java.

The vast majority of my experience is in Java and C#, and with both of those it seems like you have to update your libs every other day for some reason or another.

Basically everyone (including ecto) uses elixir_uuid. It’s effectively an “anointed package” at this point, though I wish it had typespecs :slight_smile:

1 Like

Does it? Looking at its mix.exs I don’t see such a dependency, and ecto/lib/ecot/uuid.ex implements UUID4 all on its own. Also, the uuid package has an order of magnitude more installs according to hex.pm.

That said, I doubt either of them are going away anytime soon.

1 Like

If you already depend on Ecto, you can call Ecto.UUID.generate for a UUID4 and get UUIDs without any further deps.

As others have noted, though, Elixir libraries tend to be pretty reliable even when not touched for a long while. This is a very nice benefit of Elixir-the-language being considered “complete” and a strong resistance to adding everything possible to standard library (e.g. UUIDs :wink: ) → the std library has the core tools (as it should) with most everything else in (usually sensibly chunked) libraries, even “very common” things like JSON or UUIDs. As such, things don’t break underneath libraries much at all, and as many libraries have pretty thin dependency chains of their own, this lends a good amount of stability that makes pulling in useful deps pretty low risk.

4 Likes

Elixir_uuid package is the uuid package renamed to prevent collision with the erlang uuid package when hex.pm started to become the package manager for erlang. You should use elixir_uuid moving forward.

I Stand corrected with ecto. I could swear it was the case though, ha.