-
Notifications
You must be signed in to change notification settings - Fork 19
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
Interest in a FieldLens
for bypassing inner constructors?
#179
Comments
Yes, this looks useful and we can add it. I think I would prefer a more ugly name to make clear that this is dangerous stuff that can make your numbers odd. |
Yeah. Maybe something like |
Yes these sound fine. |
About implementation, I wonder if it would be better to do struct FieldLens{field} end
function prop_to_field_lens(optic)
... # replace all occurences of PropertyLens by FieldLens
end
macro unsafe_setfield(ex)
setmacro(prop_to_field_lens, ex, overwrite=false)
end |
yeah that would make sense. |
A relevant old PR with some discussion: #57. I agree such a functionality would be useful, and if others are fine with including the Also, it doesn't really need new macros IMO. Following the linked discussion, there are at least two ways to do it:
|
Also, maybe |
Those alternatives sound kinda annoying to use. I'd rather scare users off by using a scary name rather than more awkward syntax. |
Ofc these preferences are subjective, I just believe that "define a new macro" needs a much higher barrier than "define a new function/struct". Macros can do many nontrivial code transformations, while functions are much easier to reason about. |
Oh, and if adding field support in any capacity, then |
I agree with @aplavin's argument. How about just adding struct UnsafeFieldLens{field} end and the way to use it is: @set obj[i].f |> UnsafeFieldLens(:myfield) = val About |
I tend to agree with you regarding the desire for "scary names" for such a functionality :) Is the Regarding the macro/struct syntax, I think I understand now what subconsciously worried me in the suggested macro. In Accessors, |
So it makes sense that by default, Accessors.jl should be going through ConstructionBase and work in terms of properties, but I think there are times where it might make sense for a user to want to work in terms of fields instead of properties.
Furthermore, I think there are cases where you might want to make this choice at the call-site, rather than overloading
constructorof
and making that choice apply to all instances of a type. I made a demo of an optic for this on Discourse, and it's quite simple:and then here's how you could use it on some struct:
vs normal usage:
Is this something we might want to put into Accessors.jl? I just thought I should ask before I bother making a PR
The text was updated successfully, but these errors were encountered: