Returning different sub-classes from a function? - apache-flex

Say I have four sub-classes of 'Car'. One for each color. I want to have one function that can build and return a 'color-car' sub-class based on the passed value. This is a dumb example, I know, but it is precisely what I am trying to do only on a smaller scale.
public class Car
{
}
public class BlueCar extends Car
{
}
You get it.
Then, in another (helper) class, I have a function which takes in a string of the color and returns the correct sub-class.
public function GetCarFromColor(_color:String):Car
{
if (_color == "blue")
{
var myCar:BlueCar = new BlueCar;
return myCar;
} else if (_color == "red")
{
var myCar:RedCar = new RedCar;
return myCar;
}
Ok. You get it. This doesn't work for a reason unknown to me. I get 1118 errors which complain about conversion of BlueCar into Car, etc...
Can someone help me out here? Thanks!

Make the return variable to be of the supertype:
public function GetCarFromColor(_color:String):Car
{
var myCar:Car
if (_color == "blue")
{
myCar = new BlueCar;
return myCar;
} else if (_color == "red")
{
myCar = new RedCar;
return myCar;
}
This should now compile ok.

You should try casting your derived class to the base class before returning it back.
Not sure about actionscript but in C++ you could do it like this
Base *GetCarFromColor()
{
Base *b1;
b1 = new D1;
return b1;
}

Maybe you should use an interface instead?
public interface ICar
{
}
public class BlueCar implements ICar
{
}
public function GetCarFromColor(_color:String):ICar
{
}

The reason you were getting errors is because you have two local variables of the same name with different types in one function:
var myCar:BlueCar = new BlueCar;
var myCar:RedCar = new RedCar;
The variable myCar is typed as both BlueCar and RedCar. In ActionScript 3, variables are always scoped to the entire function. In other languages, like Java, I know that if statements and loops create a new block-level scope, but that's not the case here.
As Matt Allen suggested, typing myCar as the superclass, Car, should stop these compiler errors.

Related

Haxe: Binding pattern with abstract fields access methods

I'd like to make wrapper to implement simple data binding pattern -- while some data have been modified all registered handlers are got notified. I have started with this (for js target):
class Main {
public static function main() {
var target = new Some();
var binding = new Bindable(target);
binding.one = 5;
// binding.two = 0.12; // intentionally unset field
binding.three = []; // wrong type
binding.four = 'str'; // no such field in wrapped class
trace(binding.one, binding.two, binding.three, binding.four, binding.five);
// outputs: 5, null, [], str, null
trace(target.one, target.two, target.three);
// outputs: 5, null, []
}
}
class Some {
public var one:Int;
public var two:Float;
public var three:Bool;
public function new() {}
}
abstract Bindable<TClass>(TClass) {
public inline function new(source) { this = source; }
#:op(a.b) public function setField<T>(name:String, value:T) {
Reflect.setField(this, name, value);
// TODO notify handlers
return value;
}
#:op(a.b) public function getField<T>(name:String):T {
return cast Reflect.field(this, name);
}
}
So I have some frustrating issues: interface of wrapped object doesn't expose to wrapper, so there's no auto completion or strict type checking, some necessary attributes can be easily omitted or even misspelled.
Is it possible to fix my solution or should I better move to the macros?
I almost suggested here to open an issue regarding this problem. Because some time ago, there was a #:followWithAbstracts meta available for abstracts, which could be (or maybe was?) used to forward fields and call #:op(a.b) at the same time. But that's not really necessary, Haxe is powerful enough already.
abstract Binding<TClass>(TClass) {
public function new(source:TClass) { this = source; }
#:op(a.b) public function setField<T>(name:String, value:T) {
Reflect.setField(this, name, value);
// TODO notify handlers
trace("set: $name -> $value");
return value;
}
#:op(a.b) public function getField<T>(name:String):T {
trace("get: $name");
return cast Reflect.field(this, name);
}
}
#:forward
#:multiType
abstract Bindable<TClass>(TClass) {
public function new(source:TClass);
#:to function to(t:TClass) return new Binding(t);
}
We use here multiType abstract to forward fields, but resolved type is actually regular abstract. In effect, you have completion working and #:op(a.b) called at the same time.
You need #:forward meta on your abstract. However, this will not make auto-completion working unless you remove #:op(A.B) because it shadows forwarded fields.
EDIT: it seems that shadowing happened first time I added #:forward to your abstract, afterwards auto-completion worked just fine.

Google Guava - Filter Multiple Inputs on Single Property

