15 ms·
The Ergonomics of Type Checking (2016)
- zpr 9y agoHate to be that guy but, Flow for JS is something like that. You can optionally enable it and even enable/disable it in certain files, functions, lines, etc.
- hliyan 9y agoThat is optional typing. The author is talking about gradual or progressive typing.
- zpr 9y agoBut he mentions he wishes he could start dynamically and end statically. By enabling Flow at the end, don't you basically achieve that? You can also enable it progressively for files as you move on, and still rapidly prototype without it. In fact, unless I'm mistaken I believe it's "opt-in" by default, ie. Flow won't scan the file unless you include the special comment keyword at the top of the file.
- mercer 9y agoCould you explain the difference? I though TypeScript was a good example of gradual typing.
- tuxracer 9y agoThe author's ideal language seems to also describe TypeScript https://eng.lyft.com/typescript-at-lyft-64f0702346ea https://eng.lyft.com/typescript-at-lyft-64f0702346ea
- mebassett 9y agoI can agree with the author's premise that types are essentially great, but it would be useful to have control over when (if ever) in the development stage I need to bring them, much like we do with testing. I guess there is an underlying analogy between TDD and strong static typing. I think this is one of the motivations for typed/racket, which is a lot of fun to use.
- msl09 9y agoI think that type hints are becoming like that in the python world. Almost all my code starts untyped and later I start adding types when they are not trivial
- adrianratnapala 9y agoThe type checker has questions, and it is adamant about them, refusing to let you move forward until they are answered. Often, I’m ok with that. They are good questions, and most of the time they really should be answered. The question is: when? This is a good point. The poster then proposes being able to nail down your types progressively -- which I think is fine as a long as you don't let the iteration cycle get too long. Optionally typed languages are one way we approach this ideal. Another way is linters that are stricter than the compiler itself. You must run the compiler to have testable code, but you can run the linter later, perhaps only as part of CI. It is possible that in future, both of these approaches will look like primitive exteremes of some richer process progressively nailing down the static safety of a program.
- jryan49 9y ago"Gradual typing" is one of the reasons I still love using Groovy :)
- hliyan 9y agoI've recently come become aware of Groovy while looking for a programming language with Java's features _without_ the need for object orientation. How production ready is it? (I'm talking enterprise systems, not web based or consumer applications)
- megaman22 9y agoIt's been around for a long time. Works pretty well as a scripting language to plug into Java enterprise systems. At least we've been using it for that for six or eight years.
- pjmlp 9y agoIt was booming in JUG talks about 10 years ago, Sun was even doing talks about making it available as implementation language for JEE beans. Nowadays if it wasn't for Gradle, I bet it would have been gone by now.
- eikenberry 9y agoIt is good when used an an extension to Java. Sort of like TCL works with C. It is not so great if you try to use it as a stand alone language.
- jryan49 9y agoI really only use it as a glue code language or for automation. I thought that at least a few years ago there were a bunch of people using Grails? After using it extensively, I have found a few strange bugs that's killed some time but nothing show stopping. For what it's worth, the bugs were at least in their bug tracker. I would love to use it for everything but it seems to be losing popularity, which is really the only reason that makes me hesitant to do so. Maybe I should start contributing :)
- 9y ago
- SolarNet 9y agoJulia does gradual typing very well. I think it delivers what the user wants. It has a REPL, you can write code using no types, as types are added to existing code it improves in performance and safety.
- spullara 9y agoThis is related to why all my companies use TypeScript rather than plain JavaScript for their frontends and sometimes their backends (vs other languages).
- hliyan 9y agoCan't a compiler be created to traverse the AST, locate all operations performed on the variable and infer the narrowest type definition that allows for all of those operations, and then enforce at compile time? Edit: example to further clarify what I meant (it's a bit more than type inference at initial assignment) var x = readUserInput(); // narrow down: string, int, double x += y; // if y is also a user input, no change x += 1; // narrow down to int, double p = x % 2; // narrow down to int
- llimllib 9y agoMy understanding is that Julia does something like this? But I'm far from an expert.
- dnautics 9y agoYep. Julia is a great language and I think other languages could learn from it, especially it's typing model and the excellent implementation of the REPL. but I wouldn't say Julia is good for everything (I wouldn't want to write a web service in it).
- bastawhiz 9y agoMany languages do this (type inference), to some extent. The moment you have any expression that can evaluate to more than one type, though, it requires annotations.
- hliyan 9y agoAre there any languages that do it beyond the first value assignment, or outside the current scope? Also, any languages that subsequently revise the type definition?
- tedmiston 9y agoNot sure if this is exactly what you're asking but one can reassign variables to change their types in Python: >>> x = 1 >>> type(x) <class 'int'> >>> x = 'foo' >>> type(x) <class 'str'>
- neilparikh 9y agoHaskell has a flag called -fdefer-type-errors (https://downloads.haskell.org/~ghc/7.6.1/docs/html/users_guide/defer-type-errors.html https://downloads.haskell.org/~ghc/7.6.1/docs/html/users_gui...) that gets you pretty close to the experience of ignoring errors you don't care about right now.
- twic 9y agoYou can do something similar with Eclipse's compiler for Java, JDT, which features [1]: An incremental Java compiler. Implemented as an Eclipse builder, it is based on technology evolved from VisualAge for Java compiler. In particular, it allows to run and debug code which still contains unresolved errors. That means that you can write something like: private void describe(String string) { if (string.isEmpty()) empty(); else nonempty(string); } private void empty() { System.out.println("empty"); } private void nonempty(String string) { System.out.println("contains " + string.size() + " characters"); } Then you can still run the code and call describe, but if execution reaches nonempty, you get a runtime error telling you about the compilation problem. It's one of the few things from Eclipse i miss when using IntelliJ. [1] http://www.eclipse.org/jdt/core/ http://www.eclipse.org/jdt/core/
- _pmf_ 9y agoAnd this has been working for a decade. It's one of the reasons I laugh in their face when a JS developer tries to sell his babel reload crap as progressive.
- exceptione 9y agoI can't see a problem with nonempty, am I stupid?
- toolz 9y agoI imagine the author would appreciate the static analysis/success type checking that erlang/elixirs dialyzer provides.
- jingwen 9y agoIf you're interested in learning more about gradual type systems, check out Jeremy Siek's work [0, 1]. He's one of the early researchers in this area. [0] https://wphomes.soic.indiana.edu/jsiek/what-is-gradual-typing/ https://wphomes.soic.indiana.edu/jsiek/what-is-gradual-typin... [1] https://github.com/samth/gradual-typing-bib https://github.com/samth/gradual-typing-bib
- emodendroket 9y agoThe biggest value of static types is when you go to edit some code written years and years ago and you can tell what the hell the intended usage was without digging through the method
- Guthur 9y agoNot even remotely universally true. I've seen plenty of static code that is as obtuse as any dynamic code. Authorship holds far more sway than dynamic vs static.
- emodendroket 9y agoIt is 100% less likely that you'll run into out-of-date docs wrongly describing the types of the input arguments and return types. Of course the code might still be obtuse but the type declarations are a form of documentation that the compiler ensures is still up-to-date.
- krylon 9y agoNow that it is finally there, I can say this without the usual jokes we have grown so accustomed to: Perl 6 does this. Granted, (almost) nobody is using it, but it does exist (finally!).
- hinkley 9y agoRight on time too! Back in like 2006 they were estimating that all of the stuff they wanted to put in 6 would require until 2016 or 2017 to complete. I figured that was a comment meant to negotiate down the scope to something reasonable. Turns out, no...
- nearmuse 9y agoSo PEP 484? You can write out whatever and then add annotations (at least to the API).
- zero_iq 9y agoAlso Cython: start with pure Python and add types and other annotations to get the performance gains of static compilation (to C Python extension).
- pleasecalllater 9y agoSo you spend time writing the things, which then are totally ignored by the compiler/interpreter. Seems like a waste of time, except for documentation purpose.
- dec0dedab0de 9y agoDocumentation is never a waste of time. Especially if your IDE is aware of it, and pointing out errors for you.
- nearmuse 9y agoThose are actually enforceable by IDE or interpreter/compiler if implemented. Even if this wasn't the case, it is better than some arbitrary variable naming convention or comments format.
- tedmiston 9y agoYou can check them at runtime with mypy. http://mypy-lang.org/index.html http://mypy-lang.org/index.html
- quickben 9y ago"I often wish I worked in a language where I could start dynamic and end statically." C++ 'auto' type. Workflow: 1. Write 'auto =', see what the compiler picks up (iterator of a vector of a set of classes that points to a list of structs, etc). 2. Potentially copy/paste that (unless the line is bigger than 350 characters :) )
- jeremyjh 9y agoType inference or deduction is not the same as dynamic typing. An auto variable resolves to exactly one type, which is determined by the expression it is bound to. If you pass that variable to a method expecting a different type, the compiler will stop you. If it were dynamically typed, it would let you proceed and fail at runtime.
- nicoburns 9y agoAs much as it gets a bad rep, I think PHP is actually the language that comes closest to this. It's not quite there yet, as although newer versions of PHP have been adding optional type annotations, they are enforced at runtime rather than compile time. It seems not too much of a leap to imagine that some compile time checks might be added at some point though once type-annotated code becomes more universal.
- viperscape 9y ago“I often wish I worked in a language where I could start dynamic and end statically. There are a number of languages with optional type systems, but they aren’t the ones that I use.“ That’s a shame because typescript is basically this. Typing is optional, and you can become more granular as you want.
- tedmiston 9y ago> I often wish I worked in a language where I could start dynamic and end statically... I can imagine writing the core of an algorithm and then gradually tightening the conditions as it comes into focus. More than a tool, this would be a practice: progressive type elaboration. Bingo. I love this idea. I haven't tried it yet myself, but I think mypy [1] from Dropbox makes this a reality today in the Python world. [1]: http://mypy-lang.org/ http://mypy-lang.org/
- tylerjwilk00 9y agoI know it's not a favorite lang around here but this is exactly what modern php is.
- kod 9y agoHow exactly does modern php (not Hack or another vm) let you "start dynamic and end statically"? declare(strict_types=1); is checking and throwing type errors at runtime, not compile time.
- ACow_Adonis 9y agoI believe it's called Common Lisp, specifically SBCL :) Live the dream :P
- sitkack 9y agoCame here to mention MyPy and optional typing, the preferable term Gradual Typing means something distinctly different. http://mypy-lang.org/about.html http://mypy-lang.org/about.html Dropbox employs the creator of MyPy but it isn't a creation of dropbox. Type Systems as Macros http://www.ccs.neu.edu/home/stchang/pubs/ckg-popl2017.pdf http://www.ccs.neu.edu/home/stchang/pubs/ckg-popl2017.pdf Pluggable Type Systems http://bracha.org/pluggableTypesPosition.pdf http://bracha.org/pluggableTypesPosition.pdf Propositions as Types http://homepages.inf.ed.ac.uk/wadler/papers/propositions-as-types/propositions-as-types.pdf http://homepages.inf.ed.ac.uk/wadler/papers/propositions-as-... https://www.youtube.com/watch?v=IOiZatlZtGU https://www.youtube.com/watch?v=IOiZatlZtGU
- 9y ago
- z3t4 9y agoin JavaScript I pepper each function with if(...) throw, eg the program will throw if something is wrong. Doing this catches a lot of errors and bad state. - Without having to babysit a compiler (Typescript) and I can enjoy all the dynamic features. Sometimes I miss the type annotation as documentation, but then I just rename the variable to make it more clear what it is and does.
- bitwalker 9y agoInstead of "babysitting" your compiler, you're "babysitting" your application at runtime - I fail to see the difference.
- z3t4 9y agoGood point. Think of it as hand-holding your peers instead of hand-holding the compiler. if(x==y) throw "friendly error" // avoids eternal loop ... The compiler doesn't care if you get an eternal loop at runtime, and static types wont prevent it.
- iitalics 9y ago> I wish I had a language that did optional typing > they exist, but I don't use them. :/ The author's thoughts (I want to write dynamically to get the code working, but then make it static afterwards) are nearly the exact thing I've heard from Matthias Felleisen when he talks about the motivation behind Typed Racket.