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 13-Dec-2014 19:15:01

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

how to define a struct function? [solved]

colleagues:

this was partially discussed in another thread, but i figured i would start a new one for clarity.

let's say I have a 'dog' struct. it has two properties: it's barking volume (a float), and its barking sound (a string). alot of this is psuedo code because i don't know how to declare a function in scol in any concrete way:

struct Dog =[
	fdog_volume				: F,
	sdog_bark				: S,
	//psuedo code from what i know of scol
	bark						:  fun [Dog F S] S
	
	/*psuedo code, loosely based on vbnet or java or javascript
	fun dog.bark()=
		_fooS strcatn "Dog is this loud: ":: (ftoa dogfidog_volume):: "and it sounds like this: ": (sdog_bark)::nil;;
	*/
		  ] mkDog;;
		  
fun main()=
	_showconsole;
	let 2.0 -> dog_volume in
	let "arfarfar"-> dog_bark in
	let mkDog [	dog_volume dog_bark] -> newdog in
	//open let brace
	(
	//is there a way to invoke the 'bark' function without having to something like this, i know it's very incomplete in any case:
	exec newDog.fun_dog_bark with [newdog.idog_volume newdog.sdog_bark];
	//this would be psuedo code based on the other languages i know
	newdog.bark;
	//close let brace
	);
	
	0;;

Last edited by hebdemnobad (14-Dec-2014 22:44:12)

Offline

#2 13-Dec-2014 22:15:43

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

Re: how to define a struct function? [solved]

Quickly, you could write this :

struct Dog =[
	fdog_volume				: F,
	sdog_bark				: S,
	fun_bark				: fun [Dog] S
	] mkDog;;
	
// you define the function outside the structure. Its prototype is : fun [Dog] S
fun fooBark (dog)=
	_fooS strcatn "Dog is this loud: ":: (ftoa dogfidog_volume):: "and it sounds like this: ": (sdog_bark)::nil;;

fun main()=
	_showconsole;

	let mkDog [
		nil    // fdog_volume
		nil    // sdog_bark
		nil    // fun_bark
		] -> newdog in
	//open let brace
	(
	set newdog.fdog_volume = 2.0;
	set newdog.sdog_bark = "arfarfar";
	set newdog.fun_bark = @fooBark;	// "@" is approximately equals at the pointer of function

	exec newdog.fun_bark with [newdog];
	);
	
	0;;

You can initialize the structure either by setting these fields, as here, or directly as you did. As you want, the result is the same. Sometimes, according the context, one of these method can be better.

Offline

#3 13-Dec-2014 23:00:37

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

Re: how to define a struct function? [solved]

Thx Iri

Offline

#4 14-Dec-2014 00:16:35

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

Re: how to define a struct function? [solved]

fun fooBark (dog)=
	_fooS strcatn "Dog is this loud: ":: (ftoa dog.fdog_volume):: "and it sounds like this: ": (dog.sdog_bark)::nil;;

It should be more correct wink

Offline

#5 16-Dec-2014 16:49:45

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

Re: how to define a struct function? [solved]

thx iri, dogs are the best examples for object oriented (or pseudo object oriented) programming, for some reason.

Offline

Board footer

Powered by FluxBB