-
Notifications
You must be signed in to change notification settings - Fork 61
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
more ergonomic property access #157
Comments
Thinking about it a bit more... though using generics is a neat trick that allows us to only add one method to For example, in geojson, there's only a few numeric types supported "natively" - u64, i64, and f64. I think it would be potentially surprising to folks if this worked:
While this gave an obscure error about
Maybe it'd be better to just hardcode the type into the method name - without generics like:
And for Copy types here, it's probably reasonable to have non-consuming accessors like:
While for non-copy types like Array, something like:
So, all in all this could be a dozen or so new accessors. |
Another awkward thing I just realized: While this works:
It does not work if we interact with the geometry first, since
This makes me wonder if any ergonomic accessors should be on e.g. |
I don't have anything substantive to add here, but I agree the ergonomics of property retrieval is difficult and needs some love. At the very least, the user should not have to pull in |
What about |
Thanks for weighing in!
Right, this looks nice, but there are a couple of complications that I'm hung up on which aren't clear from your example: Given:
Complication 1: non 64 bit numbersjson crate, in keeping with javascript, only deals with 64 bit numbers. Do we do likewise so that: This compiles: Or do we do an implicit coercion and silently change the precision assumptions from the json crate? Without implicit coercion, we'd only implement this feature for 64bit numbers, and I find the discoverability of generic impls to be really hard, especially for people first picking up a crate. This is why I was advocating for the more manual approach of implementing something like If we were to go one of the two other directions, I think I prefer the downsides of implicit conversion rather than inscrutable compiler errors because someone is trying to parse out a f32. Complication 2: non-copy typesYour example shows a copy type, which is pretty straight forward, but what about non-copy types? I guess these would have to be references?
But it seems like we'd want a way to have non-reference types, and avoid unnecessary cloning. That's what I was getting at with the
|
On Fri, 01 Jan 2021 18:32, Michael Kirk ***@***.***> wrote:
This compiles: `Feature::property_as::<u64>("key")`
And this fails to compile?: `Feature::property_as::<u32>("key")` due to
missing impl on u32?
I planned to implemente it for `u8`/`u16`/etc using some sort of implicit conversion. But I'm unsure.
Or do we do an implicit coercion and silently change the precision
assumptions from the json crate?
Without implicit coercion, we'd only implement this feature for 64bit
numbers, and I find the discoverability of generic impls to be really
hard, especially for people first picking up a crate.
“Discoverability” of a API is important. Could that be addressed with lots of doc comments showing all the options?
This is why I was advocating for the more manual approach of
implementing something like `u64_property("key")`,
`i64_property("key")`, `f64_property("key")`. I think it's more
discoverable and avoids implicit conversion.
If we were to go one of the two other directions, I think I prefer the
downsides of implicit conversion rather than inscrutable compiler
errors because someone is trying to parse out a f32.
But it seems like we'd want a way to have non-reference types, and
avoid unnecessary cloning. That's what I was getting at with the
`remove_*` methods in my example, since I think we could uniformly
*move* the underlying value regardless of type:
Yes, a non-copy alternative should exist too, for strings, arrays & other objects.
|
Examples would definitely help, but I'm less worried about discoverability if the trait is uniformly implemented for every type the user is likely to use. I was more worried if we base our implementation on TryFrom and the serde_json crate doesn't want to implement TryFrom for non 64bit types. |
Unfortunately this idea was initially turned down on the I have made a |
Here's some mostly real code I wrote today... I was surprised at how verbose the property accessing code needed to be.
It's been a while since I've used this crate - is there some better way I should know about?
From what I can tell - we're basically plumbing along the interface of the json crate. But I was toying with an alternative:
My approach so far entails something like:
Other thoughts: we already have a nice quick_collection method for converting a geojson to a geo collection wholesale, but that doesn't allow you to access the properties if you want to build up some structs like:
I'd love to be able to write something like:
WDYT?
/cc @rory
The text was updated successfully, but these errors were encountered: