Hello everyone,
I 've been using Julia for a few months now, and I really love it, especially the type system.
I’m working on a code to compute the optical reflectivity of a multilayer. The multilayer is a stack of homogeneous plane parallel media (layers). Each layer has a thickness and optical properties.
For now I’m using singleton types for the optical properties, but it seems this is not recommended and I would like to use value types instead. But I don’t manage to implement this.
I’m using Julia 0.5.0.
This is how I’m handling the simulations now.
I first define an abstract type OptProp
abstract  OptProp
I then define singleton types for different materials
immutable Sic <:  OptProp end
immutable Al <:  OptProp end
immutable Vac <:  OptProp end
This allows me to dispatch on the permittivity function to obtain the optical properties for each of those materials.
For instance for the optical properties of Aluminium I use:
function permittivity(material :: Al(), frequency)
     some code
end
and similarly for other materials.
I then define the layer type
immutable Layer
    material :: OptProp
    thickness :: Float64
end
A multilayer is then a vector of the Layer type. As an example, an interface between vacuum and alumium would be defined as
interface = [Layer(Vac(),0.0); Layer(Al(),0.0)]
More complex multilayers can be constructed adding layers with other materials and thickness.
This works fine, but I’m not sure this is the recommended way.
I tried to implement the “value type” -approach, but I didn’t succeed.
Here is what I tried:
immutable Mater{T}<: OptProp
end
immutable Layer{T}
    material :: Mater{T}
    thickness :: Float64
end
This new version of the Layer type does not work.
If I do for a semi-infinite medium of aluminium:
Layer(Mater{:al},0.0)
I get a MethodError : no method matching Layer{T}(::Type{Mater{:al}},:: Float64)
I have three questions:
- Is there an overhead defining vectors of composite types containing singleton types (first approach)?
- Is it recommended/more efficient to use value types for the material properties, instead of the singleton types?
- If the answer to question 2 is yes, how would you actually implement it?
Many thanks in advance.
Olivier
 But the reason your last permittivity function is fast is because there is no dispatch at all.
 But the reason your last permittivity function is fast is because there is no dispatch at all.