Skip to content
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

from Setfield to Accessors #151

Open
rveltz opened this issue Jun 7, 2024 · 5 comments
Open

from Setfield to Accessors #151

rveltz opened this issue Jun 7, 2024 · 5 comments

Comments

@rveltz
Copy link

rveltz commented Jun 7, 2024

Hi,
I want to switch from Setfield to Accessors. However I dont know the supertype of @optic

julia> using Setfield

julia> (@lens _) isa Lens
true

julia> using Accessors

julia> (@optic _) isa PropertyLens
false 

Can anyone give me a hint please?

@jw3126
Copy link
Member

jw3126 commented Jun 7, 2024

There is none, optics are duck-typed:

julia> @optic _
identity (generic function with 1 method)

julia> @optic(_) === identity
true

It is the identity function.

@rveltz
Copy link
Author

rveltz commented Jun 7, 2024

I see.

To emulate a supertype, what do you think of

const AllLensTypes = Union{PropertyLens, IndexLens, ComposedOptic, typeof(identity)}
@optic(_) isa AllLensTypes

@jw3126
Copy link
Member

jw3126 commented Jun 7, 2024

Depends on what the goal is. In the same way that I would usually prefer function map(f, itr) over function map(f::Function, itr) I usually prefer optics to be duck-typed. But it is a tradeoff and if it suits your use-case, you can go with a definition like that. But it will neither cover every optic, nor will every instance of your AllLensTypes be a valid optic.

@aplavin
Copy link
Member

aplavin commented Jun 7, 2024

Probably const AllLensTypes = Union{PropertyLens, IndexLens, Function} is a reasonable approximation. Out of curiosity, do you have an example where you really need to constrain the type like this?

@rveltz
Copy link
Author

rveltz commented Jun 7, 2024

mostly to help the user. I am so used to Setfield, I have to adapt to your new framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants