Hal9000

Hal9000

For example, how does Kernel‘s to_string “know” that it belongs to String.Chars?

If I do a defimpl String.Chars, for: MyMod – how does to_string know
to call my implemented function?

Just curious.

Showing Posts 1 to 2

OvermindDL1

OvermindDL1

The module String.Chars has a function of to_string, and the kernel to_string just calls String.Chars.to_string.

What a protocol does it auto-generate the protocol functions from a list of definitions. So when you defimpl String.Chars, for: MyMod or whatever the syntax was then it defines a module, and at protocol consolidation time it scans all the compiled modules to see what is impl’d (just an attribute marker is all it is, of protocol_impl, so for your example it would have an attribute on the module of protocol_impl: [protocol: String.Char, for: MyMod]), and it uses those to build functions that just test, so in your example it would generate a function of:

defmodule String.Chars do
  # ... other stuff
  def to_string(%MyMod{}=v), do: String.Chars.MyMod.to_string(v)
  # ... other stuff
end

I have an enhanced Protocol library that you can see the code of how it works if curious too. Can also look at the elixir code as well. :slight_smile:

NobbZ

NobbZ

This does only explain it roughly, but should be just enough to understand it.

  1. Kernel.to_string/1 is a macro, expanding to String.Chars.to_string/1
  2. String.Chars.to_string/1 matches on its argument, and without protocol consolidation it dispatches to String.Chars.xxx.to_string/1 where xxx is the qualified alias of the struct or elixirs internal type. With consolidation those xxx implementations are integrated directly into String.Chars.to_string/1.

With consolidation it might look like this (simplified):

defmodule String.Chars do
  def to_string(list) when is_list(list), do: List.to_string(list)
  def to_string(%Foo{bar: bar}), do: "#Foo<bar: #{bar}>"
end

without consilidation more like this:

defmodule String.Chars do
  def to_string(list) when is_list(list), do: List.to_string(list)
  def to_string(%struct{} = item), do: :"Elixir.#{__MODULE__}.#{struct}" |> apply(:to_string, [item])
end
— All posts loaded —

Where Next? Top

Trending in Discussions Top

sntran
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
SyntaxSorcerer
This is the start of my Elixir learning journal/journey. I plan to share my journey in this thread and am open to all feedback, construct...
New
bartblast
This is where the projects in the Hologram Showcase get posted: apps built with Hologram and libraries made for it. New ones go up usuall...
New
karolina.curiosum
Hi everyone! This is our fourth edition. Last year, 1,000+ developers took part. The more of you who answer, the more useful the results...
New
budgie
Never used it. I know it’s well-liked by some and the authors have put a lot of work into it. Aside from that, is there a community cons...
New
mandar1045
I’ve been fixing race condition / crash bugs in an open source org, for a few months now and almost all of them had the same problem. The...
New
fredwu
Over the years I’ve tried dozens of time tracking and invoicing apps, and Harvest was the only one I liked. Primarily due to its relative...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
261 13183 112
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
charlenopires
Hi everyone :waving_hand: I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
PJUllrich
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews