515
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
this post was submitted on 29 Aug 2024
515 points (98.5% liked)
Linux
48655 readers
487 users here now
From Wikipedia, the free encyclopedia
Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).
Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.
Rules
- Posts must be relevant to operating systems running the Linux kernel. GNU/Linux or otherwise.
- No misinformation
- No NSFW content
- No hate speech, bigotry, etc
Related Communities
Community icon by Alpár-Etele Méder, licensed under CC BY 3.0
founded 5 years ago
MODERATORS
My favorite, as that is the exact point made by anti-rust people.
What kind of type signature would prove the first block of any directory in an ext4 filesystem image isn't a hole?
The problem isn't that the block is a hole. It's that the downstream function expects the directory block to contain
.
and..
, and it gets given one without because of incorrect error handling.You can encode the invariant of "has dot and dot dot" using a refinement type and smart constructor. The refined type would be a directory block with a guarantee it meets that invariant, and an instance of it could only be created through a function that validates the invariant. If the invariant is met, you get the refined type. If it isn't, you only get an error.
This doesn't work in C, but in languages with stricter type systems, refinement types are a huge advantage.
Wouldn't it still crash when the smart constructor was called?
If it were poorly designed and used exceptions, yes. The correct way to design smart constructors is to not actually use a constructor directly but instead use a static method that forces the caller to handle both cases (or explicitly ignore the failure case). The static method would have a return type that either indicates "success and here's the refined type" or "error and this is why."
In Rust terminology, that would be a
Result<T, Error>
.For Go, it would be
(*RefinedType, error)
(where dereferencing the first value without checking it would be at your own peril).C++ would look similar to Rust, but it doesn't come as part of the standard library last I checked.
C doesn't have the language-level features to be able to do this. You can't make a refined type that's accessible as a type while also making it impossible to construct arbitrarily.
You can do that in C, too.
You're going to need to cite that.
I'm not familiar with C23 or many of the compiler-specific extensions, but in all the previous versions I worked with, there is no type visibility other than "fully exposed" or opaque and dangerous (
void*
).You could try wrapping your
Foo
inBut nothing stops someone from being an idiot about it and constructing it by hand:
Or even just casting it.
Yes, this is like not checking an error code.
That's not the point, though. The point is to use a nominal type that asserts an invariant and make it impossible to create an instance of said type which violates the invariant.
Both validation functions and refinement types put the onus on the caller to ensure they're not passing invalid data around, but only refinement types can guarantee it. Humans are fallible, and it's easy to accidentally forget to put a
check_if_valid()
function somewhere or assume that some function earlier in the call stack did it for you.With smart constructors and refinement types, the developer literally can't pass an unvalidated type downstream by accident.
I don't know if the type system proves it's not a hole, but the type system certainly seems to force consumers to contend with the possibility by surfacing the outcomes at the type system level. That's what the
Either
is doing in the example's return type, is it not?