-1

In ActionScript 3 you can declare optional parameters like this:

function (i:int = 0, s:String = "", o:Object = null):void { }

So you can check if the user passed parameters s and o because you can test against the empty string or the null object if(s && o)...

But how do you allow an int to be truly optional? What if all values for i are valid, including 0, negative and positive integers? And what if you want to enforce integer (not use Number?)

what's the best practice here? the (...) rest may work but then you can't enforce a certain number of parameters at runtime, nor can you make for useful code completion?

I'm trying to implement a margin(top:int, right:int, bottom:int, left:int) method that allows right, bottom and left to be optional. Any thoughts?

taskinoor
  • 45,586
  • 12
  • 116
  • 142
Tom Auger
  • 19,421
  • 22
  • 81
  • 104

3 Answers3

2

You can use either int.MAX_VALUE or int.MIN_VALUE. See documentation here.

Constantiner
  • 14,231
  • 4
  • 27
  • 34
  • But what if the user inputs int.MAX_VALUE? – laurent Aug 06 '11 at 16:34
  • 1
    Come on! It is real life. Can you imagine somebody who enters 2147483647? – Constantiner Aug 06 '11 at 16:37
  • in fact, i'm not quite sure if that's possible for int X( – nicks Aug 06 '11 at 17:12
  • Beautiful. Why didn't I think of that? So far this is the only viable option if you want to strictly enforce int in the API. I'm going to wait to see what else pops up. +1 – Tom Auger Aug 07 '11 at 18:16
  • I understand your point but it's more likely that a user would input int.MAX_VALUE than said, 658324, because the first one is a constant, the second is not. If he wants to choose an arbitrary number, it's probably better to make it random (and large). – laurent Aug 07 '11 at 18:20
  • @Laurent Anyway the particular implementation depends on application. If we are talking about, for example, pixels I see no reason for passing `MAX_VALUE` even considering it as a constant. – Constantiner Aug 07 '11 at 18:23
2

You can use NaN to check whether a user has set the parameter or not, but you need to use Number instead of int. For something like setting margins it probably won't make any difference, since it probably won't be called thousands of time per second.

function margin(top:Number, right:Number = NaN, bottom:Number = NaN, left:Number = NaN) {
    // Then here test with isNaN(right), isNaN(bottom), etc.
}
laurent
  • 88,262
  • 77
  • 290
  • 428
  • As far as I know `NaN` for `int`'s will be converted to `0`. – Constantiner Aug 06 '11 at 16:38
  • Good point, I have updated my post. I guess he could use Number instead but it depends on what he wants to do. – laurent Aug 06 '11 at 16:48
  • Thanks for posting, but I have to ask without sounding mean: did you read my post? I clearly state "if you want to enforce integer (not use Number)" I put that in there because I know about the Number NaN bit, nad that's precisely what I don't want to do - I really want to enforce int from an API perspective. – Tom Auger Aug 07 '11 at 18:14
  • Ok then you can't really do what you are trying to do. The only solution is to use an arbitrary default like 4356314 (as another posters said, what are the chances someone would enter that) but that won't make for a clean API either. Number gives you more flexibility and you can still enforce int by checking the input. – laurent Aug 07 '11 at 18:19
0

If all possible int values are valid (i.e. you can't designate -1 as a special 'not supplied' value) then you won't be able to use the int datatype.

My suggestion is this (based on Laurent's answer):

Define the type as Number=NaN, and test for it's existence with isNaN().

If the user has supplied a value then convert the value to an int, otherwise assign it your default value.

function margin(top:Number, right:Number = NaN, bottom:Number = NaN, left:Number = NaN) {
    // Then here test with isNaN(right), isNaN(bottom), etc.
    if (isNaN(right))
    {
        right = DEFAULT_MARGIN;    // some default value.

        // Any other logic here...
    }
    else
    {
        // Enforce integer values.
        right = int(right);    // or use Math.floor(right);
    }
}

You can also use the ternary operator to reduce the number of lines:

function margin(top:Number, right:Number = NaN, bottom:Number = NaN, left:Number = NaN) {
    // Then here test with isNaN(right), isNaN(bottom), etc.

    // This is equivalent to the example above:
    right = isNaN(right) ? DEFAULT_MARGIN : int(right);
}
Sly_cardinal
  • 12,270
  • 5
  • 49
  • 50
  • Did you read the part where I wrote: "If all possible int values are valid ... then you won't be able to use the int datatype."? If MAX_VALUE is not a valid/expected user value then you've found your answer. – Sly_cardinal Aug 10 '11 at 11:31