-
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
add shortcuts for Elements(), Properties(), @optic #125
Conversation
I wished several times, that these were easier to type. My main issue is that they require so many pipes and underscores around them. I wonder if we could do something like One downside of the current proposal is that it collides with |
btw thanks a lot for all the work you put into accessors already this year 😄 |
Same with me. These shortcuts (and
Especially for Alternatively, That's why I went with |
Yeah, from time to time I look at |
∗= 3
f = @optic _[∗]
@assert f([1,2,42]) == 42 # will fail with this proposal is a gotcha. |
Heh, nice edge case :) We can fix that by always resolving |
After some thinking, I believe the regular export approach is both most straightforward, and least like to cause inconsistencies. Your example is similar to someone defining a variable/function/... named |
In these cases the correct scope is used: using Accessors
struct Elements end
(::Elements)(x) = 2*x
o = @optic _ |> Elements()
@assert o(1) == 2 # works |
# Elements, Properties | ||
const ∗ = Elements() | ||
const ∗ₚ = Properties() | ||
IndexLens(::Tuple{Elements}) = Elements() | ||
IndexLens(::Tuple{Properties}) = Properties() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks much better than doing it at "parse time". Can you add a test where the user defines these symbols?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optionally we could add a layer of indirection and instead of overloading the IndexLens
constructor with something that is not an IndexLens, do this dispatch in another function index_like_lens
.
Oh I think I misread your code from the very beginning. The problem I imagined with symbols being pulled from the wrong scope might never have existed. Can you add a testcase like this: ∗= 3
f = @optic _[∗]
@test f([1,2,42]) == 42 and then it is good to go. |
And I also misread your "gotcha example", thinking that you suggest that Added tests. |
The x -> x.a + 1
@o _.a + 1
(@o _.a + 1) |
I also like it. |
Allows for shorter and much nicer to read:
@optic _[∗].a
instead of@optic _ |> Elements() |> _.a
,@optic _[∗ₚ][2]
instead of@optic _ |> Properties() |> _[2]
.