4 ms·
According[0] to Perplexity.ai, you could use std::optional<std::monostate> to get a C++ approximation of your Rust type. I am neither an expert in modern C++ n
by acka 2y ago
According[0] to Perplexity.ai, you could use std::optional<std::monostate> to get a C++ approximation of your Rust type.
I am neither an expert in modern C++ nor in Rust, but I have witnessed enough of C++'s evolution over time to know that if C++ language devs find a feature desirable enough they will do whatever it takes to frobnicate the language in order to claim support for that feature.
[0] https://www.perplexity.ai/search/is-it-possible-Sd3TML68TfKveHzIkoGTNA https://www.perplexity.ai/search/is-it-possible-Sd3TML68TfKv...
- pjmlp 2y agoWhile I watch with some desmay, one of favourite languages turning beyond PL/I levels of complexity, it isn't alone in this direction. One of the reasons I am not able to follow up on C++ as much as I did in the past, isn't directly related to its complexity, rather that my main worktools, the JVM, CLR and Web ecosystems, are reaching similar levels of complexity, specially with the 6 months release candence, and there is only so much one can keep up with.
- tialaramex 2y agostd::optional<std::monostate> has two values, Option<Infallible> has one, so by my counting that's a 100% error. It is likely the best that can be done, but that's my point, C++ can't do this because the foundational type system isn't up to the task.
- GrantMoyer 2y agostd::monostate is a "unit type"[1]; there's only one value with with type monostate (the value is std::monostate{}), so all monostate values are equivalent. Infalible is a "empty type"[2]; there are no values with type Infalible, so a value cannot be constructed, so Optional<Infalible> is always None, never Some(infallible). Importantly, the compiler knows this and can use it to reason about the correctness of code. C++ has no empty types. Void is close, but it's sometimes used where a unit type would be used, and anyway it's not a first class type. For example, you can't use std::optional<void>. Even if it were possible to make an empty type in C++, it wouldn't give you anything, because the compiler isn't equipped to reason about them. BTW, the rust equivalent to std::optional<std::monostate> is Optional<()>. The empty tuple is Rust's idomatic unit type. [1]: https://en.wikipedia.org/wiki/Unit_type https://en.wikipedia.org/wiki/Unit_type [2]: https://en.wikipedia.org/wiki/Empty_type https://en.wikipedia.org/wiki/Empty_type