I just got into Google Guava and it seems like a powerful tool and I see how you can use Predicates and filter by a specific property. How you can also chain predicates in FluentIterable My question is what's the best way to filter for a single property.
For example, if I have a collection of Cars. How do I filter the Cars.getPaintColor() to give me cars that are in Black, Red, and Yellow? Creating 3 separate predicates and using FluentIterable seems clumsy. Especially in my use, I could want possibly 10+ filters on the same property and I wouldn't want to create 10 Predicates.
Thanks you!
List<String> colorList = (List<String>)filterCriteria.get("Color");
List<String> makeList = (List<String>)filterCriteria.get("Make");
List<String> rimSizeList = (List<String>)filterCriteria.get("RimSize");
Predicate<String> predColor = Predicates.in(ImmutableSet.copyOf(colorList));
Predicate<CarObj> predDirection2 = Predicates.compose(predColor ,[????] );
Predicate<String> predMakeList = Predicates.in(ImmutableSet.copyOf(makeList));
Predicate<CarObj> predMakeList2 = Predicates.compose(predMakeList, [????] );
Predicate<String> predRimSize = Predicates.in(ImmutableSet.copyOf(rimSizeList));
Predicate<CarObj> predRimSize2 = Predicates.compose(predRimSize, [????] );
Collection<CarObj> filtered = FluentIterable.from(mAllCars)
.filter(predDirection2)
.filter(predMakeList2)
.filter(predRimSize2)
.toList();
Since I am using an List, I used copyOf instead of of when creating ImmutableSet.
I am not sure what to put in the second parameter of the compose. I am guessing it is something like this... in the CarObj class.
static Predicate<CarObj> byColor= new Predicate<CarObj>() {
public boolean apply(CarObj input) {
// What do I put here?
}
};
So, to check if a paint color is one of black, read or yellow, you'd want to create a Predicate that checks if a set contains that color:
Predicate<PaintColor> p = Predicates.in(ImmutableSet.of(
PaintColor.RED, PaintColor.BLACK, PaintColor.YELLOW));
You could then compose that with a Function<Car, PaintColor> that returns the paint color property of your class:
Predicate<Car> p2 = Predicates.compose(p, Car.GET_PAINT_COLOR_FUNCTION);
Edit:
By Car.GET_PAINT_COLOR_FUNCTION I just mean something like this:
public static final Function<Car, PaintColor> GET_PAINT_COLOR_FUNCTION =
new Function<Car, PaintColor>() {
#Override public PaintColor apply(Car car) {
return car.getPaintColor();
}
});
As I said in the comments, you can adapt that to your actual types as needed. For example, make it a Function<CarObj, String> instead.
The alternative to composing your extracting Function<Car, PaintColor> with a Predicates.in() as suggested by ColinD is to write your parameterized Predicate<Car>:
public class CarPaintColorPredicate implements Predicate<Car> {
private final PaintColor paintColor;
public CarPaintColorPredicate(PaintColor paintColor) {
this.paintColor = paintColor;
}
#Override
public boolean apply(#Nullable Car input) {
return input != null && input.getPaintColor() == paintColor;
}
}
which you can then use directly:
FluentIterable.from(cars)
.filter(new CarPaintColorPredicate(PaintColor.RED))
.toList();
or combine for multiple colors:
FluentIterable.from(cars)
.filter(Predicates.or(
new CarPaintColorPredicate(PaintColor.RED),
new CarPaintColorPredicate(PaintColor.BLACK)))
.toList();
or even combine with other types of predicates:
FluentIterable.from(cars)
.filter(new CarPaintColorPredicate(PaintColor.RED))
.filter(new CarMakePredicate("Ferrari"))
.toList();
To be complete, the version with the Function<Car, PaintColor> is as follows:
public enum CarPaintColorFunction implements Function<Car, PaintColor> {
INSTANCE;
#Override
public PaintColor apply(#Nullable Car input) {
return input == null ? null : input.getPaintColor();
}
}
The Function simply returns the value of the property, which is then compared to the collection (hopefully a Set) of accepted values through the Predicate composition:
FluentIterable.from(cars)
.filter(Predicates.compose(
Predicates.in(Sets.immutableEnumSet(PaintColor.RED, PaintColor.BLACK)),
CarPaintColorFunction.INSTANCE))
.toList();
All that's really explained in the Functional Explained page in the Guava Wiki.

actionscript variable name

I am a freshman for actionscript. I get some code from others and get confused on some syntax.
private static var _337833930conversationStartTimeList:ArrayList;
private static var conversationStartTimeList:ArrayList;
Whether these two represent same variables or different variables?
I think these should be the different ones but the get and set functions seem also work for the former one.
Does anybody help me to explain why is that and better to give me some references for further study.
Thanks
public static function get conversationStartTimeList() : ArrayList
{
return GeneralManager._337833930conversationStartTimeList;
}// end function
public static function set conversationStartTimeList(param1:ArrayList) : void
{
var _loc_3:IEventDispatcher = null;
var _loc_2:* = GeneralManager._337833930conversationStartTimeList;
if (_loc_2 !== param1)
{
GeneralManager._337833930conversationStartTimeList = param1;
_loc_3 = GeneralManager.staticEventDispatcher;
if (_loc_3 !== null)
{
_loc_3.dispatchEvent(PropertyChangeEvent.createUpdateEvent(GeneralManager, "conversationStartTimeList", _loc_2, param1));
}
}
return;
}// end function
These two have different names and they are declared in two separate statements - why would you think they are anything but two separate variables?

