News for hackers. Stuff that matters.
Re:Static types in Python by wyager on Thursday October 13, 2016 @07:13PM (#12704962)
A sufficiently good static type system won't make generic operations harder to implement. It can even make them easier to implement in terms of guiding you towards good generalization techniques.

You can also do full Python-style "duck typing" statically. It's called structural typing, with native support from languages like typescript, purescript, etc. It's used a lot in web dev because it works well with Javascript's object model.

Re:Static types in Python by duaneb on Thursday October 13, 2016 @07:49PM (#12705157)
> A sufficiently good static type system won't make generic operations harder to implement.

This is true for the JVM definition of 'generics', but it doesn't mean that arbitrary operations on a collection of types can be easily expressed with a given (static) type system. Run time type information + a dynamic type can ameliorate some of this, but that's shoehorning optional types into a static type system, and I don't think that's quite what you meant.

Think about a generic operation to diff two data structures: you need access to the members of the struct in a generic way. I don't think this is easily solved with static types.

Re:Static types in Python by wyager on Thursday October 13, 2016 @07:59PM (#12705219)
Can you give a specific example where you think you need dynamic types? The diff operation you described sounds like it could be made generic pretty easily, but I may be misunderstanding the goal.
Re:Static types in Python by duaneb on Thursday October 13, 2016 @08:05PM (#12705250)
> Can you give a specific example where you think you need dynamic types? The diff operation you described sounds like it could be made generic pretty easily.

How do you figure? This would require macros or runtime type introspection.

A specific example might be "serialize this json type without having to annotate the fields with how to serialize them".

Re:Static types in Python by wyager on Thursday October 13, 2016 @08:12PM (#12705286)
> This would require macros or runtime type introspection.

Not if you structure your data using an appropriate data structure, like a map. After all, this is all structs in dynamic languages are.

> A specific example might be "serialize this json type without having to annotate the fields with how to serialize them".

Aeson does this safely and statically using Haskell's powerful static Generics.

    data Foo = Bar Int String deriving (Generic)
    instance FromJSON Foo
    instance ToJSON Foo
You can get named fields like

    data Foo = Bar {baz :: Int, qux :: String}
Re:Static types in Python by Roboprog on Friday October 14, 2016 @12:44PM (#12709280)
True, but "batshit conservatives" won't do that, or will mess themselves if you do that, rather than making some kind of "Object-Whatever-Mapping" model and annotations.

But that's what I would do (at least in Java) for this kind of transient transfer object, if left to my own devices.

https://plus.google.com/110981030061712822816/posts/KaSKeg4v...

(I would put myself in the "moderate liberal" camp, even if the languages Steve Yegge lists for that group are not my choice languages)

Re:Static types in Python by duaneb on Thursday October 13, 2016 @10:41PM (#12705820)
Most static typing systems have no parallel to 'deriving', which is to my understanding compile-time computation (aka a macro).
Re:Static types in Python by dllthomas on Friday October 14, 2016 @05:25AM (#12706960)
"deriving" is compile time; "Generic" is runtime inspection of the shape of a value (in terms of constructor applications). Something (at least vaguely) similar to Generic exists in many statically typed languages - including (IIRC, IIUC) Java and Go.
Re:Static types in Python by duaneb on Friday October 14, 2016 @01:37PM (#12709736)
> Something (at least vaguely) similar to Generic exists in many statically typed languages - including (IIRC, IIUC) Java and Go.

Sure—but it's in spite of static typing, not because of it. You certainly give up any guarantees of avoiding runtime errors by detecting typing issues at compile time.

Re:Static types in Python by dllthomas on Friday October 14, 2016 @02:11PM (#12710011)
Sure, it's orthogonal to static typing (I wouldn't quite say "in spite of"). I wasn't trying to weigh in on the general topic, just to clarify what was happening where.
Re:Static types in Python by imtringued on Friday October 14, 2016 @04:13AM (#12706728)
>A specific example might be "serialize this json type without having to annotate the fields with how to serialize them".

https://manosnikolaidis.wordpress.com/2015/08/25/jackson-wit...

That is a property of the library not of the language.

Re:Static types in Python by Roboprog on Friday October 14, 2016 @12:38PM (#12709243)
There are some wrapper utilities for Java around reflection for property access, but yeah, it's still pretty clumsy.
Re:Static types in Python by Roboprog on Friday October 14, 2016 @12:36PM (#12709218)
Yeah, Java has kind of "choked the ecosystem" for the last decade. That monster needs to retire before the industry can move forward (the language, at least, the JVM seems to work well).

Java generics seem easy enough to instantiate and use, but the mess when you implement one really discourages people from trying, usually. And if you do, the result is often a poster child for FUGLY code. Try declaring a java 8 lambda parameter that works with collections some time, and using that in a few places - serious COBOL-fingers and eye-burn time. Even a Pascal style "type" or C style "typedef" mechanism would have greatly reduced clutter when trying to create code that implements (java) generic operations.

Die, Java, die :-)

Re:Static types in Python by catnaroek on Thursday October 13, 2016 @07:26PM (#12705038)
OCaml-style structural typing cannot handle deciding at runtime what fields an object has. Arguably it's best to know this statically, but dynamic language users make extensive use of the ability to dynamically add fields to an object.
Re:Static types in Python by nine_k on Thursday October 13, 2016 @08:54PM (#12705450)
...which, of course, make it easier to assume the presence of a particular field and crash.

Using an explicit map for dynamic attributes at least makes you aware what is guaranteed and what is optional.

This removes much of the neat magic that such a dynamic approach affords to languages like Ruby, JavaScript, and, to a lesser extent, Python.

Re:Static types in Python by jon_smark on Friday October 14, 2016 @05:45AM (#12707018)
In the case of OCaml, the structural subtyping of object fields can never cause a crash, because it is verified at compile-time.
Re:Static types in Python by Roboprog on Friday October 14, 2016 @12:29PM (#12709174)
Thanks, the OCaml stuff looks interesting (Wikipedia examples for structural types).

It feels like type inference meets duck typing, but that's a good thing.