by conradev · 8y ago · 0 comments
Serde, an equivalent third-party crate in Rust, supports a lot of customization which I find invaluable. It can (de)serialize values like this with ease:
{"type": "location", "value": {"latitude": 0, "longitude": 0}}
in a very small amount of code: #[derive(Serialize, Deserialize)]
#[serde(tag = "type", content = "value")]
#[serde(rename_all = "lowercase")]
enum Value {
Location { latitude: f64, longitude: f64 },
String(String),
...
}
Serde also supports customizing serialization on a per-field basis without having to implement the entire protocol, which is nice: #[derive(Deserialize, Serialize)]
struct Record {
#[serde(with = "chrono::serde::ts_seconds")]
updated: DateTime<Utc>
}
I really hope that Swift has better ways (like the above) to customize Codable in the future. I find myself implementing the protocol myself in 90% of cases, whereas I very rarely have to do that for Serde.If it wasn't for a blog post that gave some simple examples, I couldn't even figure it out myself.
Possibly not fair to compare them this way, though. No clue what the trade-offs are.
https://github.com/apple/swift-evolution/blob/master/proposa...
https://github.com/apple/swift-evolution/blob/master/proposa...
I've heard many people on the mailing lists talk about wanting to add hygienic macros–so when you're talking about "full-fledged macros", are you talking about unhygienic or hygienic ones?
> See Joe Groff's talk for details
I'm actually curious how much is possible, in terms of reflection capability, both currently and in the future. How close can I get to the dynamism of say the Objective-C runtime? Can I grab a function pointer knowing its mangled name? Can I list every class in a binary by consulting the runtime metadata? Can I "swizzle"?
Well, right now the compiler emits a lot of metadata that is mostly used for runtime generics, dynamic casting, and the (somewhat limited) Mirror type. But these features have enough generality that a surprising amount of stuff has to be encoded. However there’s no nice API for looking at it yet.
> Can I grab a function pointer knowing its mangled name?
That’s just dlsym().
> Can I list every class in a binary by consulting the runtime metadata?
The metadata is there, but there’s no exposed API for doing this.
> Can I "swizzle"?
There’s an experimental thing for this now: https://github.com/apple/swift/pull/20333