Friday, September 12, 2008

Typed container in AS3

In ActionScript3, how can you define and enforce type for an
Array. Something like the following:
var a:MyArray = new MyArray(NetConnection, 2);
a[0] = new NetConnection(); // should work
a[1] = "some string"; // should fail with exception.

Variable constructor arguments in AS3

The new Flex SDK (for Flash Player 10) allows two arguments in
the constructor of NetStream although second is optional,
but the old SDK (for Flash Player 9) allows only one. The following
piece of code tries to allocate NetStream, but cannot be compiled
in old SDK. How would you change it so that it can compile and work
in both old and new SDK?

if (flashMajorVersion >= 10)
ns = new NetStream(connection, id);
ns = new NetStream(connection);

Arithmetic in AS3

What is wrong in the following ActionScript3 code. It tries to
keep a (bit) vector for high-order and low-order bits of a two-byte
number. Assume somearray is an array of two-byte numbers.

var a:Array = new Array(256), b:Array = new Array(256);
for each (var n:uint in somearray) {
a[n/256] = 1;
b[n%256] = 1;

a++ for int vs short in C

Using a particular C compiler, the expression 'printf("%d\n", (a++ -
--a));' printed '0' if 'a' was int, and '1' if 'a' was short. Why?

Binary representation in Python

Give a one line expression to convert a number to its binary
representation in Python. For example, 258 to "100000010".

C++ vs Java

Give a numeric expression that is valid in both C++ and Java, but
evaluates to different values.

Parse dotted IP string in AS3

Give a one line expression in ActionScript 3 to convert a dotted IPv4
address (String) to its numeric (uint) representation. For example,
"" to 0xa000001.

C vs Python

Give a code snippet that is valid in both C and Python, but produces
different results.