A parameter that accepts separate types

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

A parameter that accepts separate types

Rezmason
Right now I have a function "drawGraphic(target:Dynamic, otherParams:Whatever):DisplayObject", which draws data onto a target object and then returns that object. That target object is either a Flash Shape or a Flash Sprite. Is there a way I can strictly type the first parameter so that the function only accepts values for that parameter that are Shape objects or Sprite objects?

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

Re: A parameter that accepts separate types

MarcWeber
Excerpts from Jeremy Sachs's message of Wed Oct 06 18:01:06 +0200 2010:
> Right now I have a function "drawGraphic(target:Dynamic, otherParams:Whatever):DisplayObject", which draws data onto a target object and then returns that object. That target object is either a Flash Shape or a Flash Sprite. Is there a way I can strictly type the first parameter so that the function only accepts values for that parameter that are Shape objects or Sprite objects?

a) use two functions calling a a common (dynamic one) :-(

b) maybe interfaces works:


    typedef MyDrawInterface = {
      function draw(x:Int, y:Int):Void;
    }

    class Sprite {
      public function draw(x:Int, y:Int){
        trace(x+" "+y);
      }
    }

    class Shape {
      public function new() {
      }
      public function draw(x:Int, y:Int){
        trace(x+" "+y);
      }
    }

    class Test {

      function new(arg) {
      }
     
      static public function drawGraphic(dyn: MyDrawInterface){
        dyn.draw(2,2);
      }

      static function main() {
        drawGraphic(new Foo());
        drawGraphic(new Sprite());
      }    

    }


I have no more ideas.

Marc Weber

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

Re: A parameter that accepts separate types

Pimm Hogeling
In reply to this post by Rezmason
You probably need a type that defines the graphics property. Adobe should've made an interface for this, but haXe provides typedefs which can basically do the same thing.

You could use this:
import flash.display.Graphics;
typedef GraphicsObject = {
    public var graphics(default, null):Graphics;
}

Your method would look like this:
drawGraphic(target:GraphicsObject, otherParams:Whatever):GraphicsObject;

On 6 October 2010 18:01, Jeremy Sachs <[hidden email]> wrote:
Right now I have a function "drawGraphic(target:Dynamic, otherParams:Whatever):DisplayObject", which draws data onto a target object and then returns that object. That target object is either a Flash Shape or a Flash Sprite. Is there a way I can strictly type the first parameter so that the function only accepts values for that parameter that are Shape objects or Sprite objects?

-Jeremy
--
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: A parameter that accepts separate types

Hudson Ansley
If I understand http://haxe.org/ref/type_advanced, you should be able to

typedef GraphicsObject = { >DisplayObject,  graphic:Graphics};

that way you can get all the other DisplayObject properties in
GraphicsObject, but you should read the doc page because there are
some provisos when extending a class like this...
quoting the docs:
>>>>>>>>>>>>>
Cascading typedefs can be created from normal class types as well, but
the compiler will not be able to match this typedef against an
anonymous object, or any other instanced class. This is because haXe
is not fully structurally subtyped. Structural subtyping can only
occur between anonymous typedefs or between an instance and an
anonymous typedef.
<<<<<<<<<<<<<

Regards,
Hudson

On Wed, Oct 6, 2010 at 2:09 PM, Pimm Hogeling <[hidden email]> wrote:

>
> You could use this:
> import flash.display.Graphics;
> typedef GraphicsObject = {
>     public var graphics(default, null):Graphics;
> }
>
> Your method would look like this:
> drawGraphic(target:GraphicsObject, otherParams:Whatever):GraphicsObject;
>

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