List Info

Thread: Type Classes... (kind of)




Type Classes... (kind of)
user name
2007-08-24 10:10:31
I really like to stretch the capabilities of Haxe language...
I think that a very interesting extension of the inferencer would be typedef Union so that we could use the duck typing this way:

class Peon {
    public    function    new() ;   {
   ; }
    public function say ()    {
    }
    public function die ()    {
    }
    public    function sleep ()    {
    }
}

typedef SayDie = {    say : Void -> Void, die : Void -> Void   ; }
typedef Sleep = {    sleep : Void -> Void   ; }

var l = new List<SayDie, Sleep>();
l.add(peon);

OR
...
typedef SleepSayDie = { &nbsp;  SayDie, Sleep ; }

var l = new List<SleepSayDie&gt;();
...

I know that this is not type classes under the hood (no dictionnaries) but it is not so far from a user perspective and would gives us a lot of facilities...
The main advantage compared to interface is that it is completly external to the class; you don't need to create an interface and make classes derive from them to capture a specific set of methods/fields (Even Haskell needs to add the deriving clause; due to its lack of duck typing).

I think such a feature would be so great!
Do you think so?

Re: Type Classes... (kind of)
country flaguser name
France
2007-08-24 10:18:27
Stephane Le Dorze a écrit :
> I really like to stretch the capabilities of Haxe
language...

This discussion would be more appropriate on haXe list, but
I'll answer
here 

> I think that a very interesting extension of the
inferencer would be
> typedef Union so that we could use the duck typing this
way:
> 
> class Peon {
>     public    function    new()    {
>     }
>     public function say ()    {
>     }
>     public function die ()    {
>     }
>     public    function sleep ()    {
>     }
> }
> 
> typedef SayDie = {    say : Void -> Void, die : Void
-> Void    }
> typedef Sleep = {    sleep : Void -> Void    }
> 
> var l = new List<SayDie, Sleep>();
> l.add(peon);


You already have the following in haXe :

var s : SayDie = new Peon();

And :

var l = new List<Sleep>();
l.add(new Peon());

So there is already duck typing / type classes in haXe.

The only thing is that there is no syntax currently to
composite two
typedefs together, but you can extend one with more
definitions :

typedef SleepSayDie = {> SayDie, sleep : Void -> Void
}

Nicolas


-- 
Neko : One VM to run them all
(http://nekovm.org)

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )