fireproofsocks

fireproofsocks

I’m just discovering make_ref/0 and I’m wondering how to print them as a string… I have poked through the source code, but I don’t see any way to see inside the reference values.

Showing Posts 1 to 10

chouzar

chouzar

This worked for me:

iex(5)> "#{inspect make_ref}"        
"#Reference<0.4290490855.1120927745.192473>"

Never used references before but it seems it can be printed just fine. Or are you looking for a certain property?

ityonemo

ityonemo

Don’t expect to. A reference is just a marker to a “place and time”. They’re usually otherwise empty values that are “guaranteed” to be unique. Now, sometimes they are coopted by the vm to refer to “something” but by and large they aren’t references like references in the JVM.

For example, let me send a call to another process. When the response comes back, it should somehow refer to the initial call, so that this response isn’t structurally confused for a callback for some other call (that maybe got dropped and thus orphaned). The thing that the call sends, which should be packaged with the response, so that it can be can referred back to correctly is a reference.

ityonemo

ityonemo

demo, you can see the return address PID and a reference packaged with the call content quite clearly:

iex(1)> self()
#PID<0.105.0>
iex(2)> pid = spawn(fn -> receive do any -> any end |> IO.inspect end) # trap the next message and print it
#PID<0.108.0>
iex(3)> GenServer.call(pid, :foo) # pretend the spawned thread is a genserver so we can peek inside the message

{:"$gen_call", {#PID<0.105.0>, #Reference<0.27482903.3541827588.143960>}, :foo}

(followed by the call failing because the called pid has stopped itself)

hauleth

hauleth

make_ref()
|> :erlang.ref_to_list()
|> List.to_string()
fireproofsocks

fireproofsocks OP

** (UndefinedFunctionError) function :erlang.reference_to_string/1 is undefined or private. Did you mean one of:

      * ref_to_list/1

    :erlang.reference_to_string(#Reference<0.1918863630.4161011720.226556>)
hauleth

hauleth

Yeah, sorry, it should be :erlang.ref_to_list/1.

fireproofsocks

fireproofsocks OP

I should clarify the use here: I’m working on a (mostly academic) exercise that seeks to run code in multiple modules, sometimes with different runtime values given to the same module, so I want a unique identifier for each “instance” (if you’ll forgive the OO speak). A reference seemed the most appropriate way to do this. The script may generate a lot of output which will be useful for reporting and data analysis, so a CSV or similar representation makes a lot of practical sense. So the question is simply how to represent the unique reference as a string inside of a CSV or similar file. inspect/1 is fine inside iex, but for the purposes of analysis, a simple textual representation is the only thing that wouldn’t look “weird”.

Plan B would be to use a UUID.

hauleth

hauleth

Remember that references are meant to be unique only within single cluster, there is no guarantee for global uniqueness of the reference, in contrast to UUID.

ityonemo

ityonemo

Uuids also have a slightly weak guarantee. Collisions are possible. However, you’ll probably be ok.

hauleth

hauleth

Much stronger than make_ref/0 (64 bits vs 122 bits, which with birthday collision will provide 1010 difference factor in possible collisions). And generating UUID can be done also outside of the BEAM, which is clearly a win for me.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews