4 ms·
I want more implicit typing in typed languages. Quite often the compiler knows exactly what type a function will return, but I still need to write it there. Som
by andix 4y ago
I want more implicit typing in typed languages. Quite often the compiler knows exactly what type a function will return, but I still need to write it there. Sometimes it’s easy („int“), but what about HashSet<Immutable<Tuple<int,string>>>
Typescript does it well. F# (completely statically typed) too…
- mikepurvis 4y agoRust does a pretty decent job of this, particularly around functions that return collect(). But yeah, C++ has a ways to go on type inference.
- tlb 4y agoBetween `auto` and `decltype(auto)` it mostly does what I want, although the 2nd syntax is frightful. The main case it doesn't handle is for (auto i = 0; i < foo.size(); i++) where obviously I want i to match the return type of foo.size(), so a size_t rather than an int.
- kaba0 4y agoNowadays even Java will do that, var list = yourFunctionReturningThatSet();
- andix 4y agoAlso on a function? var getDate() { return „no date“; }
- masklinn 4y agoThat is often considered undesirable because it makes code less clear and compilation errors inscrutable. For instance the rust developers consciously decided to remove that from the language, named functions must be fully typed.
- estebank 4y agoEaster egg: you can use -> _ to ask the compiler what type it thinks the return type should be, given your body. Because it is only used for diagnostics it isn't fully featured and there are things it doesn't cope well with, but it is there and works most of the time.
- still_grokking 4y agoStatic type inference is "fully typed". def getDate = "no date" That defines a method `getDate` with the type `() => String` in Scala. The type is statically know, of course. But it's recommended to use explicit return types for public methods. This helps preventing breaking public API by refactoring the implementation of a method.
- ivan_gammel 4y agoNo, but I think that was a good choice. In local code this is great, but in an interface contract being explicit about data type is a virtue.
- andix 4y agoFor interfaces this would obviously not work (there is no implementation). But for class members it would. I think this concept works very well in typescript and F#.
- ivan_gammel 4y agoI mean interface not as a language construct, but as a declaration of how module (class, component etc) can be used. You want to see the type in such declaration, not to infer it based on implementation details.
- gpderetta 4y agoBut rust still allows declaring returning a trait instead of explicit types.
- dunham 4y agoFor Java, I used to write `list = yourFunctionReturningThatSet();` and then have the IDE fill in the type when it complains about the undefined variable.