4 ms·
For json marshalling, I would defer to the underlying type's marshalling if it's present, and return `null` otherwise.
by wtfishackernews 5y ago
For json marshalling, I would defer to the underlying type's marshalling if it's present, and return `null` otherwise.
- shhsshs 5y agoI think this is appropriate in some cases but not others. For example how does the JSON value distinguish between `Some(null)` and `None`?
- dialogbox 5y agoIMO using Optional type means the inner value must be not null. And if it's Some(null), it should mean exactly same as None.
- uryga 5y agodistinguishing `Some(null)` and `None` is often considered a feature of Optional ;) to use a tired example: when getting a value out of a map via some `myMap.get(key)`, you may want to distinguish "not present" = `None` and "present, with value null" = `Some(null)` the right solution is to just not have nulls in the first place, then there's no problem ;)
- alophawen 5y ago(Assuming we are discussing the Option type from rust) Some(null) is not a valid result. The whole point of the Option type is to let you know: a) we got a result: Some(value) b) there is no result: None
- andrewjf 5y agoIt's trivial to store a null pointer in an Option::Some(). struct Foo; fn main() { let option_with_null: Option<*const Foo> = Some(std::ptr::null()); dbg!(option_with_null); dbg!(option_with_null.is_none()); } Output: [src/main.rs:6] option_with_null = Some( 0x0000000000000000, ) [src/main.rs:7] option_with_null.is_none() = false https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6b87832bf48f4115ae44504e497287c4 https://play.rust-lang.org/?version=stable&mode=debug&editio...