[cast]

Description

Link: [cast]
Author: Jason Huck
Category: Variable
Version: 8.x
License:
Posted: Feb. 13, 2006
Updated: Jan. 01, 0001
More by this author...
This is a shortcut to programmatically re-cast an element to a new type.

Parameters

-in custom type, required The element to re-cast.
-as string, required The target 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;	
	'<br>\n';
/iterate;

'<hr>\n';

var('bar' = '<?xml version="1.0"?><root><record>hi</record></root>');
$bar->type;
'<br>\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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

07/18/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 )); ]
Email:


Password:



Newest

Most Popular

Support tagSwap.net