Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
language:types2 [2024/07/18 16:51] – [Default parameters] rajit | language:types2 [2025/05/02 10:18] (current) – [Default parameters] rajit | ||
---|---|---|---|
Line 199: | Line 199: | ||
Note that ACT is very strict about type-checking; | Note that ACT is very strict about type-checking; | ||
- | ===== Direction flags and user-defined | + | |
+ | ==== Grouping parameters | ||
+ | |||
+ | Parameters can be combined into [[language: | ||
+ | |||
+ | ===== Direction flags | ||
Line 251: | Line 256: | ||
==== Macros ==== | ==== Macros ==== | ||
+ | |||
+ | A macro is, as the name sounds, a CHP fragment. This fragment is used to substitute for the macro in the places where it is used. Macros can be defined for any user-defined type (except for parameter structures). | ||
+ | |||
+ | <code act> | ||
+ | defproc stack (chan(int)? in; chan(int)!out ) | ||
+ | { | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In ordinary circumstances, | ||
+ | |||
+ | <code act> | ||
+ | ... | ||
+ | stack s; | ||
+ | int x; | ||
+ | ... | ||
+ | chp { | ||
+ | ... | ||
+ | | ||
+ | ... | ||
+ | | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | As an alternative, | ||
+ | |||
+ | <code act> | ||
+ | defproc stack (chan(int)? in; chan(int)!out ) | ||
+ | { | ||
+ | ... | ||
+ | methods { | ||
+ | macro push(int val) { | ||
+ | in!val | ||
+ | } | ||
+ | macro pop(int res) { | ||
+ | out?res | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The same stack use case can be written as: | ||
+ | <code act> | ||
+ | ... | ||
+ | stack s; | ||
+ | int x; | ||
+ | ... | ||
+ | chp { | ||
+ | ... | ||
+ | | ||
+ | ... | ||
+ | | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
==== Functions ==== | ==== Functions ==== | ||
+ | In addition to macros, [[language: | ||
+ | |||
+ | <code act> | ||
+ | deftype signed_int (bool s; int< | ||
+ | { | ||
+ | methods { | ||
+ | function negative() : bool | ||
+ | { | ||
+ | chp { | ||
+ | self := s | ||
+ | } | ||
+ | } | ||
+ | function mag() : int< | ||
+ | { | ||
+ | chp { | ||
+ | self := v | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | With this definition, a user can use method calls to access the fields of the structure as follows: | ||
+ | |||
+ | <code act> | ||
+ | | ||
+ | ... | ||
+ | chp { | ||
+ | ... | ||
+ | [ s.negative() -> log (" | ||
+ | [] else -> log (" | ||
+ | ] | ||
+ | } | ||
+ | ... | ||
+ | </ | ||
+ | |||
+ | Note that functions cannot have any side-effects; | ||
+ | any of the members of the pure structure. Macros can be used to change those. | ||
+ | |||
+ | === Operator overloading === | ||
+ | |||
+ | Functions within pure structures are also used to support operator overloading. In particular, the following function methods are interpreted to be the definition of operator overloading for arithmetic operators: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | An example of a fixed-point arithmetic datatype is provided in the [[https:// | ||
+ | In the linked example, '' | ||