Gowtham
Gowtham

Reputation: 941

How to use Non-assignment expressions in Flex as3 using Flex SDK?

I was looking in to ActionScript 3 Specification in that i noticed Non-assignment expressions under index -> 14 Expressions > 14.18 Non-assignment expressions. Is it possible to assign data type for a variable dynamically through ternary operator ?: like below as mentioned in docx..

A NonAssignmentExpression may be used where ever a TypeExpression may be used.

var x : hintString ? String : Number

If its is possible in as3 when i build this using flex SDK i got Syntax Error

> src\App.mxml(29):  Error: Syntax error: expecting semicolon before String.
                   var x1: h1 ? String : Number;

for the below code.

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:mx="library://ns.adobe.com/flex/mx"
            xmlns:s="library://ns.adobe.com/flex/spark"
            creationComplete="onComplete()">

    <fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        private function onComplete():void
        {
            //test 
            var h1:String = 'hai';
            var x1: h1 ? String : Number;
            Alert.show("Type of x1 :"+typeof(x1));

        }

    ]]>
    </fx:Script>

    <fx:Declarations>

    </fx:Declarations>

</s:Application>

If it is possible in as3 and why not in Flex ? I have checked in Adobe docs on conditional operator but nothing is mentioned there like the above why?

Upvotes: 3

Views: 103

Answers (2)

gabriel
gabriel

Reputation: 2359

Yes, really interesting question, thanks for pointing it. I tried:

var h1:String = null; //(if try var h1:String = 'hai' the output will be [class String]
var x1 = h1 ? String : Number;
// probably the same result as x1:*

trace(x1); // [class Number]

I just checked the abc of this test and when we use this approach or *, it uses coerce_a (Indicates to the verifier that the value on the stack is of the any type ( * ). Does nothing to value.)

I agree with @net.uk.sweet, about struggling to think of an application for this construct..

Upvotes: 1

net.uk.sweet
net.uk.sweet

Reputation: 12431

Interesting question. What you've written there is ActionScript 3.0 so the construct isn't possible regardless of whether you're writing pure ActionScript 3.0 or ActionScript 3.0 within a Flex context. I've worked with the language since it was born and I've never seen that construct before.

My best guess is that it must be an error in the AS 3.0 specification document. There's nothing about it in the proposed Proposed ECMAScript 4th Edition – Language Overview document and this Mozilla Tamarin bug report page says it's (quote) "not implemented nor implementable".

As a side-note, I'm struggling to think of an application for this construct.

Upvotes: 2

Related Questions