Seed7 - The extensible programming language
Seed7 FAQ Manual Screenshots Examples Libraries Algorithms Download Links
Examples echo args file in string simple clock count words subtype map function generic func declare stmt template operator 3-way if
Examples
Subtype for roman numbers
 previous   up   next 

This example declares a subtype for roman numbers

$ include "seed7_05.s7i";       # Standard Seed7 library
  include "wrinum.s7i";         # Import str(ROMAN, ... )

const type: romanNum is subtype integer;

const func string: str (in romanNum: number) is
  return str(ROMAN, number);

enable_output(romanNum);

const proc: main is func
  local
    var romanNum: number is 0;
  begin
    for number range 1 to 3999 do
      writeln(number);
    end for;
  end func;

The subtype romanNum can be used everywhere an integer can be used. The 'str' function declared for romanNum cannot be used by integers. The template enable_output uses the 'str' function of romanNum to define write, writeln, etc. for romanNum. Therefore romanNums are written as roman numbers while integers are written in the usual Arabic numeral system.

To write an integer directly as roman number use 'write(str(ROMAN, number))'.


 previous   up   next