Hacker News new | ask | show | jobs
by qwertox 1221 days ago
About Haskell's function type annotations: "But it usually results in a warning, and adding a type signature is a good practice."

I prefer Rust's way of doing this. When you're a beginner it ensures that you're passing and returning the proper type to and from the function, you kind of have the function as a guard which ensures that you're using the correct types.

5 comments

> it ensures that you're passing and returning the proper type to and from the function

You don't need it for all function declarations though, there are many trivial cases where type signatures don't add value. Consider that you probably would prefer most of the variables within a function to be inferred for you by the compiler. A similar thing could be said about the most of the functions within a given module. Important interface methods can be defined explicitly though, for extra clarity and self-documenting purposes.

pub functions: should be explicit

module functions: if I chose, I'd let them be derived, but I'm relatively indifferent

inner functions: should be allowed to be derived, IMO. They're very infrequently used so this choice doesn't have much impact.

You can use closures as functions with type inferred signatures
Closures capture variables so have significant differences compared to inner functions. Capture can affect lifetimes so the difference is significant in Rust.
Closures don't have to capture variables. If they don't then they're equivalent to functions.
That's what I thought too, but I fixed some compiler errors by turning a closure into an inner function. Probably a PBKAC, but...
I prefer to have a choice. You could have a compiler that doesn't run it you haven't eaten a balanced breakfast either. Haskell's type system is designed around type inference (of the Hindley-Milner kind), if it can infer the type I'd like to have the option to let it do so.

It's certainly good practise to write out your types, and often your editor can do it for you, but there are tons of little places where it's a waste.

In "serious" Haskell code, almost all top-level definitions (i.e. functions) are given types - for exactly the reason you specify.
I find type errors in HM typing systems can sometimes be quite obscure and misleading, due to omnipresent type inference. Writing type signatures can save you from that. For example, even if the type signature is quite "obvious", you can erroneously plug in some other type into the function, which triggers the type system to underline the function being defined instead of the function call.
Rust only half-asses this though. Lifetime annotations are part of the type of the function but can be elided if unambiguous. For beginners and generally people who read code that's confusing. I don't think there's anything to be gained by writing a few less characters and editors can easily add them as well.
There's crucial nuance here.

Lifetime annotations aren't elided based on there being only one unambiguous answer. Rather, there's three simple rules[1] that look at the function signature and in simple cases give what you probably want. If those simple rules don't match your use case you need to define manually, the compiler doesn't get clever.

This means that if you want to take a reference to an array and return a reference to an item in an array, for example, elision will work fine. But if you take a reference to a key and look up a reference to the value in a global map you need to write it by hand, even though the compiler could pretty clearly guess the output lifetime you want.

This preserves a crucial feature: you can know the exact signature of a function by only looking at the signature definition, you don't need to look into the body.

Lifetime elision isn't like inferring argument types. It's like defaulting integer literals to i32 unless you specify otherwise.

See https://doc.rust-lang.org/nomicon/lifetime-elision.html

Is 4 ‘a, ‘b, etc. really more readable?