4 ms·
One pattern that I've seen in recently-designed programming languages is to make type declarations mandatory for function arguments and return values, but to ma
by ekidd 4y ago
One pattern that I've seen in recently-designed programming languages is to make type declarations mandatory for function arguments and return values, but to make them optional for local variables. In other words, these languages do not attempt to perform program-wide inference.
One big advantage of this approach is that function types and return values are always documented in the source. But another advantage is that when type inference needs to look at many functions at once, then type errors are frequently misleading and they are reported far from the cause. By requiring parameter and return types to be declared, type inference will always report errors in the correct function.
It does also help to have a good IDE. rust-analyzer, for example, can show the inferred types of local variables, or even automatically insert the inferred type into the source code.
- withinboredom 4y agoSo basically PHP (except for the “requires” part)? It does work quite well.
- pron 4y agoYep, that's what Java (and Java IDEs) do.
- dllthomas 4y ago> type inference will always report errors in the correct function. Kinda! If you have a function calling another in apparent error, there are two functions that could be "the correct function" - maybe the call is wrong, or maybe the type signature on the called function is wrong (or potentially any of several called functions whose types interact in the calling function). Now certainly you can report all of these locations, in which case the statement I quoted is true, but note that in principle you can also report all of the potentially involved locations when you have omitted some of the function type signatures. You are very much correct that limiting the distance errors can drift is important in many contexts, and personally I strongly favor the "functions must have a signature" before a task in the code base is considered "done" (typically before merging to a shared repo), but I don't know that enforcing it in the language is the right call - deferring it during quick iteration until I've actually hit a potentially relevant type error is comfortable in Haskell, and in any case one has to be ready to add type signatures to better localize errors within a function.
- Koshkin 4y agoNitpick: the type of the returned value usually can be inferred from the 'return' statement.