3 ms·
I’ll disagree a bit here. There are situations where absence of (more) dynamic typing can make your life harder than it already is. An example of this is gener
by PartiallyTyped 3y ago
I’ll disagree a bit here. There are situations where absence of (more) dynamic typing can make your life harder than it already is.
An example of this is generating a JSON and only learning the correct schema at runtime.
Or connecting to an external database with tables of unknown structures that you can’t type check.
You can, of course deal with all these by wrapping the objects in some structure; but it’s just one more thing you need to keep in mind.
- Permik 3y agoNothing in rust actually limits or makes it any harder to handle something "more dynamic" like JSON. In rust you just make a JsonValue enum that represents the values and the data types that you're handling. In something like Python, you just use the built-in runtime value type to represent the structure. Either way you still have to plumb up the data ingestation and your codepaths that you want to use the dynamic data in. For example you still have to check if the "name" key's value is a string and not an object, array etc. Otherwise, you probably could just use some accessors that access the values willy nilly that coerce them to text, return an error or panic, that could also exist in rust (some of these things you would have to wire up manually, some are provided by the language and others are fixed by just annotating structs.) To summarize, fail to see the point you're trying to make, do you have some specific example you are thinking of that I can take a look at?
- PartiallyTyped 3y agoYou made my point, in that it is a slightly more involved process over say Python. I didn't say that it's a "problem" or "impossible", just that it is making your life slightly harder. Something that I am interested in doing is generating a json schema at runtime, and then parsing some incoming json to see if that matches the target schema. Do you know of any crate already providing this kind of functionality?
- Permik 3y agoWould something like Valico fit the bill? https://crates.io/crates/valico https://crates.io/crates/valico When looking for crates lib.rs is an excellent search utility for searching crates that you would need. For example: https://lib.rs/search?q=json https://lib.rs/search?q=json
- PartiallyTyped 3y agoTIL lib.rs exists :D I think it fits the bill. I was doing this on python side and aimed to use Pydantic and this looks close enough.
- gaganyaan 3y agoI haven't found dynamism like that to be a problem. It's slightly more verbose, but not by much. Instead of `foo: int = unknown['foo']` like in Python, you do `let foo = unknown.get_u64('foo')?` (or something like that) in Rust with `serde_json`.
- PartiallyTyped 3y agoI didn't claim that it's a problem, just making your life slightly harder and needing to enumerate all enum cases in e.g. serde_json.
- junon 3y agoYou don't, though? I'm not sure what kind of code you're referring to. Can you give an example?