I’m trying to implement an API which supports something along these lines:
struct MyObject field_one field_two field_three end using Distributions object_dist = MyObject(field_one = Normal(0, 2), field_two = (Uniform(-2, 1), Normal(0, 1)), field_three = 5) rand(object_dist) # returns MyObject with random float field_one, tuple of random floats field_two; field_three always = 5 nparams(object_dist) # = 3 from_params(object_dist, [1., 2., 3.]) # MyObject(1, (2., 3.), 5) param_dists(object_dist) # [Normal(0, 2), Uniform(-2, 1), Normal(0, 1)]
But it turns out not that easy. I cannot understand how to “convert” type of
object_dist replacing all
Distributions with floats, and how to build a (nested) struct from a flat vector of values, filling in only some of the fields - those that are specified as
Any ideas on how to do that, or maybe design something similar, but simpler?