Skip to content

Commit

Permalink
Figured out complicated parametric subtyping of functors, functions, …
Browse files Browse the repository at this point in the history
…and their types
  • Loading branch information
kapple19 committed Aug 6, 2024
1 parent f2fc277 commit 398b0a1
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions scripts/prototype.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
density_profile(::Val{:one}, x::Real; a::Real)::Float64 = x + a
struct DensityProfileFunctionType{MediumSymbol} <: Function end

struct DensityProfile{F <: Function}
profile::F
const ocean_density_profile = DensityProfileFunctionType{:Ocean}()

function DensityProfile(model::Val; pars...)
profile(x::Real)::Float64 = density_profile(model, x; pars...)
new{typeof(profile)}(profile)
ocean_density_profile(::Val{:Homogeneous}, z::Real; ρ::Real = 1500.0) = ρ

const seabed_density_profile = DensityProfileFunctionType{:Seabed}()

seabed_density_profile(::Val{:Homogeneous}, z::Real; ρ::Real = 3000.0) = ρ

abstract type OceanographyProfile <: Function end

struct DensityProfile{MediumSymbol, ProfileFunctionType <: Function} <: OceanographyProfile
profile::ProfileFunctionType

function DensityProfile{MediumSymbol}(model::Val; pars...) where {MediumSymbol}
profile(z::Real) = ocean_density_profile(model, z; pars...)
return new{MediumSymbol, profile |> typeof}(profile)
end
end

function (den::DensityProfile{F} where {F <: Function})(x::Real)
den.profile(x)::Float64
function (den::DensityProfile{MediumSymbol, ProfileFunctionType} where {MediumSymbol, ProfileFunctionType})(z::Real)
den.profile(z)
end

ocn_den = DensityProfile{:Ocean}(Val(:Homogeneous); ρ = 1520.0)
sbd_den = DensityProfile{:Seabed}(Val(:Homogeneous); ρ = 3200.0)

0 comments on commit 398b0a1

Please sign in to comment.