Learning BSV‎ > ‎Data Types‎ > ‎

typedef

Overview

Type synonyms are just for convenience and readability, allowing one to define shorter or more meaningful names for existing types. The new type and the original type can be used interchangeably anywhere.

The exception is that each typedef of an enum, struct or union introduces a new type that is different from all other types. For example, even if two typedefs give names to struct types with exactly the same corresponding member names and types, they define two distinct types.

Syntax

typedef type Identifier;

Note: The new type name must start with an uppercase letter.

Examples

Type Synonyms:

typedef Int#(32) TDataX;
typedef Int#(32) TDataY;
typedef Bool TFlag;

TDataX a = 10;
TDataY b = a; // this is ok, since TDataX and TDataY are synonyms of the same type

Struct Definition:

typedef struct { int x; int y; } Coord;
typedef struct { int x; int y; } Position;

Coord coord ;
Position pos;

coord = pos; // typechecking error

Even though the struct definitions are identical, they are unique types.

Comments