Skip to content

Conversation

edevil
Copy link
Contributor

@edevil edevil commented Oct 14, 2016

There was no mention of not passing default values or requiring some fields to be passed a value.

There was no mention of not passing default values or requiring some fields to be passed a value.

## Default values and required values

If you don't specify default field values when defining a struct, **nil** will be assumed:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be

`nil`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

@whatyouhide whatyouhide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments @edevil :)

iex> defmodule Product do
...> defstruct [:name]
...> end
iex> prod = %Product{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to bind the newly created struct to prod here :) We can just show %Product{}.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -113,3 +113,27 @@ iex> Map.keys(john)
```

Structs alongside protocols provide one of the most important features for Elixir developers: data polymorphism. That's what we will explore in the next chapter.

## Default values and required values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "and required fields"? Wdyt? Not sure about this one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't they be keys ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eksperimental I think we use key and field interchangeably in structs, personally I prefer fields but any one of them is ok.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the thing is error messages in the example use "key", and we are using an attribute called "@enforce_keys"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go with key then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

...> @enforce_keys [:make]
...> defstruct [:model, :make]
...> end
iex> car = %Car{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, we don't need the car variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


## Default values and required values

If you don't specify default field values when defining a struct, `nil` will be assumed:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should read "If you don't specify a default field value", since only one value can be assigned.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

%Product{name: nil}
```

You can also enforce that certain fields have to be specified when creating the struct:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keys is what docs uses to refer to fields: "You can also enforce that certain keys have to be specified"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@josevalim
Copy link
Member

Looks good to me, thank you @edevil. ❤️

@whatyouhide, please merge when you are ready.


## Default values and required keys

If you don't specify a default field value when defining a struct, `nil` will be assumed:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're calling the structs fields "keys" everywhere apparently, let's fix this to be "[...] a default key value [...]" and then we're good to go!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@whatyouhide whatyouhide merged commit 0d54fda into elixir-lang:master Oct 15, 2016
@whatyouhide
Copy link
Member

Thanks a lot @edevil! 💟

@edevil edevil deleted the patch-1 branch October 16, 2016 01:09
vladdu pushed a commit to vladdu/elixir-lang.github.com that referenced this pull request Jan 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants