www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - like types

reply bearophile <bearophileHUGS lycos.com> writes:
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
Aug 06 2011
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
This seems to be to be nothing more than applying the same
idea behind objects and interfaces to other types... if all
variables were typed Object and you had:

interface Int {}
interface String {}

void whatever(Int lineno, String rawdata) {}

it'd be the same thing, would it not?
Aug 06 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
Adam D. Ruppe Wrote:

 This seems to be to be nothing more than applying the same
 idea behind objects and interfaces to other types... if all
 variables were typed Object and you had:
 
 interface Int {}
 interface String {}
 
 void whatever(Int lineno, String rawdata) {}
 
 it'd be the same thing, would it not?

In their language (here adapted to D syntax) this compiles with no errors, and it gives an error at runtime in the first line of foo, because the dynamic type of x is string, and it's not like an int: int foo(like int y) { return y + 1; } void main() { dyn x = "bar"; foo(x); } Is this the same thing you are saying? Bye, bearophile
Aug 07 2011
parent Adam D. Ruppe <destructionator gmail.com> writes:
bearophile:
 Is this the same thing you are saying?

Yeah, though in D you'd have to use objects and interfaces rather than primitives. Casting to the interface would return null if it doesn't match, so it compiles but then errors at runtime.
Aug 07 2011
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 8/6/2011 7:24 PM, bearophile wrote:
 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

D has opDispatch which fills that role nicely.
Aug 06 2011
parent Kagamin <spam here.lot> writes:
Walter Bright Wrote:

 On 8/6/2011 7:24 PM, bearophile wrote:
 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

D has opDispatch which fills that role nicely.

opDispatch doesn't work with interfaces and you can't call it on an Object.
Aug 07 2011