Survey.jl icon indicating copy to clipboard operation
Survey.jl copied to clipboard

Simplifying structs

Open ayushpatnaikgit opened this issue 2 years ago • 3 comments

Structures in the package have giant if and else ladders. This is because a user should be allowed to pass probs, weights or popsize, and they all can be vectors as well as symbols, and they can be derived from one another.

The if and else ladder is hard to read and develop upon. How about the following design:

struct PopSize 
popsize
end

function PopSize(data::DataFrame, x::Vector) 
return x 
end

function PopSize(data::DataFrame, x::Symbol)
return data[!, x]
end

struct Weights
wts
end

function Weights(data::DataFrame, x::Vector) 
return x 
end

function Weights(data::DataFrame, x::Symbol)
return data[!, x]
end

struct SurveyDesign
data
weights,
popsize 
end

function SurveyDesign(data, popsize::PopSize)
   SurveyDesign(data, nrow(data)./popsize.popsize, popsize.popsize)
end

function SurveyDesign(data, weights::Weights)
   SurveyDesign(data, nrow(data)./popsize.popsize, sum(weights.weights))
end

User will do:

mydesign = SurveyDesign(apisrs, PopSize(apisrs, :fpc)

Doesn't look nice? Perhaps:

mydesign = @surveydesign (data = apisrs, popsize = :fpc) 

The macro will turn the expression (data = apisrs, popsize = :fpc) into SurveyDesign(apisrs, PopSize(apisrs, :fpc)

ayushpatnaikgit avatar Dec 20 '22 12:12 ayushpatnaikgit

We can look into the macro way? or perhaps later when more functionality related pending tasks done in main? Once more certainty on how many variables needed inside class, then can look into syntactic sugar like this?

smishr avatar Dec 20 '22 14:12 smishr

@iuliadmtru can you see if such a macro is possible?

ayushpatnaikgit avatar Dec 21 '22 07:12 ayushpatnaikgit

Your suggestion definitely feels nicer than the if-elses we have now. Once the other high-priority tasks are done, I will look into this.

iuliadmtru avatar Jan 09 '23 15:01 iuliadmtru