3 ms·
I had the idea, years ago when I still thought some day I'd write a programming language, that somewhere between type inference and programmatic type inspection
by hinkley 9y ago
I had the idea, years ago when I still thought some day I'd write a programming language, that somewhere between type inference and programmatic type inspection that there is space for promoting a type based on your inspection. Especially if you're using Static Single Assignment inside of your compiler.
For instance:
Object foo = inputArgument1;
if (foo typeof function) {
// in this block foo is a function.
foo(bar);
}
Where this breaks down on legibility is when people rely too much on inductive reasoning (you can only get into this block if three other things are true... except now there's a bug so that's no longer true) or they keep reassigning to 'foo' willy nilly through their code. But if those are the sorts of people who lose out to the benefit of everyone else, I'm not even sad.
- greiskul 9y agoKotlin does that with it's smart cast feature: https://kotlinlang.org/docs/reference/typecasts.html https://kotlinlang.org/docs/reference/typecasts.html It's really great actually, and the IDE also helps by marking foo slightly different inside the block, and if you mouseover to check for it's type it says what it is, and that it's because of a smart cast.
- hinkley 9y agoI have a suspicion that the Jetbrains guys were reading the same language design sources that I was. There are several other features like the ? operator that I saw in a small language first (called Nice). Daniel Bonnoit did a good job of articulating some of his design choices, especially optional types.