(no title)
moss2 | 2 years ago
>
> x := 10
I...
Why is this a feature in every new language? Can't we have a language that is more verbose and explicit, not less? I'd love it if named parameters were mandatory, not optional.
(Named parameters is when you name parameters/arguments you pass on to functions, like you can do in python and groovy: `foobar(arg1: 123, arg2: 'hello')`)
Most of my problems I run into is due to implicit behaviour that no one bothers to explain. In Onyx here, having type-inference means I now how to remember that x := 10 means x will be a signed 32-bit integer instead of, you know, the code that I'm writing remembering it for me.
And I'm just guessing here. Maybe x is a double. Or unsigned since its initial value isn't negative. Or maybe it was signed 32-bit integer but the Onyx developers changed it to 128-bit long long for version 666.0.0. The point is I have to look this stuff up or remember it instead of, you know, it being right there.
I don't even know what you gain by doing this. Less code is less messy but also hides a lot of information from you. Hiding information should be something an IDE does, not the language itself.
Thank you for reading my rant.
justinpombrio|2 years ago
My preference would be to do some type inference, but maintain the property that you can tell the type of every expression without looking outside of it (except perhaps for an immediate enclosing function call). This requires, for example:
- The third option isn't allowed, you need to write the second option instead.
- You must annotate function argument types.
- In Rust, you couldn't write `.collect()`, you'd instead write `.collect::<Vec<_>>()`.
- The `x := 10` example is actually somewhat ambiguous. If the language fixes the type of `10` then `x := 10` is legal. If it's an unspecified type (as is typically the case), you'd have to write the type down.
stcg|2 years ago
For that case I like a type signifier as part of the number literal expression, like this: `x := 10f32` or `x := 10i32`.
Nevermark|2 years ago
--
What would resolve this whole issue is standardized editor/IDE visualization support for showing all inferred types, just one toggle button/key away.
Inferred types simplify writing code. But when reading code, why should we have to mentally emulate the language's inference algorithm? It is, by definition, supposed to be automating that for us.
vips7L|2 years ago
This is untrue. Inferring diamond types has been built into the language for over a decade.
moss2|2 years ago
Take your Java example. If the code in the .java file looked like
but IntelliJ showed it to you likekstrauser|2 years ago
An extra advantage I see is that non-default types stand out. At a glance at the code, `x:=10` is the most common int type. It’s plain, unadorned. It’s the usual, boring thing. Things that are less common stand out: oh, this is unsigned for some reason. Guess I should see why. I like the pattern where unusual things are easier to visually scan for.
But at the end of the day, darn it, compiler, you know very well that I mean 3.14 to be a float. Stop making me say so each time!
moss2|2 years ago
Is Onyx like that? I don't know. It would take me maybe two minutes to look up. The point is I have to look it up to be certain when that certainty could be part of the language.
giraffe_lady|2 years ago
I love complete inferred type systems, using one has completely changed how I think about types and what I expect from a programming language. I feel ill every time I have to use go or typescript. Like I am teaching the compiler how to compile. This is not my job, it's a waste of time and energy, it should already know. Things like rescript and ocaml are where my attention is going for typed languages in the future.
cobbal|2 years ago
trealira|2 years ago
I prefer the type inference approach, e.g. in Rust, where they're i32 if the type cannot be inferred and the literal has no type suffix. And I like that no two integers can be used by the same binary operator unless they have the same type, so you need to explicitly cast them to the right type.
Joker_vD|2 years ago
dragonwriter|2 years ago
That's not why you don't have to do that.
Many (most statically typed?) Algol-likes have strict types for literals (e.g., 2 is always a signed int, if you if you want specifically unsigned you might say 2u and if you want a double you say 2.0, and if you want a single-precision float you say 2.0f, or something) and strict rules at how math between them works and what types it produces, this has been true since long before tyoe inference became common, and is why you don't have to say 2int+2int — 2 is syntactically defined as int.
There is no inference
Joker_vD|2 years ago
Also reasonably, each source file should start with declaring the version of a language it was written in, so that e.g. Onyx 666.0 changing its default integer width wouldn't affect the code in the file with "///OnyxVer=665.0" at the top of it. Now that is a feature I would like every new language to have.
wharvle|2 years ago
That is, as long as the core tools for the language provide that info somewhere that’s not more than barely more hidden than explicitly typing it out, I think that’s Ok.
dragonwriter|2 years ago
Because superfluous type checker incantations are, aside from breaking up flow of thought when writing code, annoying visual noise when reading it.
> I'd love it if named parameters were mandatory, not optional.
Named parameters are a different thing than type incantations, and I agree that it would be good for any non-operator function that takes more than 1 argument to require named parameters.
rstat1|2 years ago
I disagree. Implicit typing for the most part makes things harder read. The less I have guess about what type something is the better. I never use stuff like "var" or "auto", unless forced to for this very reason.
xigoi|2 years ago
gpderetta|2 years ago
goto11|2 years ago
So if you want the type to be always explicit, the type specifier should be coupled with the literal like `10[i32]` or similar, so you can write `foo(10[i32])`.
moss2|2 years ago
Assigning variables or calling methods (when you discard the return value) would look the same as is typical in many languages:
Named variables in method calls would have to have the type so as to not hide what type a method-as-parameter returns:thefaux|2 years ago
otabdeveloper4|2 years ago
What's wrong with the old verbose languages?
mrweasel|2 years ago
If you have a type system, then force the user to specify exactly what type they want something to be. It's completely reasonable in my mind to write var x float32 = 10, the compiler can then deal with adding the .0 if it wants.
bobbylarrybobby|2 years ago
moss2|2 years ago