-
Notifications
You must be signed in to change notification settings - Fork 36
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
A field with a default value set to nil should not be enforced #14
Comments
Hello @lasseebert, An enforced value must be non-nil. By setting defmodule Test do
@enforce_keys [:non_enforced_field]
defstruct non_enforced_field: nil
@type t() :: %__MODULE__{non_enforced_field: integer() | nil}
end Which is obviously invalid: you cannot create a In your first example, the |
Thanks for the explanation :)
which is why I expected typedstruct enforce: true do
field :non_enforced_field, integer | nil, default: nil
end to generate something like defmodule Test do
@enforce_keys []
defstruct non_enforced_field: nil
@type t() :: %__MODULE__{non_enforced_field: integer() | nil}
end |
Mmm. Maybe that’s a documentation issue then. I’ll fix it in the next release. |
Thanks for your quick responses. It all makes sense :) |
@lasseebert You’re welcome. Please let it open so I can remember it. |
Mmm. In fact that’s a bug. Reading my tests, I have : test "does not enforce keys for fields with a default value" do
refute :with_default in EnforcedTypedStruct.enforce_keys()
end
test "does not enforce keys for fields with a default value set to `false`" do
refute :with_false_default in EnforcedTypedStruct.enforce_keys()
end So I’ll add a test for the default value set to |
Awesome 👍 🙂 |
When the struct has
enforce: true
, one can override this by setting a default value on a field. Something like this:But it is not possible to default to nil (it will not un-enforce the field:
I know that adding
enforce: false
on the field will do the same thing, sincenil
is the default value for non-enforces fields, but for the explicitness and readability, it would be nice the set the default value tonil
.If you agree, @ejpcmac, I will be happy to give it a shot.
The text was updated successfully, but these errors were encountered: