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

haslens analog tohasfield #145

Open
goretkin opened this issue Jul 9, 2020 · 2 comments
Open

haslens analog tohasfield #145

goretkin opened this issue Jul 9, 2020 · 2 comments

Comments

@goretkin
Copy link

goretkin commented Jul 9, 2020

I didn't see an existing way to check whether get(obj, l) would return a value or throw any or all of

ERROR: type $obj has no field $f
BoundsError
ArgumentError: invalid index: $i

etc.

It could be nice to have a function or two for this. Possibly two so that one can do all the "static" checks---no bounds checking on AbstractArrays with non-inferrable size, or keys in Dicts)

@jw3126
Copy link
Owner

jw3126 commented Jul 9, 2020

Thanks! I agree it would be nice to have this. I think this is basically Base.hasmethod(get, Tuple{typeof(obj), typeof(lens)}). Sadly that is not static at the moment. I also think the lens case is no easier then the general Base.hasmethod. Just think about FunctionLens. Also ComposedLens seems pretty hard and it is absolutely essential. So I see no other way than wait for some bright day, where hasmethod is static.

@tkf
Copy link
Collaborator

tkf commented Jul 10, 2020

Is it important that you can do haslens or is it OK to have a non-failing lens? As discussed in #65, we can have a lens that returns Union{Some{T},Nothing} to express possibly non-existing locations. A cool feature is that we can also use this to remove the location as well (which, of course, works only with something like Dict and NamedTuple).

I guess we can have a lens transformation to automatically lift a lens to the safe/"maybe" version. Something like get(obj, maybe(@lens _.a.b[:c])).

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