Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

~m{a: 1} generates atom instead of string #15

Open
KristerV opened this issue Jan 18, 2023 · 3 comments
Open

~m{a: 1} generates atom instead of string #15

KristerV opened this issue Jan 18, 2023 · 3 comments

Comments

@KristerV
Copy link

KristerV commented Jan 18, 2023

thanks for the library. i had my homegrown thing which wasn't half as powerful.

there's a bug when using alt names:

iex> Mix.install([:decimal, {:shorter_maps, "~> 2.0"}])
iex> import ShorterMaps
iex> ~m{a}   
%{"a" => 1}
iex> ~m{a: 1}
%{a: 1}

So in ~m{a} the key will be a string and in ~m{a: 1} the key will be an atom.

@KristerV
Copy link
Author

KristerV commented Feb 2, 2023

i dug through the code (tests more specifically) and found that the syntax that does work is ~m{"a" => 1}. i'm going to assume this is the best approach and not dig in to "fix" this.

@KristerV KristerV closed this as completed Feb 2, 2023
@meyercm
Copy link
Owner

meyercm commented Feb 9, 2023

Hi, sorry to have missed you while this was open and you were struggling.

I'm not 100% clear what your initial goal was: if you want the map %{:a => 1}, then you should just use the standard Elixir atom shorthand notation of %{a: 1}

The goal of shortermaps is when your variable names correspond to the keys of a map; so if you had:

iex> a = 1
...> ~M{a}
%{:a => 1}

# -or- 
...> ~m{a}
%{"a" => 1}

in short, ~M is for atom keys, and ~m is for string keys.

Please feel free to re-open if you think there's an issue to fix here.

@KristerV
Copy link
Author

KristerV commented Feb 13, 2023

oh no, i did indeed supply a bad example that didn't explain my motive.

i want to pattern match. let's say there's an incoming map like

a = %{"a" => 1, "b" => 2}

i want to extract both keys, but if b is already in use i need to rename it. so i'd like to go

~m{a, b: alt_b} = a

I'm expecting that sigil to return a map with two string keys, but actually alt_b is an atom.

# translated to
%{"a" => a, b: alt_b} = a

does this make more sense?


edit: I did at one point figure out by reading the tests that the expected syntax for this is ~m{a, "b" => alt_b}. but this isn't very intuitive and it's undocumented. but if this syntax is on purpose then i guess it's just a documentation issue.

@KristerV KristerV reopened this Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants