Daily Thought - 2025-02-25
Hey, I'm Hanno! These are my daily thoughts on Crosscut, the programming language I'm creating. If you have any questions, comments, or feedback, please get in touch!
I'd like to introduce the concepts of structural and nominal typing. I'm going to talk about some aspects of the type system next, and this is going to be required background. Let's start with structural typing. Consider this function in Rust:
fn measure_width() -> (Value, Unit) { ... }
It's a function that returns two values. Except, a function in Rust can only return one value, really, so I've bundled those up into one tuple. Please note that this tuple is an anonymous data type. I just wrote down its structure here, the data it contains, without giving it a name.
I can define another function that's similar:
fn measure_height() -> (Value, Unit) { ... }
The return type looks the same.
And not only do they look the same; in a very real way, they are the same:
let width = measure_width();
let height = measure_height();
let max_dimension = if width > height {
width
} else {
height
};
This works. The type of max_dimension
is (Value, Unit)
, which is also the
type of either of those return values. Even though we've defined that type in
two locations (and could define it in more), it's the same type. Because it's
the structure of the type that counts. This is structural typing.
Hey, you! Want to subscribe to my daily thoughts? Just let me know (maybe include a nice message, if you're up for it), and I'll send you an email every time I post a new one.