9 ms·
Rust looks fantastic, and has a lot of things I wish I could do while in a higher level language like F#. I just wish Rust was a bit less verbose. Requiring, f
by MichaelGG 12y ago
Rust looks fantastic, and has a lot of things I wish I could do while in a higher level language like F#.
I just wish Rust was a bit less verbose. Requiring, for instance, type annotations on function arguments because it's sometimes helpful is such a weird decision. Let the programmer decide when an annotation is needed. This gets annoying when you get into functions with complex arguments. Especially for local functions where the signature could be messy, but the limited scope means annotations just clutter things. I'm not sure why Rust forces us to compromise here.
- steveklabnik 12y agoEven in languages with whole-program inference, it's generally regarded as best practice to write out the types of your functions, hence Rust's choice here. You're right that there's a tradeoff. In general, Rust follows 'explicit over implicit.' > This gets annoying when you get into functions with complex arguments. Have you seen the where clauses yet? This should significantly help complex function declarations.
- MichaelGG 12y agoI just don't understand why there has to be a tradeoff. I just don't get why the compiler should decide on such a large thing, instead of letting the programmer do it. One can always be more explicit if one feels they're getting value from it. If someone wants to write a bunch of terse code, why stop them? Does the compiler gain a large benefit from not having to include this feature? Who loses by allowing users to do what they want? Comparing C# and F#, the extra annotations change the frequency in which I'll introduce an inner (local) function. For instance, here's a little helper in a piece of code I'm writing at the moment. It's part of a larger function and isn't exposed beyond 5 lines of code. let runWithLog f name = try run f name with ex -> logExn ex Used in: runWithLog "sessions" collectSessionStats runWithLog "visitors" collectVisitorStats Having to add "(f: string -> RunParams -> Whatever -> unit) (name: string)" almost doubles runWithLog helper yet provides no benefit. And this an extremely simple case! Once the arguments are generic, higher-order functions themselves, it gets quite noisy. Sure, if it's a top-level export, then maybe annotating is a good idea. But if it's limited in scope then what's the harm? Not that it'll change when I use Rust - there's nothing competing in this category. It'd just be nice if the language let the user decide on the style.
- burntsushi 12y agoRust will do type inference on lambdas for you. :-) fn fubar(x: uint) { let times = |n| x * n; println!("{} * 5 = {}", x, times(5)); } Rust only enforces type annotations on top-level functions. > Does the compiler gain a large benefit from not having to include this feature? Who loses by allowing users to do what they want? FWIW, I feel precisely the opposite as you. I'd rather have an ecosystem of code where top level functions must be annotated by types than an ecosystem where types are there only if the author feels like adding them. There is a small cost but a large gain, IMO.
- MichaelGG 12y agoOh if nested functions don't need annotation, then I suppose that saves most of the problem. Can top-level definitions be of the lambda form? If not, what's the reason to have separate ways?
- burntsushi 12y agoThere are `fn` types---which are just function pointers---and there are `||` types, which are closures that correspond to a pointer to a function and an environment. You can actually define `fn`'s within other `fn`'s, but their types are not inferred. Closures cannot be defined at the top level, presumably because there is no global environment to capture. (Rust does have global "static" constants, though, which are available everywhere in the enclosing module I think.) I can probably hazard a few guesses at why there is a split here (probably relating to lifetimes), but I don't know for sure. Perhaps someone else will chime in.
- MichaelGG 12y agoBut if a closure doesn't capture any variables then how it is different?
- smosher_ 12y agoThe immediate difference is the body of what you need to infer against. In the local case it's quite small, and the compiler doesn't have to worry about a lot of what-ifs. Personally I like seeing types and I'm glad people are forced to write them. (This is an opinion I've had long before Rust existed, so I'm not rationalizing excuses.)
- pestaa 12y agoFully typed function signatures form a kind of contract you can program against. Haskell and other extremely strongly typed languages can infer the types of function parameters, yet the community still agrees it is good practice to annotate your work.
- MichaelGG 12y agoWould Haskell be better off if the compiler enforced this community agreement, instead of letting users decide? Also, the type annotations can be added on later. While you work and play with ideas, leave everything unannotated. After it's cemented and perhaps refactored a bit, add the "contract". In Rust, even while working things out, the user has to figure out and jot down the types.
- burntsushi 12y ago> Would Haskell be better off if the compiler enforced this community agreement, instead of letting users decide? Unequivocally, yes. My logic is that, while writing the function may be slightly quicker and more convenient if you can leave off the type, reading that same code is made at least an order of magnitude easier if the type annotation is sitting there in the code. Actually, it gets better than that. Writing down the type of a function before writing the function often helps you write the function. Protip: Use `ghc` with `-fwarn-missing-signatures -Werror`, or even better, `-Wall -Werror`. :-)
- MichaelGG 12y agoCertainly the user should be allowed to decide when they feel the code needs further documentation to be readable. Trying to drag people into something seems unfriendly.
- dbpatterson 12y agoThe thing is, with current tooling, the current design means that I can write a function, type a key combination, and have the type signature printed in a buffer (where I can then copy it into place). And until I do that, there is warning highlighting on the function. Now in some ways this may seem silly - are you really going to understand code without understanding types? But especially for people new to the language, _or_ when you're dealing with new libraries (if you've ever written a little wrapper around a function from a complicated library, you know what I mean), it's nice to choose whether you want to work from values or from types (where undefined is your friend). Which isn't to say that think rust's decision is bad, just that having flexibility makes this kind of tooling easier (and I'm assuming here that in all cases, the end result will be all annotated top level functions). And, part of rust's choice was probably to make type checking easier, which is an important thing (especially given how sophisticated the borrow checker is).
- pcwalton 12y ago> Requiring, for instance, type annotations on function arguments because it's sometimes helpful is such a weird decision. In separate compilation you have to annotate functions anyway, and in most large ML projects I've worked on people tend to annotate just because the error messages get much better. This is a common design in functional languages these days.