-
Notifications
You must be signed in to change notification settings - Fork 832
Default values and required values #864
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
Conversation
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be
`nil`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this 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{} |
There was a problem hiding this comment.
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{}
.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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{} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
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: |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Thanks a lot @edevil! 💟 |
There was no mention of not passing default values or requiring some fields to be passed a value.