frost
Is it not possible to use UPPERCASE struct field names?
I just had the idea that I would create a struct with some fields in uppercase, and I ran in to a lot of seemingly weird behaviour.
Given this code:
defmodule Lower do
defstruct lower: "lower"
end
defmodule Upper do
defstruct UPPER: "UPPER"
end
When I try these out in IEx, I can instantiate them just fine, and I can type lower. and hit the tab-key, and it prints the fields, but I can’t seem to tab-complete the UPPER field on an %Upper{} struct, and if I manually type it out, I get an error message:
iex(38)> lower = %Lower{}
%Lower{lower: "lower"}
iex(39)> lower.
__struct__ lower
iex(39)> lower.lower
"lower"
iex(40)> upper = %Upper{}
%Upper{UPPER: "UPPER"}
iex(41)> upper.
UPPER __struct__
iex(41)> upper.UPPER
** (CompileError) iex:41: invalid alias: "upper.UPPER". If you wanted to define an alias, an alias must expand to an atom at compile time but it did not, you may use Module.concat/2 to build it at runtime. If instead you wanted to invoke a function or access a field, wrap the function or field name in double quotes
Does anyone know if this is intended, and if so, where I can read up on why this happens?
Marked As Solved
gregvaughn
iex(36)> :"Elixir.UPPER" = UPPER
UPPER
Upper case identifiers are prefixed with Elixir behind the scenes. They are intended to be used for module names and the prefix ensures that no Elixir modules will conflict with Erlang code.
Also Liked
03juan
Welcome to the community
This technically works but won’t be tab-completed either:
iex> defmodule Upper, do: defstruct UPPER: "UP"
{:module, Upper, <<...>>, %Upper{UPPER: "UP"}}
iex> upper = %Upper{}
%Upper{UPPER: "UP"}
iex> upper."UPPER"
"UP"
iex> :UPPER = :"UPPER"
:UPPER
iex> Map.keys(upper) |> Enum.map(&Atom.to_string/1)
["UPPER", "__struct__"]
You can define a “raw” atom with : and even wrap it with quotes, but the compiler will try to expand upper.UPPER as mentioned.
Not even this will help
iex> defmodule Upper, do: defstruct [{:"Elixir.UPPER", "UP"}]`
iex> upper = %Upper{}
%Upper{UPPER => "UP"}
iex> upper.UPPER
** (CompileError) iex:..: invalid alias: "upper.UPPER". ...
iex> upper."Elixir.UPPER"
"UP"
And now the word “upper” has lost all meaning in my brain.
Popular in Questions
Other popular 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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









