Classes

1.Class definition

User classes can be defined within Mmx-light, as shown in the following example:

Mmx] 

type_mode? := true

:

Mmx] 

class Color == {

mutable { r: Double; g: Double; b: Double; }

constructor grey (x: Double) == {

r == x; g == x; b == x; }

constructor rgb (r2: Double, g2: Double, b2: Double) == {

r == r2; g == g2; b == b2; }

}

Mmx] 

rgb (1, 0, 0)

:

Mmx] 

flatten (c: Color): Syntactic ==

syntactic ('rgb (as_generic flatten c.r,

as_generic flatten c.g,

as_generic flatten c.b));

Mmx] 

rgb (1, 0, 0)

:

Mmx] 

mix (c1: Color, c2: Color, a: Double): Color ==

rgb (a * c1.r + (1-a) * c2.r,

a * c1.g + (1-a) * c2.g,

a * c1.b + (1-a) * c2.b);

Mmx] 

mix (rgb (1, 0, 0), grey (0.5), 0.5)

:

2.Type conversions

The operator :> can be used to convert a given type into another, provided that the corresponding function downgrade is defined. We show here an example of an explicit conversion from a class Alpha_color to the class Color:

Mmx] 

class Alpha_color == {

mutable { c: Color; a: Double; }

constructor alpha_color (c2: Color, a2: Double) == { c == c2; a == a2; }

};

Mmx] 

alpha_color (rgb(0,0,1), 0.5)

:

Mmx] 

downgrade (ac: Alpha_color): Color == mix (ac.c, rgb(1,1,1), ac.a);

Mmx] 

alpha_color (rgb(0,0,0), 0.6) :> Color

:

In order to define implicit conversions from a type into another, one can use the function updgrade. We complete the example with a convert from Double to Color:

Mmx] 

upgrade (x: Double): Color == grey x;

Mmx] 

mix (1.0, rgb (0, 1, 0), 0.2)

:

Mmx] 

mix (0.8, 0.2, 0.4)

:

Mmx] 

postfix .greyed (c: Color): Color == (c.r + c.g + c.b) / 3;

Mmx] 

rgb (1, 0, 0).greyed

:

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU General Public License. If you don't have this file, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.