holatuwol
Developer
Veracity added typedefs to v10.7, and the following is me copying what was written about them because you are all better at writing engine-breaking scripts than I am, since I'm still a newbie to this script-writing thing. ^_~
--------------------------------
A discussion on kolmafia.us has me thinking that user typedefs might make ASH code easier to read and maintain. In particular, holatuwol's new syntax for implicitly passing an object as the first parameter of an invoked function is sort of like method invocation - if it were easier to define the "class".
It could be as simple as this:
Consider two examples from the kolmafia note. Holatuwol's:
could be:
or mine:
could be:
Those are pretty similar, actually...
Edit: Fixed my own dumb code error from the original note. Veracity.
--------------------------------
A discussion on kolmafia.us has me thinking that user typedefs might make ASH code easier to read and maintain. In particular, holatuwol's new syntax for implicitly passing an object as the first parameter of an invoked function is sort of like method invocation - if it were easier to define the "class".
It could be as simple as this:
Consider two examples from the kolmafia note. Holatuwol's:
Code:
void push( boolean [item] list, string data )
{
string [int] items = data.split_string( "\\s*,\\s*" );
foreach key in items
list[ string_to_item( items[key] ) ] = true;
}
boolean [item] supergarnishes;
supergarnishes.push( "grapefruit, lemon, olive, orange, soda water, strawberry" );
could be:
Code:
// define the "class"
typedef boolean [item] ItemSet;
// define a "method"
void push( ItemSet list, string data )
{
string [int] items = data.split_string( "\\s*,\\s*" );
foreach key in items
list[ string_to_item( items[key] ) ] = true;
}
// make an instance of the "class"
ItemSet supergarnishes;
// invoke the "method"
supergarnishes.push( "grapefruit, lemon, olive, orange, soda water, strawberry" );
or mine:
Code:
boolean [item] supergarnishes;
supergarnishes[ $item[grapefruit] ] = true;
supergarnishes[ $item[lemon] ] = true;
supergarnishes[ $item[olive] ] = true;
supergarnishes[ $item[orange] ] = true;
supergarnishes[ $item[soda water] ] = true;
supergarnishes[ $item[strawberry] ] = true;
foreach key in supergarnishes
if (item_amount( key ) > 0)
print( "hey, you have some " + key );
could be:
Code:
// define the "class"
typedef boolean [item] ItemSet;
// define a "method"
void add( itemSet set, string elt )
{
set[ string_to_item( elt ) ] = true;
}
// define an instance of the "class"
ItemSet supergarnishes;
// invoke the "method"
supergarnishes.add( "grapefruit" );
supergarnishes.add( "lemon" );
supergarnishes.add( "olive" );
supergarnishes.add( "orange" );
supergarnishes.add( "soda water" );
supergarnishes.add( "strawberry" );
foreach key in supergarnishes
if (item_amount( key ) > 0)
print( "hey, you have some " + key );
Those are pretty similar, actually...
Code:
// define the "class"
typedef boolean [item] ItemSet;
// define "methods"
void add( itemSet set, string elt )
{
set[ string_to_item( elt ) ] = true;
}
void push( ItemSet set, string data )
{
string [int] items = data.split_string( "\\s*,\\s*" );
foreach key in items
set.add( items[key] );
}
// define an instance of the "class"
ItemSet supergarnishes;
// And then take your pick on how to initialize it...
Edit: Fixed my own dumb code error from the original note. Veracity.