# Datatype representation

**URL:** <https://discourse.ats-lang.org/t/datatype-representation/772>\
**Category:** Google Group\
**Created:** [January 14, 2014, 8:18pm UTC](https://discourse.ats-lang.org/t/datatype-representation/772 "2014-01-14T20:18:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [January 14, 2014, 8:18pm UTC](https://discourse.ats-lang.org/t/datatype-representation/772/1 "2014-01-14T20:18:28Z")

</div>

Say you have a datatype declared as follows:

datatype foo = A of () | B of (int) | C of (int, double)

There are three types foo\_A, foo\_B, and foo\_C associated with foo which are  
for values constructed by  
A, B, and C, respectively. These types can be described using C-syntax as  
follows:

typedef intptr\_t foo\_A;  
typedef struct { int atslab\_0 ; } \*foo\_B ;  
typedef struct { int atslab\_0 ; double atslab\_1 ; } \*foo\_C ;

Note that the value A() is represented as an integer (intptr\_t is for  
integers that are as wide as a pointer).

Say you have a datatype for which every constructor is unary. For instance,

datatype weekday =  
| Monday  
| Tuesday  
| Wednesday  
| …

Then this is datatype is like an enum type in C. Internally, Monday(),  
Tuesday()  
and Wednesday() are represented by 0, 1, and 2 because they the first,  
second  
and third constructors associated with the datatype weekday.

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [January 14, 2014, 8:48pm UTC](https://discourse.ats-lang.org/t/datatype-representation/772/2 "2014-01-14T20:48:07Z")

</div>

You can use the following code to test what I said about  
the datatype weekday:

local  
//  
staload  
UN = “prelude/SATS/unsafe.sats”  
//  
macdef  
ptr2int (x) =  
$UN.cast{int}($UN.cast{intptr}(,(x)))  
//  
in (_in-of-local_)

val-(0) = ptr2int(Monday())  
val-(1) = ptr2int(Tuesday())  
val-(2) = ptr2int(Wednesday())  
val-(3) = ptr2int(Thursday())  
val-(4) = ptr2int(Friday())

end // end of [local]On Tuesday, January 14, 2014 3:18:28 PM UTC-5, gmhwxi wrote:

> Say you have a datatype declared as follows:
> 
> datatype foo = A of () | B of (int) | C of (int, double)
> 
> There are three types foo\_A, foo\_B, and foo\_C associated with foo which  
> are for values constructed by  
> A, B, and C, respectively. These types can be described using C-syntax as  
> follows:
> 
> typedef intptr\_t foo\_A;  
> typedef struct { int atslab\_0 ; } \*foo\_B ;  
> typedef struct { int atslab\_0 ; double atslab\_1 ; } \*foo\_C ;
> 
> Note that the value A() is represented as an integer (intptr\_t is for  
> integers that are as wide as a pointer).
> 
> Say you have a datatype for which every constructor is unary. For instance,
> 
> datatype weekday =  
> | Monday  
> | Tuesday  
> | Wednesday  
> | …
> 
> Then this is datatype is like an enum type in C. Internally, Monday(),  
> Tuesday()  
> and Wednesday() are represented by 0, 1, and 2 because they the first,  
> second  
> and third constructors associated with the datatype weekday.
