A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Vector as function default parameter

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    9

    Vector as function default parameter

    Hi everyone.

    Does anyone know what is the syntax to use for a function's default parameter when it's a Vector?
    You can easily create a default value for an int or Number or even an Array, but what about a Vector?

    Thanks

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I don't think you can do that easily, since the default value must be statically determinable. I'm mildly surprised it works with arrays.

    The workaround would be to make the default value null, and then check for null inside the function and use a runtime determined default value at that time.
    Code:
    function someFunction(arg:Vector.<Number> = null):void{
      if (null == arg){
        arg = Vector.<Number>([1, 3, 5, 7]);
     }
     //do stuff
    }

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks!

    I knew it was not possible directly, what a shame! X(

    Thanks for your workaround suggestion though. I'll see what I can do with that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center