Scolring - Forum

Entraides et échanges autour de la technologie Scol - Informations and exchanges on the Scol technology

Vous pouvez changer la langue de l'interface une fois inscrit - You can change the language once registered

You are not logged in.

#1 4-Nov-2015 22:51:04

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

a simple object with a simple method

During the warm months I haven't been coding, but it is no longer warm.

Last year I didn't study Iri's example of making functions as fields of a struct, but after looking over his posts and the mini text editor example in the repository, here is a very simple example.  It's a cylinder object, that can figure out the area it takes up and its volume by doing calculations with its own fields. 

I used to do this in other projects in javascript, but here is how it works in a very very simple way in scol. In javascript, to call a method of a custom object,  you could write somethinglike

customobject.custommethod();
struct   Cylinder =[
	Fradius			: F,
	Fheight			: F,
	funreturnheightasintergerasstring: fun[Cylinder] S,
	funreturnarea	:	fun [Cylinder] F,
	funreturnvolume:	fun[Cylinder] F
	
	]mkCylinder;;
	
	
fun returnheight(dcylinder)=
	let ftoi dcylinder.Fheight -> height in
itoa height;;
	


fun return_area_function(dyclinder)=
	let dyclinder.Fradius *. PIf-> result in
	 result;;

fun return_volune_function(dyclinder)=
	let return_area_function dyclinder-> area in
	area *. dyclinder.Fheight;;
	 
fun initcylinder(radius, height, returnheightfunction, returnareafunction, returnvolumefunction)=
	_fooS "initializing cylinder";
	mkCylinder [radius height returnheightfunction returnareafunction returnvolumefunction];;
		
fun copy_this_cylinder(dcylinder)=
     initcylinder dcylinder.Fradius dcylinder.Fheight dcylinder.funreturnheightasintergerasstring dcylinder.funreturnarea dcylinder.funreturnvolume;;

fun main()=
	_showconsole; 
	let initcylinder 102.0 19.087 @returnheight @return_area_function @return_volune_function -> dcylinder in
	(
	_fooS strcatn  "the height of the cylinder as a float converted to an interger converted to a string is " :: (returnheight dcylinder) :: nil;
	_fooS strcatn "the area taken up by the cylinder is ":: (ftoa (return_area_function dcylinder)):: " square meters."::nil; 
	_fooS strcatn "the volume taken up by the cylinder is " ::(ftoa (return_volune_function dcylinder)):: " cubic meters"::nil; 
	let copy_this_cylinder dcylinder-> new_cylinder in
		(
		   _fooS "creating new cylinder and changing radius from 102.0 to 10.5 with copy_this_cylinder (dcylinder) function";
			set new_cylinder.Fradius= 10.5;
			_fooS strcat "the radius of the copied cylinder is" ftoa new_cylinder.Fradius;
		);
	);
	0;;
 
	
	
 

Edit....I added a new function that copies a cylinder and returns a new one, something like

var henry = new Cylinder(Robert);

in javascript or

dim henry = new Cylinder (Robert)

in vb.net/vb/vb script
where Robert is an existing Cylinder and henry is a new cylinder and not just a reference to the original cylinder.

Last edited by hebdemnobad (10-Nov-2015 21:11:33)

Offline

#2 5-Nov-2015 19:51:34

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: a simple object with a simple method

That's right.

However, you can continue further and make something as JS :

You may rewrite your main function like this (your old code is commented and i use the Scol formatted strings function sprintf) :

fun main()=
	_showconsole; 
	let initcylinder 102.0 19.087 @returnheight @return_area_function @return_volune_function -> dcylinder in
	(

	_fooS strcat  "the height of the cylinder as a float converted to an interger converted to a string is " exec dcylinder.funreturnheightasintergerasstring with [dcylinder];
	//_fooS strcatn  "the height of the cylinder as a float converted to an interger converted to a string is " :: (returnheight dcylinder) :: nil;

	_fooS sprintf "the area taken up by the cylinder is %f square meters." [exec dcylinder.funreturnarea with [dcylinder]]; 
	//_fooS strcatn "the area taken up by the cylinder is ":: (ftoa (return_area_function dcylinder)):: " square meters."::nil; 

	_fooS sprintf "the volume taken up by the cylinder is %f cubic meters" [ftoa exec dcylinder.funreturnvolume with [dcylinder]]; 
	//_fooS strcatn "the volume taken up by the cylinder is " ::(ftoa (return_volune_function dcylinder)):: " cubic meters"::nil; 

	);
	0;;

Here, i use exec to call the method set in the initialization.

exec function_to_call with [arguments];

The return of exec is the return of the called function.

You are able to change the function (the method) during the execution. For that, change the method in your structure.

// fun [Cylinder fun [Cylinder] F] I
fun changeVolumeMethod (dyclinder, functionToSet)=
  set dyclinder.funreturnvolume = functionToSet;
  0;;

In the next exec call, the new method will be executed. If you set nil, nothing is done.

To be continued ... smile

Offline

#3 5-Nov-2015 20:10:13

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

Re: a simple object with a simple method

thanks, yes I'd like to see how that function redefining/changing works.

Offline

Board footer

Powered by FluxBB