Is it possible to have a function for setting values inside an existing struct? I'm talking about the idea of passing the existing struct into a function and setting that structs "name" value (for example)?
What I have:
main.exs
Code.require_file("user.exs") # Requiring in module
person1 = User.constructor("Name") # Making a new user
IO.write inspect person1
user.exs
defmodule User do
defstruct [name: ""]
def constructor(name) do
%User{name: name}
end
end
Any way to get this idea working?
def setName(struct, newName) do
struct.name = newName
end
Thanks
user.exslikeperson1 = %User{name: "Name"}instead of calling some constructor function.