Functions calls with an implied first parameter

holatuwol

Developer
There are now two special uses of the "." operator which are exclusive to variables. The first one, already present in previous releases of KoLmafia, is the ability to accessing the various fields of a record. As some people have discovered, while you can return records from functions, you're unable to access the fields of said record unless it's stored in a variable.

The second shiny new feature has the same limitation, and it's often known in object oriented programming languages as the "implied first parameter". It's a little hard to explain in words, so we'll use a script example with some inline comments to explain exactly what this means.


# Just in case you've never seen these before, this
# defines a record of type "album".

record album
{
string artist;
string album_name;
int song_count;
};

# Let's define a method that initializes an album
# with some simple values.

void initialize( album fun )
{
fun.artist = "SHeDAISY";
fun.album_name = "Knock on the Sky";
fun.song_count = 13;
}


# This is where the fun begins. See the function
# that's defined above? We're going to call it.
# Let's create a variable to pass to the function.

album my_latest;

# If you've never taken a programming languages course
# before, this will be your first lesson in OOP. A
# "class" is a "record" with special functions that
# allow you to pass an implied first parameter.

my_latest.initialize();

# In the above example, the "." operator tells the
# interpreter that everything before it should be
# passed as the first parameter to the initialize()
# function. And that's what KoLmafia will do.

void print( album fun )
{
fun.artist.print();
fun.album_name.print();
fun.song_count.print();
}

my_latest.print();

# Remember that only VARIABLES have this functionality.
# For example, $item[spices].item_amount() won't work.
# You can't do it on function return values yet, either.

print( "\n\n" );
print( "This isn't a variable." );
print( "I have to print this the long way." );

# That concludes this demonstration of new functionality.
# Naturally, it only has very limited use until "." can
# be used on everything, but it's still neat.

string end_demo = "\n\n";
end_demo = end_demo + "Demo complete.";

end_demo.print();
 

holatuwol

Developer
And now, I finally have ASH burnout.  The last of the changes that went in was making the "." more flexible.  I validated a few of the longer scripts I found lying around here on the forums, which means the parser isn't broken, but I may have nerfed functionality somewhere.  If anyone wants to experiment when the next release goes public:


record game
{
  string company;
  string game_name;
  string [string] weapon;
};

game initialize( game fun )
{
  fun.company = "SquareENIX";
  fun.game_name = "Dragon Quest VIII";

  fun.weapon["Hero"] = "Boomerang";
  fun.weapon["Yangus"] = "Sickle";
  fun.weapon["Jessica"] = "Staff";
  fun.weapon["Angelo"] = "Sword";

  return fun;
}

game my_latest;
foreach name in my_latest.initialize().weapon
name.substring(0,4).print();
 

Metraxis

Member
Whenever the new version is ready, I'll be more than happy to throw a thousand lines of spagehtti at it and read the entrails. :)
 
Top