-
Notifications
You must be signed in to change notification settings - Fork 27
try to handle structs with custom constructors #181
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
Conversation
|
One can bypass inner constructors. I think it makes sense to bypass constructors because we are working with the fields directly macro force_construct(T, args...)
return esc(Expr(:new, T, args...))
endjulia> struct CustomConstructorArray{T, N, A<:AbstractArray{T, N}} <: AbstractArray{T, N}
data::A
function CustomConstructorArray(data::A) where {T, N, A<:AbstractArray{T, N}}
return new{T, N, A}(data)
end
end
julia> Base.size(a::CustomConstructorArray) = size(a.data)
julia> Base.getindex(a::CustomConstructorArray, inds...) = getindex(a.data, inds...)
julia> macro force_construct(T, args...)
return esc(Expr(:new, T, args...))
end
@force_construct (macro with 1 method)
julia> x = CustomConstructorArray([1.0 2.0; 3.0 4.0])
2×2 CustomConstructorArray{Float64, 2, Matrix{Float64}}:
1.0 2.0
3.0 4.0
julia> y = @force_construct(typeof(x), [5.0 6.0; 7.0 8.0])
2×2 CustomConstructorArray{Float64, 2, Matrix{Float64}}:
5.0 6.0
7.0 8.0
|
|
Wow, what is this In any case, we need to splat the arguments, so the macro would have to be a bit more complicated. I'd like to give it a go if you think it's the right path forward |
As I understand it, it is the thing that powers the I think it is the right path. |
|
It seems like 1.3 introduced splatnew @simeonschaub says for 1.0 we need to do @generated force_construct(T, args...) = Expr(:new, :T, Any[:(args[$i]) for i in 1:length(args)]...)but after 1.3 we can do using generated functions rather than macros |
Before we would see something like:
because there is a custom constructor. Not the best solution (one could imagine trying to figure out what the constructor is), but an improvement over the current state.