Are interfaces like Abstract Classes in the sense that super-type methods work on class subtypes?
Tag : java , By : Lathentar
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , If MyClass implements MyInterface, then a reference to a MyClass, or any type derived from MyClass, is also a reference to that interface. When cast to the interface type, using any member of that interface will cause the corresponding MyClass member to be used on the instance of MyClass (or type derived from it). These facts mean that in most cases code which expects a MyInterface will work as expected when given a reference to a MyClass, but that is not always 100% true. Some interfaces include "optional" functionality which is implemented in some but not all implementing classes. For example, an immutable key-value mapping class might implement the Map interface but not support the put method. A method which expects to add values to a passed-in Map would fail if given an reference to an object which did not allow such functionality.
|
How to specify subtypes of abstract type as type parameters in Julia?
Date : March 29 2020, 07:55 AM
This might help you When you get into a situation where you can use a type but not any of it's subtypes, it can be often involved by introducing a type parameter in the right place. The following seems to work: abstract Component
type Position<:Component
x::Real
y::Real
end
typealias ComponentType{T<:Component} Type{T}
v = Vector{ComponentType}()
push!(v, Position)
|
Generic constructors for subtypes of an abstract type
Date : October 27 2020, 04:13 PM
this will help It's a little funny-looking, but you actually can dispatch on the type of the function itself! In the case of a constructor, the function is the type in question: julia> abstract type AbstractT end
julia> struct ConcreteT{T} <: AbstractT; end
julia> (::Type{ConcreteT{Int}})() = 1
julia> (::Type{ConcreteT{Float64}})() = 2
julia> ConcreteT{Int}()
1
julia> ConcreteT{Float64}()
2
julia> (::Type{ConcreteT{T}})() where {T<:Number} = 3
julia> (::Type{ConcreteT{T}})() where {T<:AbstractArray} = 4
julia> ConcreteT{Float32}()
3
julia> ConcreteT{UnitRange{Int}}()
4
julia> (::Type{T})() where {T<:AbstractT} = 5
julia> ConcreteT{String}()
ConcreteT{String}()
julia> AbstractT()
5
julia> methods(ConcreteT{String})
# 2 methods for type constructor:
[1] (::Type{ConcreteT{T}})() where T in Main at REPL[2]:1
[2] (::Type{T})() where T<:AbstractT in Main at REPL[12]:1
julia> (::Type{T})(x) where {T<:AbstractT} = x
julia> ConcreteT{String}(6)
6
julia> ConcreteT{Int}(7)
7
|
julia type unstable when accessing field from abstract type
Date : March 29 2020, 07:55 AM
wish of those help If you define getX to take subtypes of AT then you have type stable code for foo: julia> function getX(a::T)::Int where T <: AT
a.x
end
getX (generic function with 1 method)
julia> foo() = getX(rand([T1(1),T2(2)]))
foo (generic function with 1 method)
julia> @code_warntype foo()
Variables:
#self# <optimized out>
T <optimized out>
Body:
begin
SSAValue(0) = (Core.tuple)($(Expr(:new, :(Main.T1), 1)), $(Expr(:new, :(Main.T2), 2)))::Tuple{T1,T2}
return (Main.getX)($(Expr(:invoke, MethodInstance for rand(::Array{AT,1}), :(Main.rand), :($(Expr(:invoke, MethodInstance for copy!(::Array{AT,1}, ::Tuple{T1,T2}), :(Base.copy!), :($(Expr(:foreigncall, :(:jl_alloc_array_1d), Array{AT,1}, svec(Any, Int64), Array{AT,1}, 0, 2, 0))), SSAValue(0)))))))::Int64
end::Int64
|
How to define method for all parametric subtypes of an abstract type in Julia?
Date : March 29 2020, 07:55 AM
will be helpful for those in need Suppose I have an abstract base class: , Just add the type parameter to the abstract type. abstract type B{T,V} end
struct C1{T,V} <: B{T,V} end
struct C2{T,V} <: B{T,V} end
result_type(::B{T,V}) where {T,V} = T
|