"Rek" wrote:
... For example: returning invalid instead of a real Integer when method is "as Integer" will crash -- making it clear that the incorrect type was returned from that specific method. Otherwise you could end up seeing the crash in some other code that relies on the poorly behaving function and have to trace it back to the source yourself.
Just to be clear, "crash" above is a metaphor for runtime error "Type Mismatch", not something else (like the famed player crash-reboots). You want it to happen at the earliest "checkpoint" because that way it is closer to where something bugged out and makes your life easier to discover/fix it. Function entrance and exit being the "check-points" here. Say you write
function celsius_to_fahrenheit(C as float) as float
Seems pretty clear, right - float enters, float exits - could not be anything else. And the executor will keep you honest - if something else tries to enter/exit, it will sound the alarm. Which is better than getting some weirdness happen (not even runtime error) five minutes later and thousands of lines away.
Yup, declaring types for some of function's parameters and return value is a Good Thing. Think of this feature as of guard rails (traffic barriers) on a road:
- They don't have to be present but often are good idea.
- Will they make you arrive faster, no.
- In a way they "stand in your way" but that is not the way you want to take anyway
- By stopping you they may save your life (and in the case of a central divider they prevent cluster-fractals from happening).