fosstodon.org is one of the many independent Mastodon servers you can use to participate in the fediverse.
Fosstodon is an invite only Mastodon instance that is open to those who are interested in technology; particularly free & open source software. If you wish to join, contact us for an invite.

Administered by:

Server stats:

10K
active users

I wrote about good type design in Rust.

It applies to any language with algebraic data types. (structs and enums) Please let me know what you think.

Just BeingDesigning effective types in Rust | Just BeingOne principle that determines which type pattern is better.
Dusty Pomerleau

@bshankar I think if you have a function that takes an enum, and the function is only valid for some variants of the enum, then the type needs to be reworked. For example, if you used newtypes instead of enum variants, then you could tie the ones that are expected to have an email together with a trait. And then your function to update the email can be generic over the trait, and you no longer need to return Result there.

@dpom That's true. I was too occupied with making up an example to show that the enum needs pattern matching. I didn't realize how silly the example ended up being.

I will give modify the example. Thank you for pointing it out!

@bshankar I don't think the example is silly, I go through this exact progression all the time: Struct with bools or Options, fix it by switching to an enum, realize that my enum variants are really distinct types, switch to a trait-based solution. Some day I'll learn to skip to the end :)

@dpom I meant just the example function that returned Err for some enum variants was silly. I made some corrections to the post. 🙂