I wish I could tell my compiler like "This argument will always be in this range in practice, so please optimize accordingly, but also blow up if you realize that I forgot to make sure that this is actually the case anywhere in the program"

Follow

@rq

Why doesn't checking if it's in the range at the beginning and blowing up if not do what you want?

@robryk@qoto.org String patterns. Let's say function f processes a string and expects certain patterns and length. I wrote it half a year ago. It cannot fail on valid input.

Now, I put
f somewhere else, but I forgot to put code that validates it's valid input for f (because at the original calling site, the input was always valid, so I had nothing to check).

The alternative is that every calling site has to error check or I have an unhandled return value or, worse, I have to do multi-return in a language that only has one return

@rq What did you mean by blow up? I thought you meant safely crashing. If so, you can add the verification at the beginning of the function itself and crash there.

@robryk@qoto.org I mean that the compiler throws a compiler error so I don't have to find out at runtime my assumptions were wrong

@rq What would you expect the compiler to do in the desired world if it couldn't determine whether the assumption is always held?

@robryk@qoto.org Yell at me to add code to ensure the assumption is always held or to add a compiler hint if I know it'll always be held

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.