[Ctype Carry Demonstration]

Rich Fortnum, November 01, 2005

After studying some OOP through Lasso, I wrote up some test pieces that demonstrate the concepts at work. While there seems to be a bit of a void for such programming topics demonstrated in lasso, I submit this piece to show how instances of ctypes can be carried within the lasso environment.

While a custom type can be instantiated and manipulated in a file, by the end of the page, it has to be stored somewhere so that it can be used elsewhere. Hence the assignment to a session variable.

This also demonstrates how easy it is to code for changing attributes for a ctype as well as calling attributes in a stored instance. If you design a system where multiple instances are stored in session vars (each with a unique name if contained at the same time), then it demonstrates how much LESS code there would be, as well as the time saver and encapsulation.

start.lasso --> one.lasso --> two.lasso --> three.lasso

START.LASSO
1. start a session
2. instantiate the ctype 'car', which we will set to a session variable

ONE.LASSO
1. continue the session holding the svar '$car'
2. show make, colour, speed @ 1/a
3. change colour to red from the default (null)
4. show make, colour, speed @ 1/b

TWO.LASSO
1. continue the session, holding the svar '$car'
2. show make, colour, speed @ 2/a
3. change speed to 120 from the default (null)
4. show make, colour, speed @ 2/b

THREE.LASSO
1. continue the session, holding the svar '$car'
2. show make, colour, speed @ 3/a
3. change make to Volkswagen from default (null)
4. show make, colour, speed @ 3/b

TEST_CTYPE.LASSO


define_type: 'car';

local: 'colour' = string,
'make' = string,
'speed' = integer;

define_tag: 'oncreate', -optional = 'sspeed', -optional = 'smake', -optional = 'scolour';

local: 'pName' = string;

iterate: params, local: 'thisParam';
#pName = (#thisParam->first);
#pName->removeleading: '-';
if: #thisParam->type == 'pair';
self->#pName = (#thisParam->second);
else;
self->#pName = true;
/if;
/iterate;

/define_tag;

define_tag: 'setSpeed', -required = 'newspeed';
self->speed = #newspeed;
/define_tag;

define_tag: 'setMake', -required = 'newmake';
self->make = #newmake;
/define_tag;

define_tag: 'setColour', -required = 'newcolour';
self->colour = #newcolour;
/define_tag;

/define_type;

START.LASSO


<?lassoscript

include: 'test_ctype.lasso';

session_start:
-name = 'ss',
-expires='60',
-uselink;

(session_addvar: -name='ss', 'currentcar');

var: 'currentcar' = car;

'...set svar...<br/>';

?>
<a href="one.lasso">GO TO 1</a><br/>

ONE.LASSO


<?lassoscript

include: 'test_ctype.lasso';

session_start:
-name = 'ss',
-expires='60',
-uselink;

($currentcar->make) ' / ' ($currentcar->colour) ' / ' ($currentcar->speed) ' @ 1/a <br/>';

'...setting colour to red...<br/>';
$currentcar->setcolour: 'red';

($currentcar->make) ' / ' ($currentcar->colour) ' / ' ($currentcar->speed) ' @ 1/b <br/>';

?>
<a href="two.lasso">GO TO 2</a><br/>

TWO.LASSO


<?lassoscript

include: 'test_ctype.lasso';

session_start:
-name = 'ss',
-expires='60',
-uselink;

($currentcar->make) ' / ' ($currentcar->colour) ' / ' ($currentcar->speed) ' @ 2/a <br/>';

'...setting speed to 120...<br/>';
$currentcar->setspeed: '120';

($currentcar->make) ' / ' ($currentcar->colour) ' / ' ($currentcar->speed) ' @ 2/b <br/>';

?>
<a href="three.lasso">GO TO 3</a><br/>

THREE.LASSO


<?lassoscript

include: 'test_ctype.lasso';

session_start:
-name = 'ss',
-expires='60',
-uselink;

($currentcar->make) ' / ' ($currentcar->colour) ' / ' ($currentcar->speed) ' @ 3/a <br/>';

'...setting make to VW...<br/>';
$currentcar->setmake: 'Volkswagen';

($currentcar->make) ' / ' ($currentcar->colour) ' / ' ($currentcar->speed) ' @ 3/b <br/>';

?>
END<br/>

Email:


Password:



Articles