flex: referencing class variables

I have a bunch of variables in a class. There are situations when I want to set then to null/ "temp" etc as per a well defined logic. The challenge is to list out the variables at multiple places- tedious and error-prone.
classname.speed=NaN
classname.speedtype="not_set"
classname.distance=NaN
classname.distancetype="not_set"
Ideally, would prefer a way to refer to these variables programatically and set something like
"for all class variables- if variable ends in type, set as "not_set"; for other variables set as NaN
How can I achieve this? Any pointers will help
The simplest approach would be just write function to clear them all.
If you want something more automatic, it will requre efforts - look at introspection api. Basically, you call describeType on your class and it returns XML description. All variables will be listed there, along with other info. Then you can parse returned XML and set all variables to needed value, accessing them dynamically with square bracket syntax:
var myClass:MyClass = new MyClass();
myClass["varName"] = "new value";
It can be achieved through Inheritance i.e. implementing interface or extending class
which contains common fields
public class MyClass
{
public a:String = null;
public b:String = null;
public function MyClass()
{
}
}
which contains common var and Child Class could be
public class MyClassChild extends MyClass
{
public var c:String = null;
public function MyClassChild()
{
super();
this.a ="";
this.b ="";
}
}
and you can cast or use for each loop to set values
var temp:MyClassChild = new MyClassChild ();
MyClass(temp).a = "Hello World";
Hopes that helps

Is it possible to define a generic type Vector in Actionsctipt 3?

Hi i need to make a VectorIterator, so i need to accept a Vector with any type. I am currently trying to define the type as * like so:
var collection:Vector.<*> = new Vector<*>()
But the compiler is complaining that the type "is not a compile time constant". i know a bug exists with the Vector class where the error reporting, reports the wrong type as missing, for example:
var collection:Vector.<Sprite> = new Vector.<Sprite>()
if Sprite was not imported, the compiler would complain that it cannot find the Vector class. I wonder if this is related?
So it looks like the answer is there is no way to implicitly cast a Vector of a type to valid super type. It must be performed explicitly with the global Vector.<> function.
So my actual problem was a mix of problems :)
It is correct to use Vector. as a generic reference to another Vector, but, it cannot be performed like this:
var spriteList:Vector.<Sprite> = new Vector.<Sprite>()
var genericList:Vector.<Object> = new Vector.<Object>()
genericList = spriteList // this will cause a type casting error
The assignment should be performed using the global Vector() function/cast like so:
var spriteList:Vector.<Sprite> = new Vector.<Sprite>()
var genericList:Vector.<Object> = new Vector.<Object>()
genericList = Vector.<Object>(spriteList)
It was a simple case of me not reading the documentation.
Below is some test code, I would expect the Vector. to cast implicitly to Vector.<*>.
public class VectorTest extends Sprite
{
public function VectorTest()
{
// works, due to <*> being strictly the same type as the collection in VectorContainer
var collection:Vector.<*> = new Vector.<String>()
// compiler complains about implicit conversion of <String> to <*>
var collection:Vector.<String> = new Vector.<String>()
collection.push("One")
collection.push("Two")
collection.push("Three")
for each (var eachNumber:String in collection)
{
trace("eachNumber: " + eachNumber)
}
var vectorContainer:VectorContainer = new VectorContainer(collection)
while(vectorContainer.hasNext())
{
trace(vectorContainer.next)
}
}
}
public class VectorContainer
{
private var _collection:Vector.<*>
private var _index:int = 0
public function VectorContainer(collection:Vector.<*>)
{
_collection = collection
}
public function hasNext():Boolean
{
return _index < _collection.length
}
public function get next():*
{
return _collection[_index++]
}
}
[Bindable]
public var selectedItems:Vector.<Category>;
public function selectionChange(items:Vector.<Object>):void
{
selectedItems = Vector.<Category>(items);
}
I believe you can refer to an untyped Vector by just calling it Vector (no .<>)
With Apache Flex 4.11.0, you can already do what you want. It might have been there since 4.9.0, but I have not tried that before.
var collection:Vector.<Object> = new Vector.<Object>()
maybe?
But i'm just speculating, haven't tried it.
var collection:Vector.<Object> = new Vector.<Object>()
but only on targeting flash player 10 cs4

Resources