News for hackers. Stuff that matters.
Re:What else are we getting wrong about programming languages? by nradov on Sunday October 30, 2016 @05:23PM (#12832411)
One big mistake we keep making is drawing a false distinction between static versus dynamic typing and treating it as all or nothing. Type systems should be scalable, in that programmers should be able to specify as much or as little type information as they want. For example, a variable could be declared with any of the following types. 1. A value. 2. A number. 3. A floating point number. 4. A floating point number between 0.0 and 100.0. 5. A floating point number between 0.0 and 100.0, and optimize for runtime performance instead of precision. Etc. That way you could start with something quick and dirty for a prototype and then gradually lock it down through progressive iterations. Ada and Microsoft Visual Basic incorporate some elements of this concept but didn't take it far enough.
Re:What else are we getting wrong about programming languages? by FLUX-YOU on Sunday October 30, 2016 @06:50PM (#12832970)
Every step of the scale will have to work with every other step of the scale. Does every step handle and allocate memory the same way? What about pointers? C/C++ interop? Does each one interact with the operating system the same way? How does a thread from one scale interact with objects from another scale? That's a lot to ask for something that's just going to be a prototyping feature (or a mode that is never used in production). How do you even think in such a mode, and is that easily teachable to other people from either background?
Re:What else are we getting wrong about programming languages? by nradov on Sunday October 30, 2016 @07:29PM (#12833160)
The simplest approach would be to take a dynamically typed language, like Python or something, and then add a full range of scalable type annotations that could be checked by the compiler (or a separate static analysis tool). The runtime system wouldn't have to change much, except perhaps to make the type annotations available through a reflection API.

C/C++ interop and OS interactions are separate issues mostly orthogonal to type systems.

Teaching would probably start without using the type annotations for simplicity. Let the students get something working first with basic dynamic code. Then teach the type annotations and reasons for using them in more advanced courses.

Re:What else are we getting wrong about programming languages? by raiph on Tuesday November 01, 2016 @12:48AM (#12843498)
The Perl 6 student begins:

    sub add(\number1, \number2) { number1 + number2 }
If you call `add` with simple numbers it works:

    say add(1, 2) # 3
Other types of number too:

    say add(0.1, 0.2) == 0.3 # True
It works even for newbie definitions of "number":

    say add('0.1', '0.2') == '0.3' # True
A sufficiently determined newbie can of course break `add`:

    say add('foo', 'bar') == 'waldo'
The program stops with a suitable error message ("Cannot convert string to number ...").

----

The student moves on to a more advanced course...

----

Type annotations are introduced:

    sub add(Numeric \number1,
            Numeric \number2) { number1 + number2 }
Everything still works as expected:

    say add(1, 2) # 3
But we get better error checking:

    say add('foo', 'bar') == 'waldo'
This generates a compile-time type-checking error ("Calling add(Str, Str) will never work with declared signature (Numeric \int1, Numeric \int2)").

So, reason 1 for using types: get type-checking that catches errors at compile-time.

You can use native types for closer-to-the-metal speed (compact representations, no automatic overflow checking, etc.):

    sub add(num64 \number1,
            num64 \number2) { number1 + number2 }
`num64` corresponds to C's double float datatype representation. This sub will likely outperform the versions of `add` that do not have natively typed parameters.

A third reason for explicit types is to take advantage of C interop:

    use NativeCall;
    sub add(int32, int32) returns int32 is native("calculator") { * }
If you've got a local C library (called "libcalculator.so" or somesuch) then this:

    say add(1,2)
will call the function with the symbol name `add` in that library.

And so on.

Re:What else are we getting wrong about programming languages? by perlgeek on Sunday October 30, 2016 @06:19PM (#12832755)
Gradual typing is awesome, though relatively hard to implement.

At one point you do have to make a decision though. If you come from a static type system, the compiler rejects programs if it can't proof their type correctness. Dynamic languages that introduce statics types typically do it the other way 'round: in case of doubt, allow the program at compile time, and possibly throw a type exception at run time.

You can try to make separate decisions in separate scopes, but then you have problems at the boundaries between such regions.

The point I'm trying to make is that gradual typing comes with its own complexity, both on the implementation side and through cognitive load for the programmer.

Re:What else are we getting wrong about programming languages? by stcredzero on Sunday October 30, 2016 @06:34PM (#12832860)
Gradual typing is awesome, though relatively hard to implement.

It doesn't have to be any harder than a language with a JIT VM. See Strongtalk and Dart.

At one point you do have to make a decision though. If you come from a static type system, the compiler rejects programs if it can't proof their type correctness. Dynamic languages that introduce statics types typically do it the other way 'round: in case of doubt, allow the program at compile time, and possibly throw a type exception at run time.

Also wrong. See Strongtalk and Dart. If you wanted to, you could make a variant of either system not compile your app without every type specified.

Re:What else are we getting wrong about programming languages? by sharpercoder on Sunday October 30, 2016 @06:37PM (#12832877)
Also see PHP, C# (dynamic and object), TypeScript.
Re:What else are we getting wrong about programming languages? by err4nt on Sunday October 30, 2016 @05:58PM (#12832618)
> That way you could start with something quick and dirty for a prototype and then gradually lock it down through progressive iterations.

Like tightening the bolts. This is a really interesting idea. I could also envision a utility wizard that you could run on a piece of code that could help guide you through making things more specific by asking you questions about the code. Are tools like this out there already? Like if you've defined a string and you run the utility it would ask: β€œIs this variable a number? Yes/No.” and then change to ensure it's a number in your source?

Re:What else are we getting wrong about programming languages? by ZenoArrow on Sunday October 30, 2016 @07:08PM (#12833065)
I don't know if such tools exist already, but it's conceptually similar to a linter, so perhaps you'll find something like this by investigating linters for languages with optional type annotations (such as Python).

EDIT: Found one for you:

http://mypy-lang.org/

Re:What else are we getting wrong about programming languages? by arikrak on Sunday October 30, 2016 @06:53PM (#12832987)
Dart does this to some extent. You can just declare general values with `var`, a number with `num` and then doubles or ints. https://dartpad.dartlang.org/f7559a6cf27ba0410699fac3d004269... https://www.dartlang.org/guides/language/language-tour#numbe...
Re:What else are we getting wrong about programming languages? by JBiserkov on Sunday October 30, 2016 @05:25PM (#12832437)
I agree. Have you seen Clojure spec? http://clojure.org/guides/spec
Re:What else are we getting wrong about programming languages? by eu90h on Sunday October 30, 2016 @05:31PM (#12832468)
There's been a lot of work on racketlang adding a gradual typing system. You can write without types, and when youre ready, refine your code by adding types where necessary.
Re:What else are we getting wrong about programming languages? by raiph on Monday October 31, 2016 @07:36PM (#12841956)
> false distinction between static versus dynamic typing

This is the Perl 6 view. See "Getting beyond static vs. dynamic" http://www.jnthn.net/papers/2015-fosdem-static-dynamic.pdf

In Perl 6:

> 1. A value.

    sub (\var) { say var }
> 2. A number.

    sub (Numeric \var) { say var }
> 3. A floating point number.

    sub (Num \var) { say var }
> 4. A floating point number between 0.0 and 100.0.

    sub (Num \var where 0.0 .. 100.0) { say var }
> 5. A floating point number between 0.0 and 100.0, and optimize for runtime performance instead of precision.

    sub (num \var where 0.0 .. 100.0) { say var }
> Ada and Microsoft Visual Basic incorporate some elements of this concept but didn't take it far enough.

Did they take it as far as Perl 6 per the above?

Re:What else are we getting wrong about programming languages? by zem on Sunday October 30, 2016 @06:15PM (#12832720)
the write up of stanza's optional typing system is a good read on the topic: http://lbstanza.org/optional_typing.html
Re:What else are we getting wrong about programming languages? by open-source-ux on Sunday October 30, 2016 @06:54PM (#12832993)
Does an enumerated type fulfil this? Pascal, for example, has had this for decades.
Re:What else are we getting wrong about programming languages? by nradov on Sunday October 30, 2016 @07:21PM (#12833127)
Enums are a separate issue. They are certainly helpful for some cases but are not an example of a scalable type system.