frost

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

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

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.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement