Qqwy

Qqwy

TypeCheck Core Team

I would like to spark a discussion about the static access operator: ..
For whom does not know: it is used in Elixir to access fields of a struct or map, where you want to be sure (/are sure) that a given (atom) key exists.

It is arguably more easy to write out foo.bar instead of foo[:bar] or binding it in a pattern-match, but I have found that relying on . too quickly/often is a recipe for tightly-coupled code, in my opinion.

Namely, changing the internals of a %Foo{} struct becomes impossible when you (or, even worse, users of your library) rely on being able to type foo.bar. If you want to be able to change the internals of a data structure, you should instead have written a function like Foo.bar(foo). Besides being more explicit (and also slightly longer to type), this allows you to alter the way bar is obtained from within the structure at a later time.

I have run into this issue multiple times now while developing libraries, so I want to ask you all about your opinion about this subject: Do you rely on using . a lot? Do you agree that . should be used with care? Are there alternatives? How to you mitigate this issue? Or is it, in your opinion, not an (important) issue at all?

Showing Posts 1 to 10

OvermindDL1

OvermindDL1

Direct field access does irk me about every time I use it because I know it is doing a hidden function call behind the scenes, if there was truly unique syntax for accessing each thing unambiguously that could be properly inlined, I would be significantly happier, plus I’d not have any worry of handling the wrong things, though then this becomes “Why don’t we add types so you know you are accessing something that actually exists at compile time instead of just praying”, and etc… slippery slope (sudden urge to work on ElixirML some more, blehg too busy with work projects)…

EDIT: Also, I know you can override some Access.* things, is get one of them? If so you can override access that way…

Qqwy

Qqwy OP

TypeCheck Core Team

@OvermindDL1 The Access protocol has

  • fetch(container, key) (get value for key from structure, return success tuple).
  • get(container, key, default) (get value for key from structure with default if it does not exist).
  • get_and_update(container, key, function) (call function on value found for key if it exists, depending on the result possibly mutate container).
  • pop(container, key) (remove key and its value from the container).

I do wonder what happens when the static access syntax is compiled. It might actually be inline-able to some extent. (Either at the Elixir → Erlang or at the Erlang → BEAM level).

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I’m a bit confused, what is “direct field access” here, foo.bar ? If that’s what you mean it isn’t really a function call, IIRC it translates to basically:

case foo do
  %{bar: val} -> val
  _ -> apply(foo, :bar, []) # or an error handling clause, can't recall
end

foo[:bar] However is a function call Access.get which you can see easily from quoting it:

iex(1)> quote do: foo[:bar]
{{:., [], [Access, :get]}, [], [{:foo, [], Elixir}, :bar]}

I don’t believe any fancy inlining happens there.

PatNowak

PatNowak

The most important things to remember:

foo.bar # strict access, throws error if key doesn't exist
foo[:bar] # dynamic access, returns nil if key doesn't exist

So:
dots for structs
square brackets for keyword lists
both for maps

Qqwy

Qqwy OP

TypeCheck Core Team

@PatNowak You are mistaken, the dot operator will fail at runtime for both structs and maps (and not at compiletime).

OvermindDL1

OvermindDL1

I think he meant that dot access is statically ‘called’ and bracket access is dynamically ‘called’, but yeah the terms could have been more clear. :slight_smile:

PatNowak

PatNowak

@Qqwy Thanks, fixed :slight_smile:
I meant that strict access is called in compile time, because it checks whether key exists in map /struct or not. It helps a lot to ensure that data is valid in terms of the content.

I usually use dots when dealing with structs and square bracket access when dealing with maps - especially in case and if statements.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

But this isn’t the case. Nothing at all happens at compile time to ensure that the key does or does not exist in the map, nor even that foo is a map.

Qqwy

Qqwy OP

TypeCheck Core Team

I’d love to hear more opinions about this.

One of the things I am considering right now, is to create a small library that adds overridable functions to a module that defines a struct, to access all struct fields instead of using .fieldname, so these access methods can be changed in the future.

OvermindDL1

OvermindDL1

How would it be different then just making a struct field by an anonymous function, just saving the . part of the invocation to become blah.vweep(42) instead of blah.vweep.(42) or so?

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
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
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
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

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...
New
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews