Lasso Soft Inc. > Home

[cast]

Linkcast
AuthorJason Huck
CategoryVariable
Version8.x
Licensehttp://opensource.org/licenses/artistic-license.php
Posted13 Feb 2006
Updated13 Feb 2006
More by this author...

Description

This is a shortcut to programmatically re-cast an element to a new type.

Sample Usage

// testing
var('types') = array(
	'array',
	'boolean',
	'bytes',
	'date',
	'decimal',
	'duration',
	'integer',
	'map',
	'pair',
	'string',
);

iterate($types, local('t'));
	var('foo' = '2');
	cast($foo, -as=#t);
	$foo + ': ' + $foo->type;	
	'
\n'; /iterate; '
\n'; var('bar' = 'hi'); $bar->type; '
\n'; cast($bar, -as='xml'); $bar->type;

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

define_tag(
	'cast',
	-required='in',
	-required='as',
	-priority='replace',
	-description='Casts the given object as the given type.'
);
	protect;		
		if(#as == 'bytes');
			local('x' = bytes);
			#x->importstring(string(#in));
			#in = #x;
		else;
			#in = tags->find(#as)->run( 
				-params=array(#in)
			);
		/if;
	/protect;
/define_tag;

Related Tags

Comments

18 Jul 2011, Brian Loomis

Using Cast to dynamically assign and execute types and methods.

[// lasso

define_tag(
'cast',
-required='in',
-required='as',
-priority='replace',
-description='Casts the given object as the given type.'
);
protect;
if(#as == 'bytes');
local('x' = bytes);
#x->importstring(string(#in));
#in = #x;
else;
#in = tags->find(#as)->run(
-params=array(#in)
);
/if;
/protect;
/define_tag;




define_type:'test';

local('do')=string;

define_tag('onCreate');

self->'do'='monster';
/define_tag;

define_tag:'print', -required='str';
var:'y' = ' it works';
return('string input: ' + #str + ' property input: '+self->'do');
/define_tag;

define_tag('_unknowntag', -encodenone );
return(tag_name);
/define_tag;

define_tag('three', -encodenone );
return(self->'do');
/define_tag;


/define_type;


var('once'='test');
var:'x' = $once;
/*
$x->type;
$once->type;
*/

$x->type;
'
\n';
cast($x, -as=$once);
$x->type;
'
\n';

var:'object' = $x;
var:'method' = 'three';

($object)->\$method->(run(
-owner=$object
));
]

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft