views:

54

answers:

2

I'm in the process of refactoring a Flex application into a "library project", and one of the, err, interesting errors I've come across involves a function like this:

function spam(eggs:*=undefined):void {
    ...
}

While it was a "Flex application", this function compiled without issue… But when I try to build it as a "library project", the compiler gives me the error:

1047: Parameter initializer unknown or is not a compile-time constant.

So, uuhh… Why? And is there anything I can do to fix that?

A: 

Is the class with this code being used at all? A Flex Project may have optimized it out of the project if it's never used; thus not throwing an error. A library project would not do that.


I would recommend using null as the default value instead of undefined. Isn't there a bit of a paradox to initialize a value as 'undefined' ?

www.Flextras.com
Yes, it was being referenced when it wasn't a library project.
David Wolever
Duh me, I have no idea why the syntax was unfamiliar. Instead of undefined; use null as the default value.
www.Flextras.com
The reason I don't want to do that is that `undefined` has a different meaning then `null`: `undefined` means "no value has been defined" while `null` means "a value has been defined and that value is `null`".
David Wolever
Fair enough; I still say that setting a value to undefined is a paradox.
www.Flextras.com
+2  A: 

There's a bug in jira (link) that says mxmlc and the flash compiler behave differently when working with parameter initializers. As the library projects are compiled using a different compiler (compc in place of mxmlc) I suspect it might be the same issue.

You can probably change the function to something like this, if you need it to be undefined:

function spam(eggs:*=null):void {
   if (eggs is null) eggs = undefined;
}
Robert Bak
Ah, that's good to know. Thanks.Also, I'd rather not use `null` if I can avoid it because it's possible that `eggs` will be explicitly null (eg, `spam(foo)` where `foo` is null).
David Wolever
Ok, this is a dirty workaround, but it should work, the idea is to use something which isn't likely to apear as a parameter (instead of null):function spam(eggs:*='im not set - and no one will pass this string here'):void { if (eggs == 'im not set - and no one will pass this string here') eggs = undefined;}
Robert Bak
hahaha yup, I'd thought about doing just that ;)Fortunately (?), though, after some profiling, I found that turning the app into a library project didn't make builds any faster… So the refactoring was aborted and it's not an issue.
David Wolever