by kstenerud · 2y ago · 0 comments
Oh yuck, I don't like that.
Function signatures are for HUMANS to read. Sure, the compiler can reach into a function implementation to glean what it returns, but that's no reason to force a human reader waste time on it as well.
"I'll accept this and this from you and then return that kind of thing to you."
vs
"I'll accept this and this from you and then return something to you that your supervisor will tell you if you're using correctly or not - or you could watch me as I work to see what it is I've made for you."
As I said in another comment, as the compiler knows the type of everything, it would be easy to have the LSP overlay the type (or annotate it into the text via code action for you).
Luckily Gleam programmers do write return annotations in practice.
But I agree that specifying the returns explicitly is a bit nicer when reading the source, don't think it's a big issue that it's optional though.
If you were forced to specify the return type, your original intent becomes clear for all (rather than leaving it to a machine that can only assume the narrowest scope).
If the return type changes, the code using the function should definitely blow up until it's changed, don't think that's a bad thing.
This. The big upside of algebraic data types. The function won't blow up though, the compiler shows you every piece of code where the added type needs to be handled and therefore ensures the program runs correctly.
IntelliJ (the best Scala IDE) now has something called X-Ray mode, see the videos here: https://blog.jetbrains.com/scala/2023/12/21/the-x-ray-mode/
Alternatively, it also allows you to configure it to always show the inferred types on functions.
Before, I annotated things a lot, but after having this I've never felt the need to annotate functions ever again. I still do it for certain things though but for other reasons than readability (e.g. for type-driven development).
The cases where I think it is necessary to annotate, I can do so.
Worth noting that that can be reactive, too. If an type checking error message is confusing or insufficiently localized, pinning down a few things around it can clarify.
As tome says, though, that can be a thing in Haskell too. There exist situations in Haskell where the compiler cannot figure out the types for you, but they're not most code.
Nope, exactly the other way around. Function signatures are to help machines optimize your code. What humans should care about are names. It's just that we've normalized an extremely misguided practice of abusing type systems as a crutch for non-descriptive variable and function names.
// For example, this:
fn get_driving_requirements(vehicle: Vehicle) -> String {
...
}
// Is just so much nicer than this:
fn get_driving_requirement_description_from_vehicle(vehicle) {
...
}
This is nicer for me (a human) to read, even if the compiler can infer the types of both.And maybe we just abuse formal logic because we've forgotten how to write good prose.
The little bit of time it takes to annotate the type that the function returns will save a lot of time when other people take over the codebase.
Humans are not great compilers and spending time try to figure out what the hell is returned from a function
That's why I leave it to the compiler and don't try to do it as a human.
Either way, I use (and honestly love) python on a daily basis and I wish my gripes with it were on this small a level. Yes, I'm looking at you, passed by assignment and late binding closure.
Guess it's not working 100% if someone feels this way! However, I would say that since the compiler/LSP knows the full type of every function it should be possible to have a code action that says 'annotate the type of this function for me'.
I want to know what your function COULD produce, not what it happens to produce now. Type inference can only show that in every current path you return a TYPE_X, and that works fine until one day you return an instance of its supertype TYPE_A, and now my code blows up because I misunderstood the contract. You may have always intended for it to return TYPE_A and its subtypes, but you never signaled that intent to others. This is why enforcing explicit contracts is necessary.
I agree that packages authors really should annotate their types though, and possibly that they ought to be forced to do so, just to make reading the code easier.