8 ms·
I don’t know but I hate it. Not sure if I’m in the minority but it sure feels like it. In an ideal world. I feel that types are something that should be dealt w
by jaequery 7y ago
I don’t know but I hate it. Not sure if I’m in the minority but it sure feels like it. In an ideal world. I feel that types are something that should be dealt with at the IDE level.
In fact, there so many things that can be done at that level, but no one has really been brave enough to do so I suppose.
- tomc1985 7y agoSo, what, everyone standardizes around an IDE then? You really think that's gonna unite the vim and emacs camps? I'm personally tired of staring at variables trying to figure out what they're supposed to be, then having to dive into source to see how its used. C/C++/C# solved that problem, why are we still dealing with it?
- joelbluminator 7y agoIt rarely happens to me that I stare at a variable and have to wonder what type it is. And yes, an IDE like Rubymine is becoming crazy good at autocomplete and method lookup. I think the experience of developing on Rubymine isn't that far behind from Intellij nowadays. Not everyone have to use the same IDE, the vim or emacs guys will have to find equivalent tools.
- Shish2k 7y agoC had some typing, but I'm not going to call it "solved" until "numberOfHats = distanceInPixels + weightInKg" is considered a compile-time error due to the three "int" values being incompatible; but "numberOfHats = aliceHatCount + bobHatCount" is acceptable. How does nobody(?) support this yet? Python supports some parts: you can subclass int, and you get all of the int methods like addition and subtraction for free, but "distanceInKm + distanceInKm" gives you an int instead of a distanceInKm; and "distanceInKm + distanceInMiles" gives you an int instead of an error. Rust also has partial support but from the other end: distanceInMiles and distanceInKm can be two distinct subclasses of int, and adding them together is a compile time error. But also adding distanceInMiles with distanceInMiles is a compiler error, because these are basically "completely new classes" rather than "subclasses of int", and so you have to implement add / subtract / stringify / etc for yourself for every type D: (I'm fairly new to Rust so if there is a shortcut there that I'm missing please do point it out)
- creatio 7y agoDo they, the language is called F#.
- steveklabnik 7y ago(They won’t be subclasses in Rust, Rust doesn’t have classes nor inheritance. They’d be “newtypes”, a struct with one member.)
- kgilpin 7y agoYou could do this in C++ by storing the unit of measure with the measurement value and then performing unit conversion in overloaded math operators.
- 59nadir 7y agoEvery language that has generics supports this via phantom type variables that can encode extra information only in the type system alongside some other type, or with a specific newtype keyword that effectively does the same: newtype Pixel = Pixel Int newtype Em = Em Int pixelWidthToEm :: Pixel -> Em pixelWidthToEm (Pixel px) = Em px You can try to call `pixelWidthToEm` with anything other than pixels and it won't work. More dynamically, with an open type variable that only exists in the type system: data User a = User { name :: String, socialSecurityNumber :: String } data LogSafe data LogUnsafe logUser :: User LogSafe -> IO () logUser = undefined makeUserLogSafe :: User LogUnsafe -> User LogSafe makeUserLogSafe = undefined We can never log the user unless the user is deemed LogSafe and we make functions that produce log safe users that you have to call before hand, in order to make sure that sensitive data isn't printed to logs. These are things that have been around for a long time in almost every type system, but people's general lack of interest in using type systems to help them conspires to keep them in the dark. Here's how you can create a number type distinct from other number types in TypeScript: type DistanceInPixels = number & { readonly __newtype__: "DistanceInPixels" } And a type alias that allows you to create them: export type Newtype<T, Tag extends string> = T & { readonly __newtype__: Tag } type Pixels = Newtype<number, "Pixels">
- blandflakes 7y agoI get similar concerns with functions that take multiple strings - how do I make sure I didn't swap the bucket with the key? I've seen enums used here, as well as "wrapper" classes. In any case, to answer the question of "How does nobody(?) support this yet?", have you heard of https://frinklang.org/ https://frinklang.org/ ? It's not a useful tool for most codebases I work on but it's an interesting idea.
- pjmlp 7y agoAlgol solved the problem.
- eru 7y agoWhat do you mean by types being dealt with at the IDE level? Depending on your type system, a well-typed program can eg run faster, because the compiler / interpreter can elide certain runtime safety checks that would be necessary in untyped code. If your type system is crazy enough, you can even track the runtime complexity of your program at the type level, including whether your program runs in finite time. See eg Dhall (https://dhall-lang.org/ https://dhall-lang.org/) whose type systems only allows programs running in finite time.
- gmueckl 7y agoI think what GP means is that the IDE for a theoretical programming language could automatically infer types and have you not type any code for that explicitly. It might even not show you types as code at all and by default and overlay/add this info only on request. Generally, there is this huge disconnect between how code is expressed as text and how it is handled in as a graph structure inside the tooling. It is soon time to move beyond simple text files for code, I believe.
- eru 7y agoML family languages (like Haskell) had proper type inference for decades now. And yes, integrating that with your editor/IDE is a good idea. Depending on what you want to do, you might also want to start with the types and have the computer figure out the implementation. > It might even not show you types as code at all and by default and overlay/add this info only on request. Types annotations are often are great documentation, and there's a lot of practical knowledge in the ML communities about what types annotations to show in the source for helping with understand and debugging and which ones to leave out as clutter. I wrote 'proper type inference' above, because it's much more powerful than the watered down version Go and C++ give you. See eg https://news.ycombinator.com/item?id=8447280 https://news.ycombinator.com/item?id=8447280 for a Rust example.
- joatmon-snoo 7y agoIn an ideal world, humans write bug-free code ;) But honestly, if you're asking your IDE to do it, that means you're asking your IDE to do static analysis of your code - and type-checking in a lot of ways is just another static analysis technique. And for a lot of us (myself included) we prefer to catch as many of these bugs as possible using static analysis, instead of waiting for someone to get paged when it causes an outage. Yes, there's a trade-off, and types can be obnoxious (Java imports being probably one of the worst offenders, C++11 introduced `auto` for a reason), but that's the cost we pay.
- lloeki 7y agoRuby being so dynamic means that without type annotations you cannot infer the type (or types) within a variable statically, so you basically have to eval the program, which in Ruby means basically running the whole codebase. So, please, be brave and evaluate a 100kloc codebase+deps that may contain a top level `rm -rf ~/`
- rootlocus 7y agoBy IDE level, you mean compile level. Types are at compile level.
- boffinism 7y agoNo, types can be at compile level. They can also be checked without compiling any time you like. Or automatically. By an IDE, for example.
- gmueckl 7y agoAlmost all good IDEs are essentially interactive compilers. The lines have blurred during the last two decades. For example, QtCreator and XCode use clang to provide code annotations in the editor. Eclipse is built around ECJ, its own Java compiler, which exists mostly to provide information back to the editor and refactoring tools (the editor maintains a complete bidirectional mapping between code as text and code as AST at all times). Code generation is almost only a byproduct there.
- black-alert 7y agoI come from Assembly and C, now working in Javascript. One of the reasons I made the move to JS is dynamic typing, getting rid of that administrative pain and now being able to create stuff much faster. Even in large JS apps I hardly ever have type related bugs at all, and when I have one I fix it mostly within minutes, don't need an entirely different language and ecosystem for that. Now the JS fanboys discovered and moved (from Coffeescript to Babel ESxx) to Typescript they apparently think that they can write beautiful and bugfree software just because of static type checking! Let them please move to C++ or whatever statically typed language and shoot themselves in the foot by making all those mistakes that have nothing to do with static type checking at all! Oh, and of course hitting the wall because they are missing their precious 'any' keyword! I totally agree that type checking for dynamic languages should be done in the IDE, tooling. But static typing in the dynamic language world is a hype at the moment, so we'll have to go through a wave static type checking frenzy. For my work I look at horrible code bases, perfectly typed and strictly formatted by tslint..
- DCoder 7y agoI started using TypeScript back when it was 0.8, before it even had generics. Does that make me a fanboy? I have a project with about 45k SLOC of TypeScript (using Knockout.js for presentation). There is really no way I would maintain that same project without types. > For my work I look at horrible code bases, perfectly typed and strictly formatted by tslint. There is no language that can stop people from producing horrible code.
- black-alert 7y ago> There is really no way I would maintain that same project without types. That's bold. Do you think no developer would be able to manage it without TS? In that case you must be a fanboy! And honestly, are you not using 'any'? And do you think your app cannot crash because of a type error at runtime? And do you trust all the third party libraries you are using that they always provide you with consistent types, also during runtime? I ask this because most TS proponents live in some kind of dream.