Learning BSV‎ > ‎Data Types‎ > ‎

Type Parameters

Simple Types

Some types don't require any parameters to be fully specified.  This are referred to as simple types.
Examples of simple types include:

Parameterized Types

Other types require one or more parameters to be defined, these are referred to as parameterized types.  Type parameters can be natural numbers (also known as size types) which indicate some aspect of the size of the type, such as a bit-width or a table capacity.  For example, you define a type as an Int#(n) the type parameter n indicates the width if the integer (how many bits wide).  Some types have multiple parameters.  A Vector type has two parameters.  The size type parameter indicates how many items are in the Vector while the other parameter indicates the type of the items contained in the Vector.

Examples of parameterized types:
 Bit#(16)</code>             // 16-bit wide bit-vector
 UInt#(32) </code>           // unsigned integers, 32 bits wide
 Int#(29)</code>             // signed integers, 29 bits wide
 Vector#(16,Int#(29)</code>  // Vector containing 16 items, each of which is an Int, 29 bits wide

Other examples of parameterized types:
 Tuple2#(int,Bool)</code>            // pair of items, an int and a Bool
 Tuple3#(int,Bool,String)</code>     // triple of items, an int, a Bool and a String
 Listt#(Bool)</code>                 // list containing booleans
 List#(List#(Bool))</code>           // list containing lists of booleans
 RegFile#(Integer, String)</code>    // a register file (array) indexed by integers
                                     // containing strings


Polymorphic Types

A parameterized type in which the parameter is represented by a specific (but unknown) type are referred to as polymorphic types.   These are indicated by using type variables as parameters. 

Examples of polymorphic types:
 List#(a)                 // lists containing items of some type a
 List#(List#(b))          // lists containing lists of items of some type a
 RegFile#(i, List#(x))    // arrays indexed by some type i, containing lists that                                                              // contain items of some type x

The type variables represent unknown (but specific) types.  In other words, List#(a) represents the type of a list containing items all of which have some type a.  It does not mean that different elements of a list can have different types.
Comments