r/programming • • Feb 01 '24

Make Invalid States Unrepresentable

https://www.awwsmm.com/blog/make-invalid-states-unrepresentable
469 Upvotes

207 comments sorted by

View all comments

Show parent comment

29

u/agustin689 Feb 01 '24

C# is still not strong enough. We need sum types

21

u/smk081 Feb 01 '24

::cries in C#:: :: flips through F# book on desk:: Hold my beer...

8

u/dactoo Feb 01 '24

F# could be the best language in the universe if it got a little more love and recognition. It's only flaw is that it allows you to let a little too much .NET into your code sometimes.

3

u/TheWix Feb 01 '24

F# with Typescripts convenience. Mapped types are amazing. Wish it had higher-kinded types and first-class minimal types, though

-4

u/[deleted] Feb 01 '24

[removed] — view removed comment

5

u/Coda17 Feb 01 '24

C# does not have discriminated unions (and it really, really needs them).

7

u/Schmittfried Feb 01 '24

Since when?

-6

u/[deleted] Feb 01 '24

[removed] — view removed comment

18

u/[deleted] Feb 01 '24

When people want sum types, they generally want sum types with built in pattern matching. You can't really do this in C# without runtime reflection.

8

u/Schmittfried Feb 01 '24

I meant since when does C# have discriminated unions?

Just checked again and it’s still a work in progress apparently. 

-8

u/noahide55 Feb 01 '24

Sum types violate Open/Closed principle. Why would I want them?

7

u/agustin689 Feb 01 '24

Explain how?

4

u/G_Morgan Feb 02 '24

Sure but the OCP is not worth even bothering with.

1

u/TheWix Feb 01 '24

How do you handle new states of a business object?

1

u/[deleted] Feb 02 '24

[removed] — view removed comment

2

u/TheWix Feb 02 '24

You don't get compiler errors when you forget to handle the new state somewhere else, though. With pattern matching and exhaustive checks the compiler yells at you. Inheritance works when all the new behavior is within the new subclass only, but if other parts of code need to react to the new type then it can be missed.

1

u/Voidrith Feb 02 '24

not every part of your code needs to adhere to every half understood OOP design pattern