Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add boilerplate outer constrcutors to EossParams #45

Closed
singularitti opened this issue Aug 17, 2020 · 1 comment · Fixed by #46
Closed

Add boilerplate outer constrcutors to EossParams #45

singularitti opened this issue Aug 17, 2020 · 1 comment · Fixed by #46
Assignees
Labels
design doc List possible ways of implementations and see what's the best

Comments

@singularitti
Copy link
Member

singularitti commented Aug 17, 2020

In the past I used to add outer constructors using eval-quote to types that are concrete subtypes of a type, that was slow to load and a bit o f ugly. Or I can list all concrete subtypes by hand, it's maybe a bit faster but it requires manual updating.

@singularitti singularitti added the design doc List possible ways of implementations and see what's the best label Aug 17, 2020
@singularitti singularitti self-assigned this Aug 17, 2020
@singularitti
Copy link
Member Author

singularitti commented Aug 17, 2020

But now I found I could just write

(::Type{T})(arr::AbstractVector) where {T<:EossParam} = T{eltype(arr)}(arr...)
(::Type{T})(args...) where {T<:EossParam} = T([args...])

and it works!

julia> BirchMurnaghan3rd(1,2,3.0)
BirchMurnaghan3rd{Float64}
 v0 = 1.0
 b0 = 2.0
 b′0 = 3.0
 e0 = 0.0


julia> BirchMurnaghan3rd([1,2,3.0])
BirchMurnaghan3rd{Float64}
 v0 = 1.0
 b0 = 2.0
 b′0 = 3.0
 e0 = 0.0

No need to rely on eval, ConstructionBase.constructorof and dispatch on abstract types! And it works fine on Julia 1.0!

Also, there is no type-piracy on Type:

julia> methods(Type)
# 0 methods for generic function "(::Type)":

julia> methods(EossParam)
# 2 methods for generic function "(::Type)":
[1] (::Type{T})(arr::AbstractArray{T,1} where T) where T<:EossParam in EquationsOfStateOfSolids.Collections at ~/.julia/dev/EquationsOfStateOfSolids/src/Collections.jl:230
[2] (::Type{T})(args...) where T<:EossParam in EquationsOfStateOfSolids.Collections at ~/.julia/dev/EquationsOfStateOfSolids/src/Collections.jl:231

And directly call on EossParam will error as expected:

julia> EossParam(1,2,3)
ERROR: TypeError: in Type{...} expression, expected UnionAll, got Type{EossParam{Int64}}
Stacktrace:
 [1] EossParam{Int64}(::Array{Int64,1}) at ~/.julia/dev/EquationsOfStateOfSolids/src/Collections.jl:230
 [2] EossParam{Int64}(::Int64, ::Vararg{Int64,N} where N) at ~/.julia/dev/EquationsOfStateOfSolids/src/Collections.jl:231
 [3] EossParam(::Array{Int64,1}) at ~/.julia/dev/EquationsOfStateOfSolids/src/Collections.jl:230
 [4] EossParam(::Int64, ::Vararg{Int64,N} where N) at ~/.julia/dev/EquationsOfStateOfSolids/src/Collections.jl:231
 [5] top-level scope at none:0

julia> EossParam([1,2,3])
ERROR: TypeError: in Type{...} expression, expected UnionAll, got Type{EossParam{Int64}}
Stacktrace:
 [1] EossParam{Int64}(::Array{Int64,1}) at ~/.julia/dev/EquationsOfStateOfSolids/src/Collections.jl:230
 [2] EossParam{Int64}(::Int64, ::Vararg{Int64,N} where N) at ~/.julia/dev/EquationsOfStateOfSolids/src/Collections.jl:231
 [3] EossParam(::Array{Int64,1}) at ~/.julia/dev/EquationsOfStateOfSolids/src/Collections.jl:230
 [4] top-level scope at none:0

julia> FiniteStrainEossParam(1,2,3)
ERROR: TypeError: in Type{...} expression, expected UnionAll, got Type{FiniteStrainEossParam{Int64,Int64}}
...

julia> FiniteStrainEossParam([1,2,3])
ERROR: TypeError: in Type{...} expression, expected UnionAll, got Type{FiniteStrainEossParam{Int64,Int64}

And it's fast, complete and no repetition of code!

julia> methods(BirchMurnaghan3rd{Int})
# 4 methods for type constructor:
[1] BirchMurnaghan3rd(v0, b0, b′0) where T
 @ EquationsOfStateOfSolids.Collections ~/.julia/dev/EquationsOfStateOfSolids/src/Collections.jl:46
[2] BirchMurnaghan3rd(v0, b0, b′0, e0) where T
 @ EquationsOfStateOfSolids.Collections ~/.julia/dev/EquationsOfStateOfSolids/src/Collections.jl:46
[3] EossParam(arr::AbstractArray{T,1} where T) where T<:EquationsOfStateOfSolids.Collections.EossParam
 @ EquationsOfStateOfSolids.Collections ~/.julia/dev/EquationsOfStateOfSolids/src/Collections.jl:257
[4] EossParam(args...) where T<:EquationsOfStateOfSolids.Collections.EossParam
 @ EquationsOfStateOfSolids.Collections ~/.julia/dev/EquationsOfStateOfSolids/src/Collections.jl:258

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design doc List possible ways of implementations and see what's the best
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant