News for hackers. Stuff that matters.
Re:Dynamic type systems are not inherently more open by zitterbewegung on Sunday January 19, 2020 @08:58AM (#22091439)
Using Type inference[1] on a language could be able to figure out types in most situations.

[1] https://en.wikipedia.org/wiki/Type_inference

Re:Dynamic type systems are not inherently more open by azhenley on Sunday January 19, 2020 @09:50AM (#22091688)
Yes, but "Type inference has usability problems".

http://web.eecs.utk.edu/~azh/blog/typeinference.html

Re:Dynamic type systems are not inherently more open by leshow on Sunday January 19, 2020 @12:46PM (#22092695)
I'm curious why you quote this like it's a fact when it's an opinion. The understanding of type inference used as the basis for making the argument in that blog post is superficial. Type inference doesn't just allow you to leave off type information from variables. A good inference engine will infer the most generic type for variables (and functions!), it's a discovery that you can query the compiler for in order to do type driven development.

A full rebuttal of this blog post would take much longer, but suffice to say inference does the exact opposite of what they are claiming; it increases readability and usability.

Re:Dynamic type systems are not inherently more open by azhenley on Sunday January 19, 2020 @12:50PM (#22092730)
I quoted the title of my blog post. Do you have any evidence for your claim that it increases readability or usability?

A few human-subjects studies from different labs have recently been ran on this and the findings mostly agree with my opinions. Papers forthcoming.

Re:Dynamic type systems are not inherently more open by sweeneyrod on Sunday January 19, 2020 @04:40PM (#22094305)
If type inference damaged readability, we would expect companies/projects that use languages such as OCaml and Haskell where type inference is possible but not mandatory to have policies against it in their style guides. In my experience with OCaml, the widely agreed on style is to only annotate types of functions that are part of some public interface (although since this annotation is necessary to define the public interface of a library, it's not even necessarily true that type inference impairing readability is the primary reason for this).

Also, if lack of type annotations were a common problem, you would expect Java programmers who switch to Python to frequently want to annotate all their types. But as far as I know this is not the case: even now that mypy is becoming popular, people only annotate the types that it can't infer.

Re:Dynamic type systems are not inherently more open by azhenley on Sunday January 19, 2020 @05:19PM (#22094527)
Companies do have policies against using type inference (e.g., the use of var in C#).

But how would companies/people know if it damaged readability? There has been little research on this (a few studies are currently in peer review!) and our intuition about such things is often inaccurate. There are so many other confounding factors that it is hard to isolate just type inference/annotations.

Re:Dynamic type systems are not inherently more open by yawaramin on Sunday January 19, 2020 @06:36PM (#22094947)
> Companies do have policies against using type inference (e.g., the use of var in C#).

That's a language that historically has not had type inference. Any examples of companies mandating type annotations in historically type-inferred languages?

Re:Dynamic type systems are not inherently more open by azhenley on Sunday January 19, 2020 @06:42PM (#22094987)
Yes! Any company that has adopted MyPy or TypeScript often have policies for this. One team I spoke to at Facebook said that any new code should have type annotations. I believe Dropbox had a nice blog post about new code should have type annotations too. It is hard to get these details from companies/teams (and be allowed to talk about it).
Re:Dynamic type systems are not inherently more open by yawaramin on Sunday January 19, 2020 @07:32PM (#22095221)
That's because (a) these are not historically type-inferred languages–runtime typechecking doesn't count as type inference–and (b) the gradual typechecking approaches like Mypy and TypeScript often have a lot of type inference issues which force people to try to overcome them with manual annotations.

In languages with even reasonably good type inference you'll find that the idiom is not to annotate types at the very least inside function bodies. And in OCaml, which has principality of type inference i.e. the types it infers cannot be overridden and corrected by manual annotation, the idiom is to not annotate implementation files at all.

Re:Dynamic type systems are not inherently more open by sweeneyrod on Sunday January 19, 2020 @06:50PM (#22095029)
Do they mandate that only function types (and any ambiguous ones) should be annotated, or that all types (i.e. including those of local variables which mypy can infer) should be? I strongly suspect the former, which implies that they view type annotations more as a necessary evil to allow type checking rather than a benefit in their own right. After all, there was nothing stopping people putting "type annotations" in Python 2.5 code if you wanted, by writing `x = get_thing_count() # int`. But AFAIK no-one did that until annotations could actually be used to check type safety.
Re:Dynamic type systems are not inherently more open by leshow on Saturday January 25, 2020 @12:23PM (#22147306)
Typescript will infer the 'any' type by default in a lot of cases, that's the reason teams will mandate type annotations. It's really just not the same thing. These are bolt on type systems to dynamic languages where if you fail to put a type annotation you're actually losing information. That's not the case in Haskell, type inference causes no loss of precision for types.
Re:Dynamic type systems are not inherently more open by pwm on Sunday January 19, 2020 @02:44PM (#22093513)
I don’t agree with readibility (there are pros and cons, ie. terser code vs. implicit types) but, at least in Haskell, it improves usability by inferring the most generic type to your expression.
Re:Dynamic type systems are not inherently more open by leshow on Sunday January 19, 2020 @03:56PM (#22094021)
Agree on what? "Type inference" is a broad feature, feel free to post the resources.

edit: In Haskell it goes beyond just locally inferring variables, it enables a new type of development with typed holes that feels more like a back and forth conversation with the compiler. I don't see how "I can leave the types off my C# variables" has anything to do with that.

Re:Dynamic type systems are not inherently more open by yawaramin on Sunday January 19, 2020 @03:19PM (#22093746)
In practice, not really. A good type inference system like Hindley-Milner infers the most general type of any expression you give it. This usually makes your code more polymorphic i.e. generic than if you would have manually written down the types.

You talk about cognitive load of looking up type info but there's also a cognitive load to having to parse every type annotation over and over again whenever you read the code. And since code is read more often than it's written, that's really just optimizing for the wrong load. As you re-read a codebase over time you'll get familiar with its types. And then the annotations start to get in the way. This is in fact one of the reasons people prefer dynamic typing.

> My response? Go refactor your hideous code.

To what? Would be very interesting to see your suggested refactor of that code.

> Before every language designer blindly accepts type inference as a good thing, I propose that some research be conducted.

What would be the research question(s) specifically?

Re:Dynamic type systems are not inherently more open by amelius on Sunday January 19, 2020 @09:06AM (#22091480)
Yes, some programming languages take this approach but I don't think that prototyping is as easy in such languages as it is in a dynamically typed language (e.g. Haskell vs Python).
Re:Dynamic type systems are not inherently more open by yakshaving_jgt on Sunday January 19, 2020 @09:40AM (#22091640)
In my experience (currently writing Haskell professionally, previously writing Ruby professionally) prototyping is easier in Haskell.

How many systems have you prototyped in Haskell?

Re:Dynamic type systems are not inherently more open by dllthomas on Sunday January 19, 2020 @02:04PM (#22093187)
> prototyping is easier in Haskell

That's very much my experience, as well. Assuming - and it's a very big assumption - that your use case has equivalent library support.

Re:Dynamic type systems are not inherently more open by rwmj on Sunday January 19, 2020 @09:18AM (#22091534)
When writing OCaml, if I'm unsure of the type I set the type to some impossible value, compile it, and the compiler tells me what the type really is.
Re:Dynamic type systems are not inherently more open by hnra on Sunday January 19, 2020 @09:37AM (#22091622)
I Haskell you simply put a "_" in the annotation and the compiler suggests the correct type.
Re:Dynamic type systems are not inherently more open by dllthomas on Sunday January 19, 2020 @02:07PM (#22093218)
You can also do this with values, asking "what goes here?" - it's great.
Re:Dynamic type systems are not inherently more open by andrepd on Sunday January 19, 2020 @10:51AM (#22092017)
Alternatively, merlin can tell you the type of the expression under your cursor, besides also flagging type mismatches like your situation.
Re:Dynamic type systems are not inherently more open by hopia on Sunday January 19, 2020 @10:13AM (#22091808)
Productivity can be really subjective. Those that are used to modeling the problem domain with the type system find that their most productive approach, even with prototyping.
Re:Dynamic type systems are not inherently more open by dllthomas on Sunday January 19, 2020 @02:06PM (#22093204)
Right, and the type system is such a big help in refactoring - especially in small projects - that you don't have to be all that right in your first N cuts at the model. You can write code and find out where you're wrong, and fix it fast.
Re:Dynamic type systems are not inherently more open by pwm on Sunday January 19, 2020 @02:51PM (#22093546)
It’s even more help in larger projects :) The dividends of type-checher aid refactoring really start paying off once the project is above a a certain size/age.
Re:Dynamic type systems are not inherently more open by dllthomas on Tuesday January 21, 2020 @05:04PM (#22112161)
Yeah, I think I was unclear. Given a need to refactor, it's a bigger help on a bigger refactor.

But a large refactor on a large project is still a pain. By the time you get there, you still want your model stable and/or flexible enough that you don't need to do that much.

On a small enough project, every refactor is easy enough that you can lean into it. Make a bunch of assumptions you're not confident about, setting up the types so you're sure you'll know what needs fixing when they're wrong. Then you test those assumptions as you write - and run - more code.

Re:Dynamic type systems are not inherently more open by zitterbewegung on Sunday January 19, 2020 @09:43AM (#22091661)
What would be neat is if you could take a Python program and run type inference on the program and it would output a type annotated version of the code.
Re:Dynamic type systems are not inherently more open by duckerude on Sunday January 19, 2020 @11:35AM (#22092264)
I don't have experience with it, but pytype can do that: https://github.com/google/pytype

After typechecking it leaves provisional stubs in .pytype/pyi/, and it has a tool to merge them into the original code.

mypy has stubgen: https://mypy.readthedocs.io/en/latest/stubgen.html

As far as I know it's less powerful since mypy isn't really built for unannotated code and pytype is.