Static type assertions, good method

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Static type assertions, good method

edA-qa mort-ora-y
Does anybody have a good way of doing static type assertions? That is,
how to ensure that a given type inherits from another specific type.
This is relevant with package renaming (ala flash) and when using a
preprocessor.

That is, given these two core classes:

class BaseRound

class HandRound extends BaseRound

there is also a "Round" class to be defined by the particular game.
Then I have another common class:

class HandGame {
  var myRound : Round;
}

Here I need to ensure that Round is derived from HandRound, otherwise
compilation will fail. The best I have so far is in the constructor to
do an assignment:

public function new( round : Round ) {
  var check : HandRound = round;
}

This won't compile if "round" is not an instance of HandRound. I'm
assuming this will always fail at compile time -- though I'm hoping it
won't be in the final output code as it has no effect.

Is there are any other way to do this, perhaps something clearer?


NOTE: No, I can't make HandGame use type HandRound. This is part of a
package inheritance scheme and Round must be final class in this case.


--
edA-qa mort-ora-y
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

BigTPoker uses haXe and DHLIB
        http://BigTPoker.com/?source=haxe-list

The dis-Emi-A haXe Library
        http://wiki.disemia.com/HaXe
       
A full set of tools, classes, and support facilities aimed at
simplifying and expediting game creation in Flash 9.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.


--
haXe - an open source web programming language
http://haxe.org

signature.asc (260 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Static type assertions, good method

Viktor Hesselbom
Maybe I'm not seeing the problem but why can't you not just have a HandRound as parameter for the constructor?

public function new (round : HandRound)

Then the passed object will always extend HandRound.

2010/4/8 edA-qa mort-ora-y <[hidden email]>
Does anybody have a good way of doing static type assertions? That is,
how to ensure that a given type inherits from another specific type.
This is relevant with package renaming (ala flash) and when using a
preprocessor.

That is, given these two core classes:

class BaseRound

class HandRound extends BaseRound

there is also a "Round" class to be defined by the particular game.
Then I have another common class:

class HandGame {
 var myRound : Round;
}

Here I need to ensure that Round is derived from HandRound, otherwise
compilation will fail. The best I have so far is in the constructor to
do an assignment:

public function new( round : Round ) {
 var check : HandRound = round;
}

This won't compile if "round" is not an instance of HandRound. I'm
assuming this will always fail at compile time -- though I'm hoping it
won't be in the final output code as it has no effect.

Is there are any other way to do this, perhaps something clearer?


NOTE: No, I can't make HandGame use type HandRound. This is part of a
package inheritance scheme and Round must be final class in this case.


--
edA-qa mort-ora-y
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

BigTPoker uses haXe and DHLIB
       http://BigTPoker.com/?source=haxe-list

The dis-Emi-A haXe Library
       http://wiki.disemia.com/HaXe

A full set of tools, classes, and support facilities aimed at
simplifying and expediting game creation in Flash 9.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Static type assertions, good method

edA-qa mort-ora-y
Viktor Hesselbom wrote:
> Maybe I'm not seeing the problem but why can't you not just have a
> HandRound as parameter for the constructor?
> public function new (round : HandRound)
> Then the passed object will always extend HandRound.

Where possible this is done, but it isn't always possible. Some users of
this class, and functions it calls, need the final "Round" type to be
available and not the base class "HandRound".


--
edA-qa mort-ora-y
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

BigTPoker uses haXe and DHLIB
        http://BigTPoker.com/?source=haxe-list

The dis-Emi-A haXe Library
        http://wiki.disemia.com/HaXe
       
A full set of tools, classes, and support facilities aimed at
simplifying and expediting game creation in Flash 9.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.


--
haXe - an open source web programming language
http://haxe.org

signature.asc (260 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Static type assertions, good method

Viktor Hesselbom
If the parameter has to be a HandRound then surely you are calling some method specific to that class. Then the compiler will notice that the Round class doesn't have these methods.

I don't know, I've never really had this problem.

2010/4/8 edA-qa mort-ora-y <[hidden email]>
Viktor Hesselbom wrote:
> Maybe I'm not seeing the problem but why can't you not just have a
> HandRound as parameter for the constructor?
> public function new (round : HandRound)
> Then the passed object will always extend HandRound.

Where possible this is done, but it isn't always possible. Some users of
this class, and functions it calls, need the final "Round" type to be
available and not the base class "HandRound".


--
edA-qa mort-ora-y
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

BigTPoker uses haXe and DHLIB
       http://BigTPoker.com/?source=haxe-list

The dis-Emi-A haXe Library
       http://wiki.disemia.com/HaXe

A full set of tools, classes, and support facilities aimed at
simplifying and expediting game creation in Flash 9.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Static type assertions, good method

edA-qa mort-ora-y
Viktor Hesselbom wrote:
> If the parameter has to be a HandRound then surely you are calling some
> method specific to that class. Then the compiler will notice that the
> Round class doesn't have these methods.
> I don't know, I've never really had this problem.

The problem is common in cases where you have a library which needs to
be extended. No language provides package inheritance thus you're force
to make due.

Consider this class hierarchy:

BaseRound -> HandRound -> Round*
BaseGame -> Game*
BaseLayout -> HandLayout -> Layout*

The libary does /not/ define the items marked with *. That is, the
library does not constitute a complete set of code on its own, the
classes are abstract (in C++/Java terms).

So I wish to create a specific game I must define Round, Game and Layout
to extend from the base classes. I do so as above.  Now consider that
HandLayout requires that a Round derives from HandRound. But on top of
this HandLayout needs to call a function in Game which requires the
final Round object. If I declare Round to be HandRound in HandLayout I
can't call the Game function, since I don't have the correct type. So
instead I make the abstract classes work with the final type Round.

This comes back to my problem, since HandLayout simply works with a
Round object it needs some way to guarantee that Round has been derived
from HandRound.

--
edA-qa mort-ora-y
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

BigTPoker uses haXe and DHLIB
        http://BigTPoker.com/?source=haxe-list

The dis-Emi-A haXe Library
        http://wiki.disemia.com/HaXe
       
A full set of tools, classes, and support facilities aimed at
simplifying and expediting game creation in Flash 9.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.


--
haXe - an open source web programming language
http://haxe.org

signature.asc (260 bytes) Download Attachment