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 5-Nov-2014 12:48:25

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

general scol coding question: better to use structs than globals

colleagues:

a general scol coding question about creating and destroying objects
what is the most efficient way to code with the vm
using global variable (Iri I think once you said this was a bad idea but I can't find your post), even if data in global variable is destroyed at application shutdown.

temporary variable that is passed from one function to another until the app's shutdown (so the window can be destroyed)

temporary variable held in a struct

and my second question is

why, in basic terms,  is the most efficient coding practice a better way of using the vm?

thx

Offline

#2 5-Nov-2014 14:55:41

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

Re: general scol coding question: better to use structs than globals

Hello,

Each will have a different answer. And it depends of what you want to do.

For me, the better way is a structure and a local (temporary, if you prefer) variable.

A structure can look like an encapsulated object : you put coherent fields with differents types (even other structures).
Thus, you can manage your Scol objects as you want.

Then, a such local variable contains all what you need and it'll destroyed when no longer needed.

Furthermore, you can create safely as far as needed local variables with a same structure by a same function. By example, for each client, for each instance, etc.

For me, the easier way is the global variables.
You should destroy it explicitly but you may not always be sure that you don't need it.

If you need several instances, you must :
either define several global variable (only if you know the number of instances)
or create a list and manage it (add an instance, find an instance, remove an instance, etc).

Offline

#3 5-Nov-2014 15:01:44

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

Re: general scol coding question: better to use structs than globals

hebdemnobad wrote:

using global variable (Iri I think once you said this was a bad idea but I can't find your post), even if data in global variable is destroyed at application shutdown.

Like in many language, the global variables are rather prohibited / discouraged.

Offline

#4 5-Nov-2014 17:36:08

arkeon
Admin. / Scol language & OpenSpace3D developer
From: Nantes
Registered: 30-Mar-2009
Posts: 5,161
Website

Re: general scol coding question: better to use structs than globals

A good code is a code that directly manage instances of my point of view

(Yes scol kernel should be like that)

Offline

#5 5-Nov-2014 17:38:52

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

Re: general scol coding question: better to use structs than globals

arkeon wrote:

A good code is a code that directly manage instances of my point of view

Yes and it must be readable and maintenable by others.

Offline

#6 5-Nov-2014 17:40:11

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

Re: general scol coding question: better to use structs than globals

iri wrote:
arkeon wrote:

A good code is a code that directly manage instances of my point of view

Yes and it must be readable and maintenable by others.


I'm getting there...I'm using structs more, and that means less nested let -> in statements

Offline

#7 5-Nov-2014 17:43:46

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

Re: general scol coding question: better to use structs than globals

Precisely, for me, my "better way" should be used.
The "easier way" can be easy for the beginner but it is not often suitable for a more important project.

Offline

#8 5-Nov-2014 17:46:35

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

Re: general scol coding question: better to use structs than globals

hebdemnobad wrote:

I'm getting there...I'm using structs more, and that means less nested let -> in statements

let -> in statements are also local variables. They are very interesting.

Offline

#9 5-Nov-2014 17:52:27

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

Re: general scol coding question: better to use structs than globals

iri wrote:
hebdemnobad wrote:

I'm getting there...I'm using structs more, and that means less nested let -> in statements

let -> in statements are also local variables. They are very interesting.

right, but with the struct i am working on , it has 14 fields, so i can have 14 lines of let -> in statements, and then make the struct, with just one pariof (  )...even though bluefish highlights them, after 3 or 4 nested ((( I get lost.

Offline

#10 5-Nov-2014 19:10:47

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

Re: general scol coding question: better to use structs than globals

Here is an example :

// a sub structure
struct MyOwnSubStruct = [
	mosubs_field1 : I,
	mosubs_field2 : [[S S] r1],
	mosubs_field3 : I,
	mosubs_field4 : fun [MyOwnSubStruct I] I
	] mkMyOwnSubStruct;;

// a main structure
struct MyOwnStruct = [
	mos_field1 : Chn,
	mos_field2 : Comm,
	mos_field3 : Srv,
	mos_field4 : S,
	mos_field5 : MyOwnSubStruct,	// this field set the sub structure
	mos_field6 : I,
	mos_field7 : I
	] mkMyOwnStruct;;
	
// a function : its type must be as defined in the sub structure above	
fun myCallback (myStruct, iParam)=
	// any code
	0;;
	
// another function : its type must be as defined in the sub structure above	
fun myOtherCallback (myStruct, iParam)=
	// any code
	0;;
	
// this initializes the structure : all fields are at nil, their values 
// are set later. It returns a structure perfectly defined.
fun initStruct ()=
	let mkMyOwnSubStruct [nil nil nil nil] -> subStruct in
	mkMyOwnStruct [nil nil nil nil subStruct nil nil];;
	
// this creates a local (temporary) variable
// Some fields are set, others keep to nil.
// Some that, initStruct is firstly called to correctly initialize a new 'structure variable'
fun createVariable (field1, field3, subField4)=
	let initStruct -> myStructObject in
	let myStructObject.mos_field5 -> mySubStructObject in
	(
	set myStructObject.mos_field1 = field1;
	set myStructObject.mos_field3 = field3;
	set mySubStructObject.mosubs_field4 = subField4;
	myStructObject
	);;
	
// to do something with my variable. It should be return the same variable
// to continue later (by example with other function)
fun manageMyStruct (myStruct)=
	// any code using my structure
	
// the main function. Several independant instances are created and managed
fun main ()=
	let createVariable _channel "Bob" @myCallback -> myStruct in
	manageMyStruct myStruct;
	
	let createVariable _masterchannel "Foo" @myCallback -> myStruct in
	manageMyStruct myStruct;
	
	let _openchannel "123.123.123.123:3500" nil _envchannel _channel -> newChannel in
	let createVariable newChannel "Bar" @myOtherCallback -> myStruct in
	manageMyStruct myStruct;
	
	// other code lines
	0;;

Offline

#11 5-Nov-2014 19:19:34

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

Re: general scol coding question: better to use structs than globals

thx Iri, more excerpts from your book smile

a question. how would i invoke the function:

mySubStructObject.mosubs_field4

what would that statement look like? one thing i know i haven't learned how to do with structs is define fields which are functions

Offline

#12 5-Nov-2014 20:47:16

arkeon
Admin. / Scol language & OpenSpace3D developer
From: Nantes
Registered: 30-Mar-2009
Posts: 5,161
Website

Re: general scol coding question: better to use structs than globals

exec mySubStructObject.mosubs_field4 with [(MyOwnSubStruct instance) 1];

Offline

#13 5-Nov-2014 21:24:54

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

Re: general scol coding question: better to use structs than globals

arkeon wrote:

exec mySubStructObject.mosubs_field4 with [(MyOwnSubStruct instance) 1];

thx...that's a good thing to know

Offline

#14 5-Nov-2014 21:40:07

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

Re: general scol coding question: better to use structs than globals

I add these function in the example above (i update also the main function)

// I write an API to easely manage my structure

// Two argument : my structure and a value for one of its fields (here the callback)
// I can write a similar function to set (and to get !) each field (or group of fields)
fun setMyCallback (myStruct, cbFun)=
	let myStruct.mos_field5 -> mySubStructObject in
	set mySubStructObject.mosubs_field4 = cbFun;
	myStruct;;
	
// Same thing : i continue my API to invoke my function.
// Remark : i return my structure instead of an integer. I prefer do that
// to be sure to continue with the same temporary variable.
// However, in some cases, it is not possible ('getter' for example)
fun execMyCallback (myStruct, iParam)=
	let myStruct.mos_field5 -> mySubStructObject in
        // set myStruct in the first argument allow to keep my temporary/local variable yet
	exec mySubStructObject.mosubs_field4 with [myStruct iParam];
	myStruct;;
	
// Note : you can invoke it in a specific channel like it defined in
// the field 'mos_field1'. For that, execch is used instead of exec :
fun execMyCallbackInAnotherChannel (myStruct, iParam)=	
	let myStruct.mos_field5 -> mySubStructObject in
        // set myStruct in the first argument allow to keep my temporary/local variable yet
	execch myStruct.mos_field1 mySubStructObject.mosubs_field4 [myStruct iParam];
	myStruct;;
	
// the main function. Several independant instances are created and managed
fun main ()=
	// ...
	let createVariable _channel "Bob" nil -> myStruct in
	(
	// to set a callback, i use my own api above : 'setMyCallback'
	setMyCallback myStruct @myCallback;
	manageMyStruct myStruct;
	// i call my API to invoke the function ('execMyCallback')
	execMyCallback myStruct 42;
	//...
	);
	
	// other code lines
	0;;

- exec / execch :

These two function allow to invoke another function. Bracketed values are the arguments for this function.

- Why write an API ?

It may seem more difficult but it's wrong.
Once defined, your API allows you to manage easily your project.
By example :
- if you set (or get) many times a field you write an unique function (a 'setter / a 'getter'')
- if you must modify a set, you modify only the 'setter', not each set anywhere in your project
etc, a lot of good reasons smile

Offline

#15 5-Nov-2014 22:04:17

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

Re: general scol coding question: better to use structs than globals

thx Iri...so I can put a struct in a function definition (as long as the struct is defined upstream)?

struct superrobot
//code
....
fun [MyOwnSubStruct superrobot] I

Offline

#16 5-Nov-2014 22:08:24

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

Re: general scol coding question: better to use structs than globals

I don't understand

Offline

#17 5-Nov-2014 22:10:23

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

Re: general scol coding question: better to use structs than globals

I'll explain tomorrow.

Offline

#18 5-Nov-2014 22:19:54

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

Re: general scol coding question: better to use structs than globals

Ok.

When you define a structure, you define also a new Scol type. In my example above, MyOwnStruct and MyOwnSubStruct are two new types in the current channel.
It is better and more readable to define a such new type before its use, but you could define after. But you must define it anywhere.

You can read too the log file, about a defined structure. You'll see implicites functions (not written in the pkg) that the compiler admits.

Offline

#19 6-Nov-2014 19:29:36

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

Re: general scol coding question: better to use structs than globals

so using structs in other structs allows a form of inheritance (from a basic class to a class that extends a basic class, like in java)

I see how this is useful in games/simulations, since you could have an animal struct (it eats other living things) and a fish struct that is an instance of the animal struct (it eats other living things, plus it swims), and a shark struct (which is an animal which is a fish which eats rather large living things)

Offline

#20 7-Nov-2014 00:45:07

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

Re: general scol coding question: better to use structs than globals

I answer as soon as possible.

Offline

#21 7-Nov-2014 10:53:26

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

Re: general scol coding question: better to use structs than globals

Scol doesn't manage the inheritance natively.
However, we can write a "pseudo inheritance". This is far to be perfect, far to be like in Java.

I write a quick and very basic example with few structures and few fields. The method is relatively simple. It may be sufficient under some projects.

I use also typedef to define a new type. It allows us to add functionalities to manage our class (structures).
This is more complicated but once the code written, its use is more powerful.
About typedef, you may also read the Scol tutorial by Sylvain Huet : http://redmine.scolring.org/documents/7
and the wiki : http://redmine.scolring.org/projects/tu … es_in_Scol

This example is complete, it displays some informations about few animals. You can play with it, if you want

/* 
 * 
 * PRIVATE PART 
 * 
 */

/* I create some structures :
 * - Animal
 * - Fish
 * - Cattle
 * - Shark
 * - Herring
 * - Cow
 *
 * - Life (new type by typedef)
 * 
 * Animal is the big father (ancestor)
 * Fish and Cattle are its children
 * Fish have two children : Shark and Herring
 * Cattle has one child : Cow
 * 
 * If any, each structure has a field from each child (except the terminal
 * nodes, of course).
 * 
 * I obtain a tree :
 * 
 *                     Animal
 *                       |
 *               -----------------
 *              |                 |
 *             Fish            Cattle
 *              |                 |
 *         ---------              |
 *        |         |             |
 *      Shark   Herring          Cow      >> Life (being living)
 * 
 */
struct Animal = [
	onEarth : I,		// 1 : yes, 0 : no (E.T.)
	coldBlooded : I,	// 1 : yes, 0 : no (warm blooded)
	skin :  I			// bristles or scales or feathers
	
	] mkAnimal;;
	
struct Fish = [
	ocean : I,			// atlanticOcean, pacificOcean, indianOcean or otherWater
	fAnimal : Animal
	
	] mkFish;;
	
struct Cattle = [
	isMale : I,			// 1 : yes, 0 : no
	eatGrass : I,		// 1 : yes, 0 : no (straw, ...)
	cAnimal : Animal
	
	] mkCattle;;
	
struct Shark = [
	eatPlankton : I,	// 1 : yes, 0 : no
	sFish : Fish
	
	] mkShark;;
	
struct Herring = [
	isBloater : I,		// 1 : yes, 0 : no
	hFish : Fish
	
	] mkHerring;;
	
struct Cow = [
	isBeef : I,			// 1 : yes, 0 : no
	cCattle : Cattle
	
	] mkCow;;
	
	
/* I create a new type 'Life'. It is common to all terminal nodes (Shark, Herring and Cow).
 * Note : i could have done it for other nodes too, the code would has been
 * different.
 * 
 * It allows to have several possible types for a same variable or any 
 * other valid object. This is an equivalent at the C-union. This is 
 * normally forbidden in Scol (the type of a variable is strong, static, 
 * not modifiable).
 * For each different type inside the new type, an internal and opaque 
 * function, named constructor, translates the value object.
 * (see : http://redmine.scolring.org/projects/tutorials/wiki/Types_in_Scol)
 * 
 * Here, i've just define this type for the terminal nodes (Shark, Herring and Cow).
 * Of course, it is possible to do what we want.
 */
typedef Life =
	LifeShark Shark			// LifeShark is the constructor for the Shark object - fun [Shark] Life
	| LifeHerring Herring	       // LifeHerring is the constructor for the Herring object - fun [Herring] Life
	| LifeCow Cow			// LifeCow is the constructor for the Cow object - fun [Cow] Life
	| LifeOther;;			// LifeOther is the constructor for all other objects - fun [] Life
	

/* Here are my constants : 
 * 
 * I prefer use an explicit function instead of 'var name = value'
 * because i'm never sure that its value won't change later, during the running.
 * With a such function, i'm sure that the same value will always be returned.
 * 
 * - fun [] I
 */	
fun bristles ()= 1;;
fun scales ()= 2;;
fun feathers ()= 3;;

fun atlanticOcean ()= 1<<1;;
fun pacificOcean ()= 1<<2;;
fun indianOcean ()= 1<<3;;
fun otherWater ()= 1<<4;;



/* Miscellaneous functions */
// fun [I] S
fun whatIsSkin (skin)=
	if skin == bristles then
		"and its skin is 'bristles'"
	else if skin == scales then
		"and its skin is 'scales'"
	else if skin == feathers then
		"and its skin is 'feathers'"
	else
		"and its skin is 'another skin type'";;
		
// fun [Shark] S
fun isShark (shark)= strcat "It is a shark, it " 
						if shark.eatPlankton then "eats plankton " 
						else "doesn't eat plankton ";;
// fun [Herring] S
fun isHerring (herring)= if herring.isBloater then "It is a bloater " 
						else "It is a herring ";;
// fun [Cow] S
fun isCow (cow)= 
	let cow.cCattle -> cattle in
	strcat if cow.isBeef then "It is a beef which " else "It is a cow which "
			if cattle.eatGrass then "eats grass " else "doesn't eat grass ";;
// fun [] S
fun isOther ()= "It is another animal ";;

// check the given value, if it is incorrect, a default value is returned
// fun [I I] I
fun checkBoolean (value, default)=
	if (value == 0) || (value == 1) then value else default;;



/* Initializations 
 * each field are at nil, except the parent, if given */
fun initAnimal ()=
	mkAnimal [nil nil nil];;
	
fun initFish (animal)=
	mkFish [nil animal];;
	
fun initCattle (animal)=
	mkCattle [nil nil animal];;
	
fun initShark (fish)=
	mkShark [nil fish];;
	
fun initHerring (fish)=
	mkHerring [nil fish];;
	
fun initCow (cattle)=
	mkCow [nil cattle];;

	
/* GETTERS and SETTERS */

/* For ANIMALS */
// define if the Animal is on Earth or not - fun [Animal I] Animal
fun setAnimalEarth (animal, true)=
	set animal.onEarth = checkBoolean true 1;
	animal;;
	
// define if the Animal has the warm- (0) or -cold (1) blood - fun [Animal I] Animal
fun setAnimalBlood (animal, blood)=
	set animal.coldBlooded = checkBoolean blood 1;
	animal;;
	
// define the type of the skin : bristles, scales, feathers - fun [Animal I] Animal
fun setAnimalSkin (animal, type)=
	set animal.skin = type;
	animal;;
	
// return the skin of the given Animal - fun [Animal] I
fun getAnimalSkin (animal)=
	animal.skin;;
	
/* For FISHES */
// fun [Fish I] Fish
fun setFishOcean (fish, where)=
	set fish.ocean = where;
	fish;;
	
/* For SHARK */	
// fun [Shark] I
fun getSharkPlankton (shark)=
	shark.eatPlankton;;

// fun [Shark] I
fun getSharkSkin (shark)=
	let shark.sFish -> fish in
	let fish.fAnimal -> animal in
	animal.skin;;
	
/* For HERRING */
// fun [Herring] I
fun getHerringBloater (herring)=
	herring.isBloater;;

// fun [Herring] I
fun getHerringSkin (herring)=
	let herring.hFish -> fish in
	let fish.fAnimal -> animal in
	animal.skin;;
	
/* For CATTLE */	
// fun [Cattle I] Cattle
fun setCattleGrass (cattle, true)=
	set cattle.eatGrass  = checkBoolean true 1;
	cattle;;

// fun [Cattle] I
fun getCattleGrass (cattle)=
	cattle.eatGrass;;
	
/* For COW */
// fun [Cow] I
fun getCowBeef (cow)=
	cow.isBeef;;
	
// fun [Cow] I
fun getCowSkin (cow)=
	let cow.cCattle -> cattle in
	let cattle.cAnimal -> animal in
	animal.skin;;
	
	
		
	
/* 
 * 
 * PUBLIC PART 
 * 
 */

/* ANIMALS */
// create an empty Animal - fun [] Animal
fun createAnimal ()=
	initAnimal;;
		
// create an Animal with these specifications - fun [I I I] Animal
fun newAnimal (earth, blood, skintype)=
	let initAnimal -> animal in
	(
	set animal.onEarth = checkBoolean earth 1;
	set animal.coldBlooded = checkBoolean blood 0;
	set animal.skin = skintype;
	animal
	);;
	
/* FISHES */
// Create an empty Fish - fun [Animal] Fish
fun createFish (animal)=
	initFish animal;;

// Create a Fish - fun [Animal I] Fish
fun newFish (animal, where)=
	let initFish animal -> fish in
	(
	set fish.ocean = where;
	fish
	);;
	
/* SHARK */	
// Create a Shark - fun [I Fish] Life
fun newShark (plankton, fish)=
	let initShark fish -> shark in
	(
	set shark.eatPlankton = checkBoolean plankton 0;
	LifeShark shark
	);;
	
/* HERRING */
// Create a herring - fun [I Fish] Life
fun newHerring (bloater, fish)=
	let initHerring fish -> herring in
	(
	set herring.isBloater = checkBoolean bloater 0;
	LifeHerring herring
	);;
	
/* CATTLE */
// Create an empty Cattle - fun [Animal] Cattle
fun createCattle (animal)=
	initCattle animal;;
	
// Create a Cattle - fun [Animal I] Cattle
fun newCattle (animal, grass)=
	let initCattle animal -> cattle in
	(
	set cattle.eatGrass  = checkBoolean grass 1;
	cattle
	);;
	
/* COW */
// Create a new Cow - fun [I Cattle] Life
fun newCow (beef, cattle)=
	let initCow cattle -> cow in
	(
	set cow.isBeef = checkBoolean beef 0;
	LifeCow cow
	);;


/*
 * Here, i use my Life objects.
 * 
 * An object defined by typedef must be processed using the 'match' function :
 * Each constructor allows to differentiate the cases.
 * The last empty constructor ( '_' ) is always the default constructor
 * if no specific constructor has been found previously. In our example,
 * an LifeOther object will be taken by the default constructor.
 * You must always define a default constructor.
 *
 * The ' | ' provides the logical OR between each defined type.
 * If you can write several instructions by sub block, you must use brackets.
 * Each block must return the same type (here, a string ( S )).
 * 
 * see : http://redmine.scolring.org/projects/tutorials/wiki/Types_in_Scol 
 *
 * I write only one function for all being living. Without my Life object, 
 * i should write a function for each.
 */	
// Display the skin type of a Life object - fun [Life] I
fun printSkin (life)=
	match life with
	(LifeShark s -> whatIsSkin getSharkSkin s)
	| (LifeHerring h -> whatIsSkin getHerringSkin h)
	| (LifeCow c -> whatIsSkin getCowSkin c)
	| (_ -> whatIsSkin nil);;
	
// Display the type of a Life object - fun [Life] I
fun printIs (life)=
	match life with
	(LifeShark s -> isShark s)
	| (LifeHerring h -> isHerring h)
	| (LifeCow c -> isCow c)
	| (_ -> isOther);;
	
	

/* MAIN */	



fun main ()=
	_showconsole;
	
	let newAnimal 1 1 scales -> firstAnimal in
	let newAnimal 1 1 bristles -> secondAnimal in
	
	let newFish firstAnimal pacificOcean -> firstFish in
	let newFish firstAnimal atlanticOcean|indianOcean|pacificOcean -> secondFish in
	
	let newCattle secondAnimal nil -> cattle in
	
	let newShark 0 firstFish -> firstShark in
	let newShark 1 firstFish -> secondShark in
	
	let newHerring 1 secondFish -> herring in
	
	let newCow 0 cattle -> firstCow in
	let newCow 1 cattle -> secondCow in
	(
	_fooS "First animal";
	_fooS strcat printIs firstShark printSkin firstShark;
	_fooS "Second animal";
	_fooS strcat printIs secondShark printSkin secondShark;
	_fooS "Third animal";
	_fooS strcat printIs herring printSkin herring;
	_fooS "Fourth animal";
	_fooS strcat printIs firstCow printSkin firstCow;
	_fooS "Fifth animal";
	_fooS strcat printIs secondCow printSkin secondCow;
	
	0
	);;

Offline

#22 7-Nov-2014 13:32:27

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

Re: general scol coding question: better to use structs than globals

wow iri that is a great tutorial, it will take me a while to comprehend. it's great to use the typedef in a concrete example. yet another section for that book!


a question about 'Life'....is it a parent class (or pseudo class, for a coder if it works like a class and can be used as a class, psuedo is not that far from actual) of Animal?
if I weren't to use Typedef, how would that make the coding less efficient (i assume that using typedef saves you from writing more code)




on a cladistic note, i'll play with this and (try) to change:

                    Animal
 *                       |
 *               -----------------
 *              |                 |
 *             Fish            Cattle
 *              |                 |
 *         ---------            |
 *        |         |             |
 *      Shark   Herring   Cow      >> Life (being living)

to

                    Vertebrate
 *                       |
 *               -----------------
 *              |                 |
 *             Fish            Mammal
 *              |                 |
 *         ---------            |
 *        |         |             |
 *      Shark   Herring   Cow      >> Animal (living being that eats other living beings)

Last edited by hebdemnobad (7-Nov-2014 13:54:29)

Offline

#23 7-Nov-2014 15:40:38

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

Re: general scol coding question: better to use structs than globals

hebdemnobad wrote:

a question about 'Life'....is it a parent class

No. Not in the inheritance meaning. It is not a class like the structures (Shark, Fish, ...). Maybe an Interface (i'm not really sure !)

Life encapsulates (encloses in a black box, in an opaque object) my three being living (Shark, Herring and Cow) in a same object to manage it easily.

This rejoins rather the object-oriented programming (OOP).
In each class (Cow, Animal, Fish, ...), you write the specific code, in particular the getters and the setters (like in Python or Java, by example). You can do a package by class, if you wish.

Life comes in your project code (the core). It handles yours objects (constructor function) and dispatch to their respective class (match function).

// my first class
struct AClass = [
	...
	] mkAClass;;

// my second class
struct AnotherClass = [
	...
	] mkAnotherClass;;

// my encapsulated type
typedef BlackBox =
	BB_AClass AClass
	| BB_AnotherClass AnotherClass
	| BB_Other;;

BB_AClass BB_AnotherClass and BB_Other are my constructors from BlackBox to my classes.
To add a class object in my black box, i use my constructor :

// fun [ ] BlackBox
fun newAClass ()=
	BB_AClass mkAClass [...];;

// fun [ ] BlackBox
fun newAnotherClass ()=
	BB_AnotherClass mkAnotherClass [...];;

fun main ()=
       let newAClass -> bb1 in
       let newAnotherClass -> bb2 in
      ...
      ;;

These functions return me a BlackBox object always.

The accessor is the match function :

fun getFieldInBB (blackbox)=
	match blackbox with
	(BB_AClass obj -> "it is a AClass object)
	| (BB_AnotherClass obj -> "it is a AnotherClass object)
	| (_ -> nil);;
hebdemnobad wrote:

can be used as a class, pseudo is not that far from actual) of Animal?

You could do that but it would be useless. Or i don't understand your question.


hebdemnobad wrote:

if I weren't to use Typedef, how would that make the coding less efficient

Your code will be different.


hebdemnobad wrote:

(i assume that using typedef saves you from writing more code)

Yes and no. This depends on your code design.
However, if the design is good, your code should be simpler for writing, more readable and more maintainable. Once all this understood ! smile

Offline

#24 7-Nov-2014 15:42:37

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

Re: general scol coding question: better to use structs than globals

hebdemnobad wrote:

on a cladistic note, i'll play with this and (try) to change:

Yes, you can.
And add the missing getters and setters.

Offline

Board footer

Powered by FluxBB