Seed7 Example: Declare an operator

Seed7 FAQ Manual Demo Screenshots Examples Libraries Algorithms Benchmarks Index Download GitHub Build Seed7 Links
Examples echo args file in string simple clock count words subtype map function generic func declare stmt template operator 3-way if
Examples Declare an operator

The library "color.s7i" contains the following declarations:

const type: color is new struct
 var integer: red_part is 0;
 var integer: green_part is 0;
 var integer: blue_part is 0;
 end struct;
const func color: (in color: col1) + (in color: col2) is func
 result
 var color: sum is color.value;
 begin
 sum.red_part := (col1.red_part + col2.red_part) div 2;
 sum.green_part := (col1.green_part + col2.green_part) div 2;
 sum.blue_part := (col1.blue_part + col2.blue_part) div 2;
 end func;

The type color is declared as struct with three elements. The three integer elements describe the intensities of the primary colors red, green and blue used in the RGB additive color model. Additionally the + operator is defined to do additive mixing of two colors. The syntax of the + operator is defined in the library "syntax.s7i" as:

$ syntax expr: .(). + .() is -> 7;

A totally new operator symbol, such as 'additiveMixing', could be used instead of + . In this case the syntax of 'additiveMixing' would be:

$ syntax expr: .(). additiveMixing .() is -> 7;

Memory for color objects is managed automatically without garbage collection. Additionally it is not necessary to call constructors to initialize color objects.

AltStyle によって変換されたページ (->オリジナル) /