-
Notifications
You must be signed in to change notification settings - Fork 32
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
Provide hooks to massage data after creation #52
Comments
I don't quite understand. Would it be solved with "Calculated fields" #18? Also note that if you specify the positional constructor (for all fields) then the kw-constructur will run through that. That may allow to manually program what you're after. |
@mauro3 |
But can't you provide finalizers with just registering a function: https://docs.julialang.org/en/stable/stdlib/base/#Base.finalizer: |
When do you add a finalizer? Usually at object creation time. |
Thanks, now I get it. I think the post-create hook could be ok to implement as that would be a hook of a function of one argument. The pre-create hook however (I think) would need to be a function which has all fields as positional arguments, so it would not be any easier to write than the positional constructor. So, for the post-create hook @macroexpand @with_kw struct A
a=1
b=2
@post_create a -> finalizer(a, finalfn) # post_create_fn = a -> finalizer(a, finalfn)
end
struct A
a
b
A(; a=1, b=2) = begin
A(a, b)
end
A(a, b) = begin
post_create_fn(new(a, b))
end
end I never had an application for this so it's low on my priority list. But a PR, if it does not add too many LOCs, would be welcome. |
Current workaround is making data structure have one not-parameterized field and then using a base constructor that accepts at least one argument (i.e. the non-parameterized one) This is related to the following line of code: |
I don't understand that work-around, maybe you can post a MWE. What do you mean with "base constructor"? An inner or outer constructor? |
If the pre-create hook would be just a block of code to be inserted into the positional inner constructor, then it would have access to all variables. Something like: @macroexpand @with_kw struct A
a=1
b=2
@pre_create begin
println("Before `new`")
a = a+1
end
@post_create a -> finalizer(a, finalfn) # post_create_fn = a -> finalizer(a, finalfn)
end
struct A
a
b
A(; a=1, b=2) = begin
A(a, b)
end
A(a, b) = begin
begin
println("Before `new`")
a = a+1
end
post_create_fn(new(a, b))
end
end Unfortunately, this makes the pre and post hooks asymmetric... |
Another use case from #72 is to add a deprecation warning to a field:
|
I don't think having symmetric pre/post hooks is that important? One ultimate use case might be something like:
|
I'm a bit confused by the question you ask yourself. The example you give seems closer to calculated fields, no? Or what is |
This would also be the way to generalize what macros (and other code) do inside the type-def. Currently |
If you have
mutable structs
, you can have fields you want to set before or after the constructorParameters.jl
uses.Is there a way to provide access to wrapping that function?
Maybe with
@before_create
and@after_create
(hook) macros?The text was updated successfully, but these errors were encountered: