like types

bearophile bearophileHUGS at lycos.com
Sat Aug 6 19:24:24 PDT 2011


C#4 has "dynamic" type, it allows to perform dynamic typing style code in C#:
http://en.wikipedia.org/wiki/C_sharp_4#Dynamic_member_lookup

But I have found a paper about one year old that presents a type that I like more than that C#4 feature, "Integrating Typed and Untyped Code in a Scripting Language" by Tobias Wrigstad, Francesco Zappa Nardelli et al.:
http://moscova.inria.fr/~zappa/projects/liketypes/

The idea is rather simple, for me it's new, and it's different from the gradual typing you see in recent versions of Racket Scheme. It's a class of types that are a particular intermediate point between dynamic and static typing, with some advantages of both and its even seems easy enough for a human to modify and convert code from dynamic typing to this intermediate typing to static typing. So this idea is also meant for 'program evolutution' too, from dynamically typed prototipes to statically typed code. (If you no appreciation for dynamic typing, then you will probably not appreciate this idea).


>From the paper:

>We introduce a novel intermediate point, dubbed a "like type," between dynamic and compile-time checked static types. For each type "C", there is a type "like C". Uses of variables of type like C are checked statically and must respect C's interface. However, at run-time, any value can flow into a like C variable and their conformance to C is checked dynamically. Like types allow the same degree of incrementality as previous proposals for gradual typing, but we have chosen a design which favors efficiency. In contrast to inference-based systems, like types allow static checking of operations against an explicit, programmer-declared, protocol. Notably, this allows catching spelling errors and argument type errors which are simple and frequent mistakes. Furthermore they make it possible to provide IDE support such as code completion.<

>as an intermediate step between the two, we propose like types. Like types combine static and dynamic checking in a novel way. For any concrete type C, there is a corresponding like type, written like C, with an identical interface. Whenever a programmer uses a variable typed like C, all manipulations of that variable are checked statically against C's interface, while, at run-time, all uses of the value bound to the variable are checked dynamically.<

The language they use to implement and present like types also contains "dyn" types, that are fully dynamic like the "dynamic" of C#4.


An example that uses some like types:

class ParserBase(var lineno: like Int,
                 var offset: like Int,
                 var rawdata: like String) {
    def upos(i: like Int, j: like Int): like Int {
        if (i >= j) return j;
        nlines: Int = count(rawdata.slice(i, j), "\n");
        ...
    }
}


The paper later shows that evolving code from dynamic to like types to statically typed is not too much hard.

Bye,
bearophile


More information about the Digitalmars-d mailing list