FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

A bit of static typing

 
Post new topic   Reply to topic     Forum Index -> MiniD
View previous topic :: View next topic  
Author Message
Linker



Joined: 06 Nov 2006
Posts: 29
Location: Almaty, Kazakhstan

PostPosted: Thu Aug 30, 2007 11:18 am    Post subject: A bit of static typing Reply with quote

Hello.

Could you add some static typing into MiniD? I think that in some cases it is really needed: this would improve code safety, maybe speed Smile

Take a look at ABAP: http://en.wikipedia.org/wiki/ABAP
Old code mostly uses dynamic typing, but all code that non-SAP programmers should use (BAPI, etc.) uses static typing so that you can't pass unexpected parameters when calling functions, etc.

Imagine a situation: you have tons of gamecode for an RPG, and then you notice that when your character is equipped with level 25 hammer and tries to fight an ogre of level 4, the game crashes due to incorrect parameter type. Smile Now imagine that you're not the one who wrote the code, -- you let game designers do it. Smile

Best regards.
Back to top
View user's profile Send private message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Fri Aug 31, 2007 8:07 am    Post subject: Reply with quote

I've had an idea for MiniD 2 for parameter type constraints which would look something like:

Code:
function foo(x : int, y : int | float, z, w : instanceof Foo)


This means x can take ints, y can take ints or floats, z can take anything (just like parameters now), and w can take instances of class (or any class derived from) Foo.

Of course, until then, you can use the type checking and assert() functions in the baselib to accomplish much the same thing:

Code:
function foo(x, y, z, w)
{
    assert(isInt(x) && (isInt(y) || isFloat(y)) && (isInstance(w) && w as Foo));
}
Back to top
View user's profile Send private message
Linker



Joined: 06 Nov 2006
Posts: 29
Location: Almaty, Kazakhstan

PostPosted: Fri Aug 31, 2007 10:30 am    Post subject: Reply with quote

Returning to ABAP experience, I think that something like:
Code:

function foo( int x, int y, z, Foo w );

will suffice Smile

Yes I know about those asserts but, I think that the line of code above will make it clear about what types of parameters are expected and will not post-pone type check to run-time.
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Fri Aug 31, 2007 3:19 pm    Post subject: Reply with quote

JarrettBillingsley wrote:
Code:
function foo(x : int, y : int | float, z, w : instanceof Foo)

Out of curiousity, is the 'instanceof' keyword neccessary in there? Would it not be able to work without?
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
r.lph50



Joined: 27 Nov 2006
Posts: 21
Location: New Zealand

PostPosted: Sat Sep 01, 2007 3:20 am    Post subject: Reply with quote

This is sort of off-topic but if we are talking about function signatures.. it would be awesome to have a type (or even just fancy symbol) that made whatever was passed as an argument into a syntax tree rather than a MiniD value. By syntax tree I'm jealously looking at: ParseTree.

(Oh and I haven't yet said: great work on the 1.0 release Very Happy)
Back to top
View user's profile Send private message AIM Address
doob



Joined: 06 Jan 2007
Posts: 367

PostPosted: Sat Sep 01, 2007 7:07 am    Post subject: Reply with quote

I think the syntax should be as Linker suggests, maybe you can add the "or" thing like this:
Code:
function foo( int x, int | float y, z, Foo w );

Maybe it should be two "|" ?

I don't like the colon and instanceof syntax, it would be better to follow the D syntax as close as possible, as you have specified in the minid overview: "...with a C-style syntax and D-like semantics".
Back to top
View user's profile Send private message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Sat Sep 01, 2007 9:41 am    Post subject: Reply with quote

There are two issues that have been brought up: the syntax and whether the check should be done at runtime or not.

The "x : int" syntax has the nice advantage that it's backwards compatible with old MiniD code. Since the type constraints are optional and delimited by a colon, MiniD 1 code (and especially code that uses these "reserved words" as parameter names, i.e. "function toInt(string)" or something) will compile fine. I suppose "x : Foo" isn't really ambiguous either, so the 'instanceof' wouldn't be necessary. On the other hand, if there is enough of a push for the more traditional C-style parameter syntax, I suppose I could make a clean break for MiniD 2. Which seems to be looming closer than I would have imagined Shocked

Secondly, the issue with trying to do compile-time typechecking is that you cannot, in the general case, know what a symbol refers to at compile time. Some other languages (like D) bind a function or class to a given name, and you cannot reassign what that name refers to ever. In MiniD, class and function declarations are nothing more than syntactic sugar for assignments of new class or closure objects into local or global variables. Variables are always mutable, and as such, you can reassign what a variable refers to at runtime. Furthermore, you don't know anything about the environment in which a given piece of code is being run, so symbol references that appear to reference nothing at all might be entirely legal once the code has been loaded into the proper environment.

Because of all that, and because variables themselves are not typed, you cannot tell if a given function call is valid at compile time. Either the name of the function hasn't yet been assigned a value, or that name refers to nothing at all, or if it's an object method you have no clue where to look up the method signature, etc. etc. etc. Basically typechecking has to be done at runtime. Introducing a special syntax for it can offer possibilities for optimization (i.e. parameters of non-user-defined types could be checked with a bitmask) that the current assertion method cannot. Furthermore, since these checks are often only needed when testing code, they can be disabled (probably by a compiler flag) when the code has been sufficiently tested to be pretty damn sure that it doesn't have any errors.

As I've mentioned before, the very dynamic nature of MiniD makes for some rather interesting design restrictions. There are some really cool language features, but some of them just can't fit into MiniD without a massive redesign of the language, in both its design and its philosophy.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> MiniD All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group