Parametric or normal types for dispatching on materials

When generating a type representing a material would it be better to generate types for every material or to create a parametric material type.

What would you suggest and why?

abstract type Material end
struct Gold <: Material end
isMetal(::Gold) = true
struct Material{T} end
const Gold = Material{:Gold}
isMetal(::Gold) = true
1 Like

Unless you want to compile specific code for each material, I would just store properties in a struct. Eg

struct Material
    is_metal::Bool
    # ... surely it has other fields
end
const Gold = Material(true) # look at Base.@kwdef

See

https://docs.julialang.org/en/v1/manual/performance-tips/#The-dangers-of-abusing-multiple-dispatch-(aka,-more-on-types-with-values-as-parameters)

1 Like