Initializing help system before first use

Working with unions


Type: Programming
Rating: 3 (intermediate)
Description:
  • uniondef.mos: Defining unions, assignment of values, retrieving type information, compatible union types in subroutine arguments
  • unioninit.mos: Initializing unions from a text file (requires uniondata.dat), displaying type names
File(s): uniondef.mos, unioninit.mos
Data file(s): uniondata.dat

uniondef.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file uniondef.mos 
   `````````````````
   Defining unions.
 
   (c) 2021 Fair Isaac Corporation
       author: S. Heipcke, Mar. 2021
*******************************************************!)
model "Defining unions"
  uses "mmsystem", "mmxprs"

  declarations
    u: string or real              ! Scalar accepting 'string' or 'real'
    a,a1,a2,a3: any                ! Entity accepting any type
    L: list of any                 ! List of type 'any'
  end-declarations

 ! Assigning a value determines the type of the union
  u:="a"
  writeln(u, " is string: ", u.typeid = string.id)      ! Output:  true
  u:=7.5
  writeln(u, " is real: ", u.typeid = real.id)          ! Output:  true
 ! The preceding line is equivalent to:
  writeln(u, " is real: ", u is real)                   ! Output:  true 

 ! Assignment of basic types results in constants, all others are references
  a:=10
  writeln(a, ":", a.struct=STRUCT_CONST, a is integer)  ! Output: true true
  a:=date(2020,12,24)
  writeln(a, ":", a.struct=STRUCT_REF, a is date)       ! Output: true true

 ! Can specify type in assignment to force a particular type
  a.real:=10
  writeln(a, ":", a is real, a is integer)              ! Output: true false
  a.text:="a text"
  writeln(a, ":", a is text, a is string)               ! Output: true false

 ! Employ 'create' for types that do not support assignment such as 'mpvar': 
  create(a.mpvar)
  writeln(getsol(a.mpvar), ":", a is mpvar)             ! Output: true
 ! ... or to change the type without performing any assignment:
  create(a.linctr)
  writeln(getact(a.linctr), ":", a is linctr)           ! Output: true

 !**** Defining a type name for a union of the 4 basic Mosel types
  declarations
    basictype = string or integer or real or boolean
    B: array(range) of basictype   ! Array of union type 'basictype'
  end-declarations

  B(1):="abc"; B(3):=5; B(4):=5.0
  forall(i in 1..3) writeln(B(i), ":", B(i) is string)
                                   ! Output: abc:true  ?:false  5:false
  writeln(B(2).struct=STRUCT_NIL)  ! Undefined entry has no structure
  writeln(B(2).typeid=STRUCT_NIL)  ! Undefined entry has no type
  writeln(B(3)=B(4), B(3).integer=B(4).real)    ! Output: false true

 !**** Wrapping mechanism for subroutines: compatible union types are accepted 
  procedure dosomething(aunion: any)
    writeln("In procedure: ", aunion)
  end-procedure

  a:=date(2020,12,24)   ! u:=7.5; B(1):="abc"
  dosomething(a)
  dosomething(u)
  dosomething(B(1))
  dosomething(B(10))    ! Undefined entry 

  procedure dosomething2(aunion: basictype)
    writeln("In procedure 2: ", aunion)
  end-procedure

!  dosomething2(a)      ! This would result in an error since 'date' is not a compatible type
  a:=1.5                ! u:=7.5; B(1):="abc"
  dosomething2(a)
  dosomething2(u)
  dosomething2(B(1))

end-model  

unioninit.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file unioninit.mos 
   ``````````````````
   Initializing unions from a text file.
 
   (c) 2021 Fair Isaac Corporation
       author: S. Heipcke, Mar. 2021
*******************************************************!)
model "Initializing unions"
  uses "mmsystem"

  declarations
    L: list of any                  ! List of type 'any'
    S: set of any                   ! Set of type 'any'
    mytype = text or integer or real or boolean
    B: array(R:range) of mytype     ! Array of union type 'mytype'
    TNAME: array(integer) of text   ! Type names for display
  end-declarations

 ! Reading from text format file
  initializations from "uniondata.dat"
    L  S  B
  end-initializations

  TNAME(integer.id):="integer"; TNAME(real.id):="real"
  TNAME(string.id):="string"; TNAME(text.id):="text"
  TNAME(boolean.id):="boolean"

  writeln("L=", L)
  forall(i in L) writeln("  ", i, ":", i.typeid, " ", TNAME(i.typeid))
  writeln("S=", S)
  forall(i in S) writeln("  ", i, ":", i.typeid, " ", TNAME(i.typeid))
  writeln("B=", B)
  forall(i in R)
    writeln("  B", i, "=", B(i),":", B(i).typeid, " ", TNAME(B(i).typeid)) 

 ! Writing to text format file
  declarations
    x: mpvar                        ! This type does not support 'tostring'
  end-declarations
  L+= [any(x)]
  writeln(L)

  initializations to "unionout.dat"
    L  S  B
  end-initializations

end-model  

© 2001-2022 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.