Comment by poly2it
2 days ago
You might consider it type safe according to C's rules, but it is not type safe according to contemporary opinion because the success type and error type are the same.
2 days ago
You might consider it type safe according to C's rules, but it is not type safe according to contemporary opinion because the success type and error type are the same.
> [...] because the success type and error type are the same.
That's a bit weird? In Rust, if you have an thing that returns Result<String, String> that's still type safe, even though success and error types are the same.
I think I understand what you want to express, maybe. You might say it's in-band vs out-of-band error signalling?
> In Rust, if you have an thing that returns Result<String, String> that's still type safe, even though success and error types are the same.
These types are not the same:
Those are not types at all. Ok and Err are type constructors for the Result<A, B> type that take arguments of type A and B respectively.
4 replies →
The success and error types are not the same, because they are in different positions in the type, and so distinguished by different patterns.
If you have literally the above convention where a valid result is a String, and an error result is also a String (e.g. error message) that is still type safe.
The success type and error type having to be different is not a good way to express the idea; rather that success and error cases are distinguished by type (even if the success and error data units are of the same type).
In other words, the "Ok" and "Err" entities are an ingredient of the type and that effectively makes the result and error be distinguished at the type level.
But we could also argue that Rust still allows you to write an integer-returning function such that -1 means error.
Basically, you need the whole 'success and error types are different' in a language like Python if you make the return type something like "int | string", where 'string' stands for your error message.
Of course that breaks down, when the success type is also an error message, eg when you are looking up something in a catalogue of error messages.
Rust has no trouble with this, for exactly the reasons you line out.
Contemporary opinion among who? Words mean things, and "type safe" simply does not mean "agrees with my preferred style of error reporting"
Papers on type soundness from the last 30 years usually explore the domain further than C semantics.
Soundness means that the program behaves in accordance with what its types express. You're right that C may be unsound due to undefined behaviour, but I don't think this is about soundness. What you're saying is that papers on types show how you can use types to express more constraints.
4 replies →
Can you link some?
That's not the same as asserting that soundness requires adherence to the current fashion, sum types over results and errors. TIMTOWTDI.
You don't have to think C is sound or good to distinguish a disfavored API style from mathematical unsoundness. There are infinitely many type safe APIs and languages that are, despite their soundness, ugly and hard to use.
Let's not project every dimension along which a language can be good or bad onto the axis of safety.
Opinion among people who know better than to try to authoritatively explain subjective words with rigid dogma, because that just makes them look silly.