Qqwy
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?
Trending in Discussions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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, isgetone of them? If so you can override access that way…Qqwy
@OvermindDL1 The Access protocol has
fetch(container, key)(get value forkeyfrom structure, return success tuple).get(container, key, default)(get value forkeyfrom structure with default if it does not exist).get_and_update(container, key, function)(call function on value found forkeyif it exists, depending on the result possibly mutate container).pop(container, key)(removekeyand 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
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:foo[:bar]However is a function callAccess.getwhich you can see easily from quoting it:I don’t believe any fancy inlining happens there.
PatNowak
The most important things to remember:
So:
dots for structs
square brackets for keyword lists
both for maps
Qqwy
@PatNowak You are mistaken, the dot operator will fail at runtime for both structs and maps (and not at compiletime).
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.
PatNowak
@Qqwy Thanks, fixed
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
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
foois a map.Qqwy
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
How would it be different then just making a struct field by an anonymous function, just saving the
.part of the invocation to becomeblah.vweep(42)instead ofblah.vweep.(42)or so?