PPP Exercises: Dynamic Types


Exercise: Type-safe exchange of values

A Tycoon store is not a 'closed world'. Tycoon values can be exchanges between Tycoon stores via files or network connections (sockets, RPC). Try the following demo that simulates the exchange of a Tycoon value via a file:
import unsafe store; (* from machineenv *)
let florian = tuple "Florian" 32 end;
store.fileExtern(florian "value.x");
let untypedValue = store.fileIntern("value.x");
let typedValue = unsafe.typeCast(untypedValue  :Tuple :String :Int end);
Which errors could occur when this exchange mechanism is used?

Do you see any problems in the following statement?

let typedValue = unsafe.typeCast(untypedValue  :Tuple :Int :String end);
Try to exchange the value 'florian' in a type-safe manner using the module 'dynamic' (from stdenv). Is it possible to give this value the type 'Tuple :Int :String end'? Why (not)? Is it possible to give this value the type 'Tuple :String end'? Why (not)?

Exercise: Inspection of polymorphic run-time values

Write a function that gets a tuple value and prints the field names of this tuple. Use the module 'typeRep' (from stdenv):
let printTupleFields(Dyn T <:Tuple end) = ...
Test with:
printTupleFields(:Tuple name :String  age :Int end);
Let Person = Tuple name :String  age :Int end;
printTupleFields(Person);

Gerald Schröder, 1996