here is my recommended solution:
abstract NNModule
getoutput(nnm::NNModule) = nnm.output # <<-- add this line
type LinearModule <: NNModule
weight::Array{Float64}
output::Array{Float64}
end
type SigmoidModule <: NNModule
output::Array{Float64}
end
On Thu, Jun 19, 2014 at 8:18 AM, Abe Schneider <[email protected]>
wrote:
> I'm trying to figure out the best way to factor code that consists of
> repeated modules which all share some common attributes.
>
> Specifically, if I have (very abbreviated from actual code):
>
> abstract NNModule
>
> type LinearModule <: NNModule
> weight::Array{Float64}
> output::Array{Float64}
> end
>
> type SigmoidModule <: NNModule
> output::Array{Float64}
> end
>
> For both modules I need an 'output' variable. I know the idea of putting
> variables in an abstract type has come up before. However, while it would
> solve this problem, it is also not yet resolved (and may or may not
> actually happy).
>
> Given that, two alternatives occur to me:
> 1. Keep the current design and be okay with repeated code (difficult)
> 2. Use a macro to create the repeated code for me
>
> Option (1) is certainly the easiest, and probably the one I'll stick with
> for now. However, it seems error prone and (IMHO) produces ugly code.
>
> Option (2) has the problem that it can make what's actually happening
> opaque and could potentially produce bugs that are difficult to track down.
>
> So my questions are:
> (1) Have other people come across design issues like this before, and how
> did they deal with it?
> (2) Is there a macro out there already to accomplish this? After a quick
> search, I found '@delegate' proposed as a solution for something like this,
> but it was never officially merged. If not, any suggestions on how such a
> macro should act/look?
>
>
> As a side note, it would be great if Julia added properties to abstract
> types. The current design gives something equivalent to Java's interfaces,
> where most languages opt for mixins instead.
>
> Thanks!
>