Indeed, from the article: "In spite of all of this, I’ll be busy encouraging my coworkers to use variant if anybody needs me. Sum types are such a useful concept that they’re worth the pain[...]"
Indeed, from the article: "In spite of all of this, I’ll be busy encouraging my coworkers to use variant if anybody needs me. Sum types are such a useful concept that they’re worth the pain[...]"
Just another opportunity for C++ geeks to show off.
Edit: I have seen Typescript that looks like this, but some of you could more easily corroborate this claim that are working with it more persistently.
Previously, I had been very diligent about using JSDoc to annotate methods and fields with types, which gave me a lot of information with which to work in my IDE. But JSDoc is not a type system, it's very cumbersome to express ad hoc object structures, and being "very diligent" is not "perfectly diligent", so the TS porting process discovered some subtle bugs where I had, for example, guessed wrong on the type of something coming from a library (I also forked and ported most of my dependencies), or changed the return type of a method but missed a couple of very rare calls of it.
Ad hoc union types in signatures gets super line-noisey. You can avoid it by diligently (there's that word again) using interfaces and named types. I think type expressions get overused in TS, out of some hope they can make the language into Haskell. That is not the case, and most of the time it's better to use interfaces.
Still, I have a dependency that can only be included via a script tag. This wasn't too much of a burden in JS, because the language does basically nothing for you. But using it in my TS port took a lot of effort. I had to download the source of it, patch in a tsconfig, generate .d.ts files, and fix them up where they had escaped out to `any`. It's still not perfect. The quality of the library is pretty low to begin with, but it's the only way to interface with a WebRTC server I'm using. I'm pretty close to deciding to use a different WebRTC server (probably even one I write on my own), just to fix the remaining issues.
That said, if you're starting from scratch, TS can be very clean and push you towards much better designs. It's hard to do the wrong thing. You can still do it, but it's hard. You need to have that ability, though, because there's just too much "wrong" with everyone else's code out there.
I don't mind that, too much. The only thing I mind is that it's very difficult to distribute a library in such a way that it can be used with a TS project without a lot of warnings or errors, especially if you're using Webpack (ugh, Webpack generates some uuuugly bundles). So even though a lot of my dependencies offer TS-generated bundles or .d.ts files, it still doesn't work very well. You really need to be distributing TS source for the library, and not relying on any conditional compilation that your consumers will have to replicate in their own builds.
Type checking has existed for quite some time. Those languages and developers have created some of the most static UIs you can think of. Those desktop apps got stuck, they don’t innovate or adapt. I think it has partly to do with how rigid and sedimented their tools and languages got.
I deal with it every day. We look at a Java enterprise app built, and go, yeah this has to be refreshed. That language has type checking. What are we replicating here? We throw that stuff away like it’s a joke, so I wonder if we aren’t wasting our time with some of this stuff.
Type checking defies the utter success of Python, Ruby, PHP and JavaScript in building the entire modern web. We moved quickly and accurately due to these languages (it’s not a coincidence that they are all interpreted). I don’t want us to forget that. If you guys want to add something that helps, sure, but if you guys want to add stuff that adds little value that slows stuff down, it will prove out the same way it happened before. We’re just going to throw it away when the seasons change.
More serious question for you: Why do interpreted languages exist? What are one of the things they shed? Why’d they get popular? Why’d people find a way to move fast in them?
I guess my vigilance is basically this, I’m the guy before you all turn JavaScript into what the OP is describing as the monstrosity that c++ is. Explain why that won’t happen the same way I guess, since we’re all smart, and students of history. We have something to protect here too. We built the stuff you guys couldn’t, but you know better apparently now. I’m your ghost of Christmas past.
std::variant<int, std::string> setting;
setting = "foo"s;
setting = 1;
Compared with something like: struct Setting {
Setting() { XXX }
~Setting() { XXX }
union {
string str;
int num;
};
enum Type { Str, Int, None };
Type tag;
};
Setting setting;
if (setting.tag != Setting::Str)
new (&setting.str) std::string;
setting.str = "foo";
setting.tag = Setting::Str;
if (setting.tag == Setting::Str)
setting.str.~std::string();
setting.str = 1;
setting.tag = Setting::Int;