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

Shortcut syntax for key/value pair lists and maps used in this tutorial #59

Open
iteles opened this issue Aug 14, 2017 · 3 comments
Open
Assignees
Labels
T25m Time Estimate 25 Minutes

Comments

@iteles
Copy link
Member

iteles commented Aug 14, 2017

In most beginner learning resources I have seen, map literals are presented as:

  animal = %{
     :name => "Rex",
     :type => "dog",
     :legs => 4
}

In this repo, we dive straight into using the shortcut for key/value maps (https://github.com/dwyl/learn-elixir#maps).

animal = %{
  name: "Rex",
  type: "dog",
  legs: 4
}

My understanding is that whilst this is the most common usage, it's also only able to be used if the keys are atoms. Is it worth mentioning that this is a possible syntax so that beginners who see other resources are not confused?

@iteles iteles added the discuss Share your constructive thoughts on how to make progress with this issue label Aug 14, 2017
@YvesMuyaBenda
Copy link

YvesMuyaBenda commented Aug 14, 2017

It is a really small syntax transformation, so an explanation would not take up much space, but explaining it will make the exposition that little bit smoother, and better integrated with other resources, so I see no downside, but a clear if small upside: it often helps to first see what a shortcut is, well, shortcutting.

My basic understanding is that the shortcut is a clearer sign of intent, something like the direct analogy of Javascript object literals, whereas the first form is a more general construction, that of a dictionary where the keys are not always/necessarily considered names. In my mind it is the role that atoms play in Elixir as literal names that is the pattern that is of importance and interest.

Anyways, there be my thoughts.

@iteles iteles added the T25m Time Estimate 25 Minutes label Aug 15, 2017
@iteles iteles self-assigned this Aug 15, 2017
@iteles iteles added in-progress An issue or pull request that is being worked on by the assigned person and removed discuss Share your constructive thoughts on how to make progress with this issue labels Aug 15, 2017
@ZooeyMiller
Copy link

I'm a bit late on this, but I definitely agree that a brief description of the differences between the two pieces of syntax would be a really good addition, especially considering the different ways to access the keys dependant on the type.

I definitely ran into confusion when trying to access values in the map literals with map.key rather htan pattern matching.

@iteles iteles removed the in-progress An issue or pull request that is being worked on by the assigned person label Sep 21, 2019
@iteles
Copy link
Member Author

iteles commented Sep 21, 2019

Re-learning elixir and I sat and stared at the last of the 3 iex lines for 5 minutes (from Dave Thomas' Programming Elixir 1.6 book), wondering why it was different before I figure out that this was shortcut syntax for the last line which is a map of atoms (rather than strings or tuples for example):

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T25m Time Estimate 25 Minutes
Projects
None yet
Development

No branches or pull requests

3 participants