Tuesday, June 03, 2008
ActionScript: The difference between Object and * (an asterisk)
If you've ever wondered what the difference is between the following two statements, you aren't alone.
It's fairly hard to Google for the explanation, but Subhash Chandra Gupta recently pointed me to a good example.
The full article can be found in the Flex 3 Help.
The difference is that * (an asterisk) signifies that the object can be any type and typing something as an Object will require you to cast if you are compiling in strict mode. Here's a greatly simplified example to demonstrate.
var result:Object = sendRequest();
var result:* = sendRequest();It's fairly hard to Google for the explanation, but Subhash Chandra Gupta recently pointed me to a good example.
The full article can be found in the Flex 3 Help.
The difference is that * (an asterisk) signifies that the object can be any type and typing something as an Object will require you to cast if you are compiling in strict mode. Here's a greatly simplified example to demonstrate.
:Object {
return 1;
}
var one:Number = returnOne(); // compiler error in strict mode
:* {
return 2;
}
var two:Number = returnTwo(); // no compiler error in strict modeLabels: *, ActionScript, object
Friday, August 31, 2007
Ruby: Adding a "not" method for readability
The other day I was working with an if statement that looked similar to the snippet below.
I spent enough time in C, C#, etc to be able to parse the if statement fairly easily; however, I thought it would improve readability if I could write the following snippet instead.
Adding a "not" method to Object turned out to be fairly easy. I started with the following tests (block syntax provided by dust).
The implementation of the "not" method is similar to the implementation of the "as" method provided by facets (an explanation of the "as" implementation can be found in a previous entry).
The Object.not method returns an instance of Object::Not. The Object::Not instance is initialized with the subject (the instance that was sent the "not" message). The Object::Not instances are basically proxies that send all calls back to the original instance. The Object::Not class privatizes almost all of it's methods so that most method calls will be handled by method_missing. The method_missing implementation simply forwards on any method call to the subject and negates the result.
if !response.incomplete? && !response.invalid? && response.total > 0
...
endI spent enough time in C, C#, etc to be able to parse the if statement fairly easily; however, I thought it would improve readability if I could write the following snippet instead.
if response.not.incomplete? && response.not.invalid? && response.total > 0
...
endAdding a "not" method to Object turned out to be fairly easy. I started with the following tests (block syntax provided by dust).
unit_tests do
test "not negates true to false" do
assert_equal false, nil.not.nil?
end
test "not negates false to true" do
assert_equal true, Object.new.not.nil?
end
endThe implementation of the "not" method is similar to the implementation of the "as" method provided by facets (an explanation of the "as" implementation can be found in a previous entry).
define_method :not do
Not.new(self)
end
private *instance_methods.select {|m| m !~ /(^__|^\W|^binding$)/ }
@subject = subject
end
!@subject.send(sym,*args,&blk)
end
end
endThe Object.not method returns an instance of Object::Not. The Object::Not instance is initialized with the subject (the instance that was sent the "not" message). The Object::Not instances are basically proxies that send all calls back to the original instance. The Object::Not class privatizes almost all of it's methods so that most method calls will be handled by method_missing. The method_missing implementation simply forwards on any method call to the subject and negates the result.


