是否有可能从包含对该结构的引用的变量实例化新结构?

问题描述:

考虑下面的例子。是否有可能从包含对该结构的引用的变量实例化新结构?

iex(2)> defmodule User do 
...(2)> defstruct name: "tester" 
...(2)> end 
{:module, User, 
<<70, 79, 82, 49, 0, 0, 8, 32, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 232, 
    0, 0, 0, 22, 11, 69, 108, 105, 120, 105, 114, 46, 85, 115, 101, 114, 8, 95, 
    95, 105, 110, 102, 111, 95, 95, 9, 102, ...>>, %User{name: "oste"}} 
iex(4)> test = User 
User 
iex(5)> %test{} 
** (CompileError) iex:5: expected struct name to be a compile time atom or alias, got: test 

iex(5)> %User{} 
%User{name: "tester"} 

我得到的结构从配置传递,我想创建新的基于指定配置的结构。

我认为这可能工作:

test = User 
struct(test) 

全部信息,可以发现here

+0

完美! Thx朋友。 – MartinElvar