Flash

1046: Type was not found or was not a compile-time constant

Came across this little oddity the other day. Took me ages to work out what was going on, so thought I would share in case anyone else ran into the same issue.

One day, for a reason I couldn’t fathom, my project stopped compiling. I kept getting these odd “1046: Type was not found or was not a compile-time constant” errors all over the place. Not only that, when I tried to include the class in question either via auto-complete (control & space) or via manual import the error persisted.

To cut a long story short it seems that if you try to new a member property that is of type Class from another class and the constructor takes in at least one parameter the error will occur.

So for example take the two following classes:

package package2
{
	import package1.MyTestClass;
 
	public class MyTestClass2
	{
		public var type : Class = MyTestClass;
	}
}

And

package package1
{
	public class MyTestClass
	{
		public function MyTestClass(someVar:String)
		{
			trace(someVar);
		}
	}
}

Now try using them in the following fashion:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
			   xmlns:s="library://ns.adobe.com/flex/spark"
			   xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="application1_creationCompleteHandler(event)">
 
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;			
 
			protected function application1_creationCompleteHandler(event:FlexEvent):void
			{
				var class2 : MyTestClass2 = new MyTestClass2();
				var class1 : MyTestClass = new (class2.type)("hello");
			}
 
		]]>
	</fx:Script>
 
</s:Application>

And uh oh, bad times:

1046: Type was not found or was not a compile-time constant: MyTestClass.	FlexBugExperiment.mxml	/FlexBugExperiment/src/main	line 14	Flex Problem
 
1046: Type was not found or was not a compile-time constant: MyTestClass2.	FlexBugExperiment.mxml	/FlexBugExperiment/src/main	line 13	Flex Problem
 
1180: Call to a possibly undefined method MyTestClass2.	FlexBugExperiment.mxml	/FlexBugExperiment/src/main	line 13	Flex Problem

The bad line is:

var class1 : MyTestClass = new (class2.type)("hello");

If you take away the “hello” part or you split it out into two lines like so:

var tmpC : Class = (class2.type);
var class1 : MyTestClass = new tmpC("hello");

Then everything is gravy

Anyway, I hope this helped someone out!

Funk IoC – A New Dependency Injection Framework

Twitter can be a funny beast, what makes it great can also make it poor. I use Twhirl which keeps me updated any time one of the people I follow tweets about something, the only problem is that so many people tweet that if I dont happen to see it within about and hour or so of the Tweet, ill miss it. This time however I was lucky enough to catch a tweet by @Joa about his new Inversion of Control and functional-programming-like library, Funk AS3.

As I have been getting well into RobotLegs (a Dependency Injection MVCS framework) recently I was extremely interested to hear about this new project by Joa who I respect very much as a brilliant coder not least because of his excellent work on low-level Flash byte-code optimisation (see Apparat).

Joa has taken a different approach to doing dependency injection. The approach most frequently used (and the one used in SwiftSuspenders / RobotLegs) is to use meta-data to declare to a number of variables for injection. You then map a class to be injected and instantiate it using the injector.

As an example, with Swift Suspenders you would define a class for injection with something like the following:

  1. class MyInjectedClass
  2. {
  3. public function sayHello(toSay:String)
  4. {
  5. trace("Hello "+toStay);
  6. }
  7. }
  8.  
  9. var injector : Injector = new Injector();
  10. injector.mapSingleton(MyInjectedClass);

Here we are telling the Injector to make a single instance of our class and hold it internally ready for when it is next requested, such as:

  1. class MyDependantClass
  2. {
  3. [Inject] public var myClass : MyInjectedClass;
  4.  
  5. public function performAction()
  6. {
  7. myClass.sayHello("World");
  8. }
  9. }

Here the [Inject] meta-data indicates that we want the framework to supply the class with an instance of type MyInjectedClass. The final part is to make an instance of the dendant class and inject into it:

  1. var dependant : MyDependantClass = new MyDependantClass();
  2. injector.injectInto(dependant);
  3. dependant.performAction();

As you can see this is a nice way of handling inter-module dependencies in your code, when coupled with a MVC framework such as RobotLegs it becomes and extremely powerful yet elegant way of coding.

It however isnt perfect and Joa, on his google code page mentions three drawbacks of this method:

  • Your injected properties are publicly exposed and mutable.
  • describeType is very expensive.
  • Steep learning curve.

This is where he suggests his alternative method, which is quite ingenious. Using the same example as above you would see something like the following:

  1. class MyInjectedClass
  2. {
  3. public function sayHello(toSay:String)
  4. {
  5. trace("Hello "+toStay);
  6. }
  7. }
  8.  
  9. bind(MyInjectedClass).asSingleton();

Then the dependant class would look like the following:

  1. class MyInjectedClass
  2. {
  3. protected var myClass : MyInjectedClass = inject(MyInjectedClass);
  4.  
  5. public function performAction()
  6. {
  7. myClass.sayHello("World");
  8. }
  9. }

And making an instance of it could be as simple as:

  1. var dependant : MyDependantClass = new MyDependantClass();
  2. dependant.performAction();

As can be seen there are some benefits to this method, the biggest one in my opinion is that injected properties dont have to be public as they are provided by the call from within the class scope rather than from outside.

So how does Joa perform this magic? By abusing a little used ability of the Actionscript programming language known as package-level-functions. These are throwbacks from the old AS1 & AS2 days of global functions. There are actually a couple of common examples in AS3 still such as getTimer() and getQualifiedClassName() still used. What Joa has done is to use these package level functions as a method of generating concise looking code reminiscent of functional programming.

Performance wise, im not entirely sure whether by using package-level functions instead of describeType() calls used in meta-data driven IoC frameworks is any faster as Till Schneidereit of Swift Suspenders suggests:

I don’t think that Funk’s approach is any faster than an optimized
metadata-based IoC container: describeType may be slow (as in “takes a
few dozen microseconds to run”), but is only ever called once for each
class an instance of which is injected into. After that, it’s just a
straight iteration over an array for all injection points instead of
Funk’s multiple method calls for each injection point.

So the next step for me is to run some tests to see how things pan out. Either way im very impressed with both approaches and cant wait to see what kind of exciting advances will be developed in the coming months.

BlastWave: Lost at Sea

FINALLY ITS LIVE!!!! Oh my, it has taken literally months and months to get all the deals sorted but finally now its done and out.

Its Oliver and my latest game. It was supposed to be a quick re-skin of BlastWave, but as usual “quick” was a word that got lost during development as we got excited about it and decided to add more and more features.

The Art by Oliver Pitceathly follows a simmilar style to MonkeyMines. Hope you like it!

Read More…

1 2 3 4 5 6 7 8 9 10 11  Scroll to top