-
-
Notifications
You must be signed in to change notification settings - Fork 116
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
Inner constructor #332
Comments
No, you don't need the compat |
@yuyichao: And a 0.4 and 0.5 compatible inner-constructor? |
That's already documented as 0.5 style call overloading syntax. We can certainly add something to the doc, just feels weird since there's nothing about inner constructors that's Compat related....... |
It is definitely confusing. Using an 0.5 constructor in Julia 0.6 gives:
But then using
Anyway, either add a recommendation to the depreciation warning about how to code it 0.5 compatible, or add something to Compat.jl, as that is the place people turn to. |
The deprecation warning would better suggest the syntax which works on both 0.5 and 0.6. |
As someone trying to update a package to work with 0.5 and 0.6, I'm finding it quite unhelpful that there's no way of writing inner constructors using this The best I can come up with at the moment is this:
which then allows me to call:
but it's so untidy compared to what I'd write in either 0.5 or 0.6. If this is the best thing to do, could it be documented clearly somewhere, and if it's not could someone tell me how to write PS Obviously, I'd like to remove immutable too... |
@richardreeve I had the same issue and in the end I came up with a macro that you just prepend to the 0.5-style constructor, i.e. it allows you to write e.g. type LocalFields{ET}
lfields::Vector{ET}
lfields_last::Vector{ET}
move_last::Int
@inner {ET} function LocalFields(N::Int)
lfields = zeros(ET, N)
lfields_last = zeros(ET, N)
return new(lfields, lfields_last, 0)
end
end Here's the code: wrapin(head::Symbol, fn, T::Symbol) = Expr(:curly, fn, T)
function wrapin(head::Symbol, fn, T::Expr)
@assert Base.Meta.isexpr(T, [:tuple, :cell1d])
Expr(head, fn, T.args...)
end
# horrible macro to keep compatibility with both julia 0.5 and 0.6,
# while avoiding some even more horrible syntax
macro inner(T, ex)
VERSION < v"0.6.0-dev.2643" && return esc(ex)
@assert Base.Meta.isexpr(ex, [:(=), :function])
@assert length(ex.args) == 2
@assert isa(ex.args[1], Expr) && ex.args[1].head == :call
@assert isa(ex.args[1].args[1], Symbol)
fn = wrapin(:curly, ex.args[1].args[1], T)
fargs = ex.args[1].args[2:end]
body = ex.args[2]
return esc(Expr(ex.head, wrapin(:where, Expr(:call, fn, fargs...), T), body))
end |
@carlobaldassi Thanks for that, it looks infinitely less ugly than what I'm currently wrestling with! I really need to learn how macros work - though having spend many years avoiding as much preprocessor nonsense as I could in other languages I already wish I hadn't felt I needed to say that :( |
If we had #349 that would address this, but it's not trivial to do. Maybe a few easy cases could be supported first |
That would be great. I appreciate the long term improved flexibility of the new system, but the fact that I'm going to have to spend a year making all of my code worse for backward compatibility reasons does not make me happy at all. I appreciate this is the fate of early adopters, but if there was an @compat fix like #349 then it would help me (everyone?!) write idiomatic Julia 0.6 this year rather than next... |
I don't think this is relevant anymore (0.5 is no longer supported); feel free to re-open if that's not the case |
Am I correct that the way to write a 0.5-compatible inner-constructor in 0.6 is
? If so, it is missing from the Compat README. I can make a PR.
The text was updated successfully, but these errors were encountered: