[feature req.] optional variable in typedef

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

[feature req.] optional variable in typedef

Ali Jaya Meilio Lie
Hi list :D *start with smile
I have a problem.
Sometimes i work with typedef, and i want to create a object of it first n insert the value later.

I mean like this:

typedef Test = {a: Sprite, b: String, c: Array<Int>}

class Main
{
 public static function main()
 {
  var test:Test = { a:null, b:null, c:null };
  test.a = new Sprite();
  test.b = "test";
  test.c = new Array<Int>();
 }
}

It is a pain to write all to null first.
And if i use dynamic, the code is not elegan enough. :-P

If i use this code:
var a: Sprite = new Sprite();
var b: String = "test";
var c: Array<Int> = new Array<Int>();
var test:Test = {a:a, b:b, c:c};

I will be tired soon if i use it in function argument.

I request the typedef become like this
typedef TypedefName = {a:Type, ?b:Type, ?c:Type = defaultValue}

The variable name without question mark before it is a required value in declaration of typedef.
The variable with question mark before it is a optional value in declaration of typedef, if it doesn't define default value, its default value become null.

Any suggestion about this?
Is it possible?

Thanks,
Ali

---- 
Sent using a Sony Ericsson mobile phone
--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: [feature req.] optional variable in typedef

Ian Martins
Ali Jaya Meilio wrote:

> Hi list :D *start with smile
> I have a problem.
> Sometimes i work with typedef, and i want to create a object of it first n insert the value later.
>
> I mean like this:
>
> typedef Test = {a: Sprite, b: String, c: Array<Int>}
>
> class Main
> {
>  public static function main()
>  {
>   var test:Test = { a:null, b:null, c:null };
>   test.a = new Sprite();
>   test.b = "test";
>   test.c = new Array<Int>();
>  }
> }
>
> It is a pain to write all to null first.
> And if i use dynamic, the code is not elegan enough. :-P
>
> If i use this code:
> var a: Sprite = new Sprite();
> var b: String = "test";
> var c: Array<Int> = new Array<Int>();
> var test:Test = {a:a, b:b, c:c};
>
> I will be tired soon if i use it in function argument.
>
> I request the typedef become like this
> typedef TypedefName = {a:Type, ?b:Type, ?c:Type = defaultValue}
>
> The variable name without question mark before it is a required value in declaration of typedef.
> The variable with question mark before it is a optional value in declaration of typedef, if it doesn't define default value, its default value become null.
>
> Any suggestion about this?
> Is it possible?
>
> Thanks,
> Ali
>
> ---- 
Sent using a Sony Ericsson mobile phone
can you create a (static) function that takes optional arguments and
builds the object?

    function createTypedefName(a:Type, ?b:Type, ?c:Type = defaultValue)
    {
       if (b==null)
          b = "test";
       var test:Test = { a:a, b:b, c:c };
       return test;
    }


then you can build the object with

    typedef TypedefName = createTypedefName(a);


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