In Elixir how do you initialize a struct with a map variable -
i know possible create struct via %user{ email: 'blah@blah.com' }
. if had variable params = %{email: 'blah@blah.com'}
there way create struct using variable eg, %user{ params }
.
this gives error, wondering if can explode or other way?
you should use struct/2
function. docs:
defmodule user defstruct name: "john" end struct(user) #=> %user{name: "john"} opts = [name: "meg"] user = struct(user, opts) #=> %user{name: "meg"} struct(user, unknown: "value") #=> %user{name: "meg"}
Comments
Post a Comment