LASER IS NOT DIFFICULT!

I was inspired to start here partly because of Shamus Young, owner of the entertaining and informative Twenty-Sided Tale blog.  Seriously, go check him out.

Shamus is a good programmer, and like any good programmer, when he gets bored, he likes to write little experimental coding projects.  Currently he’s working on a game called “Project Good Robot,” which is shaping up into something I would REALLY like to play.  The cool thing about Shamus is that when he does these coding projects, he posts about them.  It’s all very educational, and a lot of fun to read, and Shamus really has a talent for explaining high-level technical stuff in a way that anybody can understand.  Reading through his earlier projects would really be a good way to gain some solid practical knowledge for beginning programmers.  Or anybody who would like to be a beginning programmer.  Or anybody who’s curious about the arcane arts of “Computer Science” and would like a peek behind the curtain.

Part of the reason I started this blog is because I saw what Shamus was doing, and thought “That looks like fun, I’d like to do that.”  So I create a blog, and I give it a “Programming” category where I can talk about the game that I’ve been working on, and I give it a “Mad Science” category, where I can talk about whatever geeky and bizarre things I’m up to.  And then I think “Well, I’ve got this new blog, I should put some content in it!  Hey, how about that work that I’ve been doing recently on my video game!”

Problem!

I’ve been working on this thing for upwards of a year now, since before I lost my job back in December, 2012.  That’s easily a year and a half of work to catch up on.  OK, that’s not so bad.  I could post something on what I’m doing every week, but I want to get into the interesting stuff, and what’s interesting is whatever I’m doing RIGHT NOW!

So what am I doing right now?  Working with Templates!

How’s that a problem?

Templates are one of those things in C-type languages that give experienced programmers headaches, let-alone laymen.  Here I am wanting to chat about what I’m doing right now, and what I’m doing right now, and “What I’m doing right now” is the sort of thing that you don’t get taught in class because that requires finding a professor who knows what it is!

Actually now that I think about it, that’s not a bad idea.  See, I’m a huge fan of this language called “D.”  D makes a lot of things REALLY trivially easy.  Let’s take string comparison, for example.

Oh.  A “String” is an array of characters.

Oh.  A “Character” is like a letter or a punctuation mark, or pretty much anything else that may appear in a text box.

Oh.  An “Array” is a location-ordered collection of individual values, organized by how far they are past the initial value.(so array[0] is the first value, array[1] is 1 past the first value, array[2] is 2 past the first value, etc…)

You may be starting to see my problem already.  I don’t really have Shamus’s talent for finding that golden level of simplification, where anybody can read it and everybody can understand.

Right, so let’s say we’ve got two strings, String1 and String2(and I need to re-read The Cat In The Hat so I can put an appropriate joke here), and we want to find out if they say the same thing!  How would you do that?

Let’s pretend we’re in C++.


if(String1 == String2)
{
cout << "THE STRINGS ARE EQUAL!" << endl;
}

OK well… yes and no.  Technically yes, the strings are equal, but they’re arrays, and in the case of arrays, “equal” means “start at the same place.”  It has to do with pointers.  Which are an entirely different beast.  If you want to compare the CONTENTS of the strings, in C++, you would actually want to do this:


if(String1.equals(String2))
{
cout << "THE STRINGS ARE EQUAL!" << endl;
}

string.equals() is a function–

Oh… a function is a block of code which runs when called based on a set of input parameters, and then returns control to where it was called from.  Precise terminology is more complicated than that, but that’s roughly it.(in Java they’re called “Methods,” which it turns out is actually more than just Java programmers being all fancy.  They do the same thing, though.)

string.equals() is a function which will go through the calling string and the string provided as a parameter and check that each character in the string is the same, like if both String1 and String2 say “Hello, I am a string!”

That double-quotes thing means it’s a string, by the way.

So how do you do it in D?  Funny you should ask.

if(String1 == String2)
{
writeln("THE STRINGS ARE EQUAL!");
}

Also, seriously WordPress, PLEASE STOP ERASING MY SPACES WHEN I SWITCH BETWEEN VISUAL AND TEXT MODE!  You’re going to make me look like an idiot!

Right, so in C and C++, arrays are just a fancy word for “pointers.”  Yeah, those fancy things that I’m not talking about right now.  In D, arrays are treated as a primitive type, just like an int(integer), char(character), or float(er…float).  That means they can be compared the same way.  That’s not the cool part.

The cool part is that in D, arrays have built-in variable length and management tools, which in C, C++, C#, or Java you have to import a library to get!  One of the great purposes in D was making arrays work the way the programmer always wanted them to, without having to add any messy extraneous stuff.  And then if you want that messy, extraneous stuff, there are more libraries built-in that you can import.

What was I talking about?  Oh yes, Templates!

I’m going to level with you here, I really don’t know how Templates work in C, C++, C#, or Java.  And really, that’s OK.  Go ask any of your programmer friends about Templates.  Typically, either their eyes will gloss over, or they’ll get this really worried look on their faces, like if you’d just asked “What happens if I drill a hole in the gas tank of my car here while you’re smoking a really good cigar?”

But in D, templates are easy!

It works like this.  Let’s say I’ve got a function.

int subintelligate(int input)
{
//subintelligate input into output here
return output;
}

Well that’s great if we only ever want to subintelligate integers, but what if we want to also be able to subintelligate floating point values?  Or characters?  Well, you’ve got two options.  First, you can “overload” the function.  That means “write another function with an identical name, but different inputs, and the compiler will figure out what the appropriate function is for me.”

int subintelligate(int input)
{
//subintelligate integer input into output here
return output;
}
float subintelligate(float input)
{
//subintelligate floating-point input into output here
return output;
}
char subintelligate(char input)
{
//subintelligate character input into output here
return output;
}

Well that’s all fine and good, but you’re generating a lot of code here, and if all the subintelligate functions are basically the same thing, then you’re overcomplicating things for no good reason, and everyone else who uses the code will hate you.

With Templates, you can instead…

T subintelligate(T)(T input)
{
//subintelligate input into output here
return output;
}

And whatever variable type you feed in, subintelligate will do the same thing.  How does it work?

It turns out that all functions actually have TWO sets of parameters!  You’ve got a list of input values(int input, int moreinput, int toomuchinput), but you ALSO have a list of variable types that the function will need to work with.

So that first subintelligate function?  To the compiler, it actually looks like this:

int subintelligate(int)(int input)
{
//subintelligate input into output here
return output;
}

We’re telling the compiler that subintelligate is a function that works with integers, accepts one integer value, and outputs one integer value.

Or we could replace the type list with a set of variables themselves.  Anything works, so long as it’s not already used in the current namespace.  The following is totally valid.

ULTRAQUARK subintelligate(ULTRAQUARK)(ULTRAQUARK input)
{
//subintelligate input into output here
return output;
}

If a bit silly.  The compiler will just remember that it needs to figure out what type of variable ULTRAQUARK is supposed to be based on what “input” is when the function is called.  T is just the customary value.

Finally, you can also specify what ULTRAQUARK is supposed to be when calling subintelligate, by writing

writeln(subintelligate!(char)(thing_to_subintelligate));

And now, thing_to_subintelligate had BETTER be a char, or your compiler will yell at you.

So what’s this got to do with what I’m doing now?

I’ll get back to you!

Laser is not difficult!

Laser is not difficult!

Advertisements

Leave a Reply. Wheaton's Rule in full effect.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s