XML

kent logo

CO538 Anonymous Questions and Answers Keyword Index

This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.

To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.

Keyword reference for data-type

2007

Question 74 (2007):

Submission reference: IN1460

If we have:

  DATA TYPE FOO
    RECORD
      [20]INT data:
  :

  CHAN [20]INT c:

  [20]INT x:

Can we use casting, so that c transfers an element of type FOO? i.e is the code:

  c ? FOO x

correct?

Answer 74:

Your data type FOO is not cast compatible with [20]INT, so neither can be cast into the other. In occam-pi, casting is a special operator used (mostly) to change the representation of data values between types. For example, to change from REAL32 to an INT, we cast and the internal representation changes dramatically.

However, if we define a new type directly from an existing type:

  DATA TYPE BAR IS [20]INT:

then we can cast between BAR and [20]INT variables freely. In this case, no change in internal representation is needed.

The result of casting is a new piece of data. So your line:

  c ? FOO x

where x is an [20]INT and channel c carries [20]INT makes no sense. It works without the (illegal attempt to) cast with the FOO operator.

If you need to turn your x data from an [20]INT into a FOO, there is a way – but not by casting! If two types have respresentations of exactly the same size, they can be RETYPESd into each other. This is described in the occam-pi reference wiki, which is linked from the Co631 module web page. Take a look at Section 10.2 "Retyping". We did not cover this during the lectures, so it is not examinable. I'll probably include it in future years, just after abbreviations in the shared-etc sildes.

For information, since your FOO obviously has the same representation size as [20]INT, retyping is allowed between them. To retype your x, RETYPES it:

  FOO x RETYPES x:
  ...  for the duration of this block, x has the type FOO

Retyping is like abbreviating: all the rules about aliasing carry over, along with VAL retyping (like VAL abbreviations). So, if we only needed to use x as a FOO and not change it:

  VAL FOO x RETYPES x:
  ...  x has the type FOO here and it's a VAL

The retyping can go the other way. If f is a FOO variable, then:

  [20]INT f RETYPES f:
  ...  for the duration of this block, f has the type [20]INT

and:

  VAL [20]INT f RETYPES f:
  ...  f has the type [20]INT here and it's a VAL

Keywords: data-type , cast , retypes

Valid CSS!

Valid XHTML 1.0!

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
Last modified Mon May 20 13:50:26 2013
This document is maintained by Fred Barnes, to whom any comments and corrections should be addressed.