MiniD 1.0 released!
Jarrett Billingsley
kb3ctd2 at yahoo.com
Sat Aug 4 11:56:00 PDT 2007
"Christopher Wright" <dhasenan at gmail.com> wrote in message
news:f926br$2a08$1 at digitalmars.com...
> Is there any chance of support for optional strong typing? That's my main
> beef with Javascript; the one time I've used it for a production system,
> it took six hours to do something that would have been fifteen minutes
> with strong typing (due to a single bug), and I never did accomplish what
> I set out to do.
>
> At least function arguments should be typed, or typeable, in an
> object-oriented language. Lua doesn't need strong typing as much because
> it only has primitives, functions, and tables. But MiniD is
> object-oriented, and the only alternative to typing is reflection.[1]
> Maybe some sort of 'where' clause?
> function do_stuff(arg1, arg2) where arg2 : ExpectedClass {}
I think you must have been reading my personal design notes :) Function
parameter type constraints is a feature I've been considering for v2.0.
They'd look something like this:
function f(x : int, y : !int, z : int | float, w : instanceof Foo) { ... }
x : int means only ints are allowed; y : !int means anything _but_ ints; z :
int | float means z can take an int or a float, and w : instanceof Foo means
it has to be an instance of class Foo (or any class derived from it).
I also had an idea for "semi-static typing" last year when I was making the
transition from static typing to dynamic typing. Basically variables could
be typed (but didn't have to be), but it wouldn't go any further than one
"level" of typing deep. That is, you could do:
int x = 5;
array a = [1, 2, "hi"];
Basic types are not really affected, but aggregate and container types are
just typed to the "first level": 'a' can only hold arrays, but there's no
way to specify what should be _in_ that array. Notice that a holds ints and
strings.
I'm wondering how feasible this would be to implement. The static typing
all happens at compilation, and then the compiler could emit instructions
for optimized operations, i.e. if it knows that two operands of + are ints,
it can output an "iadd" instruction which skips dynamic type checking on the
operands at runtime and knows that the operands are ints.
Who knows, it could eventually make it in :) It'd probably be optional in
order to largely preserve backwards compatibility, but it probably wouldn't
be too much of a departure from the current design.
More information about the Digitalmars-d-announce
mailing list