User types
In a Mosel model, the user may define new types that will be treated in the same way as the predefined types of the Mosel language. New types are defined in declarations blocks by specifying a type name, followed by =, and the definition of the type. The simplest form of a type definition is to introduce a new name for an existing type, such as:
declarations myint = integer myreal = real end-declarations
In the section on records above we have already seen an example of a user type definition for records (where we have named the record `mydata'). Another possible use of a user type is as a kind of `shorthand' where several (data) arrays have the same structure, such as in the model blend.mos from Chapter Some illustrative examples, where, instead of
declarations ORES = 1..2 ! Range of ores COST: array(ORES) of real ! Unit cost of ores AVAIL: array(ORES) of real ! Availability of ores GRADE: array(ORES) of real ! Grade of ores (measured per unit of mass) end-declarations
we could have written
declarations ORES = 1..2 ! Range of ores myarray = array(ORES) of real ! Define a user type COST: myarray ! Unit cost of ores AVAIL: myarray ! Availability of ores GRADE: myarray ! Grade of ores (measured per unit of mass) end-declarations
without making any other modifications to the model.
Type constructors
The following example (file arcs4.mos) defines the 'arc' record structure that we have seen in the previous section as a user type and employs this type in the definition of another data structure.
public declarations NODES: list of string ! List of nodes arc = public record ! Arcs: Source,Sink: string ! Source and sink of arc Cost: real ! Cost coefficient end-record ARCLIST: list of arc end-declarations NODES:=['A','B','C','D','E','F'] i:='S'; ct:=0 forall(j in NODES, ct as counter) do ARCLIST+= [arc(.Source:=i,.Sink:=j,.Cost:=10*ct)] i:=j end-do writeln(ARCLIST)
In this example, the type name 'arc' is used as type constructor for populating the list ARCLIST with 6 different entries, namely:
[[Source=`S' Sink=`A' Cost=10],[Source=`A' Sink=`B' Cost=20], [Source=`B' Sink=`C' Cost=30],[Source=`C' Sink=`D' Cost=40], [Source=`D' Sink=`E' Cost=50],[Source=`E' Sink=`F' Cost=60]]
Type constructors for records can be used to populate all or only a selection of the record fields. For example, this code snippet
! Initializing selected record fields writeln(arc(.Source:="S")) writeln(arc(.Sink:="Z")) writeln(arc(.Cost:=100,.Source:="XYZ"))
will result in the following output:
[Source=`S' Sink=`' Cost=0] [Source=`' Sink=`Z' Cost=0] [Source=`XYZ' Sink=`' Cost=100]
Further examples of type constructors are discussed in Section Date and time data types for the creation of date, time, and datetime data.
© 2001-2021 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.
