Oh, I got hot sause all over my bazito!

You know what this is? It's a brain sucker. You know what it's doing? Filing its tax return

If you wish to make an apple pie from scratch, you must first invent apple pie

The Adventures of Little Ed Brave

Tell airport security your name is McCannister because you can hide anything in a cannister.

You know what? Nobody notices when this changes anyway.

There are 10 types of people in the world: Those who understand binary, and STFU

What happens in a black hole stays in a black hole

The black hole draws you inexorably inward. Time slows. You are likely to be eaten by a grue.

I'd diddle little umdidlie... if she weren't my half-sister.

Abortion prevents pedophilia. In more ways than one!
Get Firefox!
I wrote a haiku

which I was about to share,

but then I thought, "screw it."
Level 1

Notice to all users of the Holodeck:

There are safety protocols in place that cannot be deactivated without the approval of two commanding officers or the captain to protect users of the Holodeck from potential harm. However, every time the Holodeck is ever used in a nontrivial manner, no matter what the safety protocols say, the Holodeck turns into a deathtrap.

Unless you believe yourself to be adept at constructing a forcefield from your communicator and 19th century Earth tools, or you're at the very least not wearing a red shirt, you are strongly advised not to attempt to use the Holodeck until a designer comes up with a safety protocol that doesn't kill you whenever somebody looks at it funny. Even when you're not on the holodeck. Or in the same quadrant. Or time period.

In fact, if you are wearing a red shirt, Starfleet may not be the job for you

« Adult OnlyMy week end »

Late binding, or: Screw you, compiler!

Permalink 06/19/07 at 04:25:09 pm, by Ed, 613 words   English (US)
Categories: Programming

I've never had the pleasure of having to deal with late binding before today. For those not initiated in the ways of programming and compilers, a quick, shoddy, and probably not entirely accurate outline:

Programming (these days) is object based. If I want to, for example, play a game, I would be a main character. This character would be an object that could perform actions in an environment (another object). For example, I could say MainCharacter.MoveForward() which would call the MoveForward method of the MainCharacter object. The MainCharacter object has a handy reference to its environment so that before it updates its position, it looks to make sure that the environment will allow it to move forward, by calling, for example, Environment.IsFree(MyPosition + 1), which will let the MainCharacter object know if the position directly in front of it is open.

These objects have strict types. A MainCharacter object would be, for example, of a Sprite class (something that moves), where the Environment object would be, for example, of a Map class (something that sprites can move around on). This strict typing makes sure you don't accidentally make a MainCharacter object and start calling Environment methods on it. That is, I can't say:

Sprite MainCharacter = new Map()

The compiler (the bit that takes human-readable code and turns it into machine-readable code) would complain, saying that maps and sprites are not compatible! This prevents later accidents like:

MainCharacter.IsFree(MyPosition + 1)

Which a careless programmer might try if he wasn't thinking straight or just slipped up. Now, if you tried to call a method of one class (Map) on an object of another class (Sprite), and the compiler let you actually do it, you would have the machine code looking through a Sprite object layout for data in a map object layout, which just gets all kinds of messy, not to mention memory leaks, data corruption, and computer crashing. So it's a good thing the compiler is there for us.

At least, it's there for us normally (i.e. when we're using early binding). Late Binding basically says, "Screw you, compiler. You think you're so smart, with your strongly-typed classes, and your optimized code? Yeah, well, screw you! I'm gonna make a sprite a map anyway and there's nothing you can do about it!"

Something like that, anyway. In the .NET Framework, the quickest example would be something like this:

object MainCharacter = Activator.Create(typeof(Sprite));
typeof(map).GetMethod("IsFree").Invoke(MainCharacter, new object[]{MyPosition + 1});

So, yeah, there's a couple of things to point out. Obviously they don't make it easy to screw the compiler over (not made hard just to make it hard, but because late binding requires more detail in the code because it can't get it inherently by the types). Also, the .NET Framework is still pretty adamant about correct types being used, so this would fail in .NET, but if it could be run unmanaged (as in older languages, i.e. C/C++), it would run along on its merry way, doing the very thing that the compiler stopped us from doing up above. We're creating only a basic Object instead of a specific type of object, so that we can call anything we want to on it. Fun, huh? Start screwing around in your memory for long, though, and you'll suddenly find a BSoD, or maybe a GSoD if you screw up in the right spot. Which would actually be pretty amusing. And awesome. And incredibly unlikely. And nearly/entirely impossible, based on your current OS memory access settings. But still.

Next week: Only you can prevent frequent flyers (yeah... that is actually how it's spelled. Sad, I know.)

No feedback yet

Comments are closed for this post.