How to define the struct in the correct way, look at the following code snippet:
defmodule SapOdataService.Worker do
defstruct uri: "", user: nil, password: nil
Should I define the default value as nil or?
You have a few options.
You can list the key value pairs explicitly using nil
for blank values
defmodule User do
defstruct name: "zero_coding", age: nil
end
You can pass a list of atoms that will all default to nil:
defmodule User do
defstruct [:name, :age]
end
You can do a mix of the above a list of atoms that will all default to nil:
defmodule User do
defstruct [:age, name: "zero_coding"]
end
You can also enforce specific keys that must be specified when creating the struct
defmodule User do
@enforce_keys [:name]
defstruct [:name, :age]
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With