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.
Pages: 1
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
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
Thx Iri
Offline
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
Offline
thx iri, dogs are the best examples for object oriented (or pseudo object oriented) programming, for some reason.
Offline
Pages: 1