Elixir: struct default value from function -
can default value variables of struct defined function instead of raw value?
a default value struct field expression evaluated @ time of struct definition.
proof:
# struct.exs defmodule m defstruct [a: io.gets("> ")] end # ... $ iex struct.exs erlang/otp 17 [erts-6.0] ... > hello interactive elixir (0.13.3-dev) - ... iex(1)> %m{} %m{a: "hello\n"}
you can define function create struct , set of fields:
# struct.exs defmodule m defstruct [a: nil] def new(val) %m{a: val} end end # ... m.new(123) #=> %m{a: 123}
Comments
Post a Comment