News for hackers. Stuff that matters.
Re:Rust vs. Haskell by qwertox on Tuesday February 14, 2023 @08:11AM (#34788769)
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.

Re:Rust vs. Haskell by Y_Y on Tuesday February 14, 2023 @08:29AM (#34788941)
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.

Re:Rust vs. Haskell by ghostwriter on Tuesday February 14, 2023 @08:20AM (#34788847)
> 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.

Re:Rust vs. Haskell by bryanlarsen on Tuesday February 14, 2023 @08:31AM (#34788968)
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.

Re:Rust vs. Haskell by nicoburns on Tuesday February 14, 2023 @08:57AM (#34789282)
You can use closures as functions with type inferred signatures
Re:Rust vs. Haskell by bryanlarsen on Tuesday February 14, 2023 @09:52AM (#34790126)
Closures capture variables so have significant differences compared to inner functions. Capture can affect lifetimes so the difference is significant in Rust.
Re:Rust vs. Haskell by nicoburns on Tuesday February 14, 2023 @11:52AM (#34792129)
Closures don't have to capture variables. If they don't then they're equivalent to functions.
Re:Rust vs. Haskell by bryanlarsen on Tuesday February 14, 2023 @12:00PM (#34792270)
That's what I thought too, but I fixed some compiler errors by turning a closure into an inner function. Probably a PBKAC, but...
Re:Rust vs. Haskell by LegionMammal978 on Tuesday February 14, 2023 @06:07PM (#34797530)
Perhaps it's due to the fact that you can't really have type-generic closures in Rust: once the compiler has inferred one type, the closure cannot be used with another type. In contrast, inner functions can have any number of additional type parameters.
Re:Rust vs. Haskell by thedataangel on Tuesday February 14, 2023 @08:22AM (#34788872)
In "serious" Haskell code, almost all top-level definitions (i.e. functions) are given types - for exactly the reason you specify.
Re:Rust vs. Haskell by Hirrolot on Tuesday February 14, 2023 @10:57AM (#34791212)
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.
Re:Rust vs. Haskell by dgb23 on Tuesday February 14, 2023 @11:21AM (#34791622)
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.
Re:Rust vs. Haskell by iudqnolq on Tuesday February 14, 2023 @12:27PM (#34792752)
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

Re:Rust vs. Haskell by kaba0 on Tuesday February 14, 2023 @01:38PM (#34793905)
Is 4 ‘a, ‘b, etc. really more readable?