Language

Casting

Introduction

Casting is a way to change some value from a type to another one.

Pliant casting machinery is a fairly sophisticated one since several casting functions can transparently be involved to get the result if no direct one is available. In the end, it is just powerful enough for built in numerical data types to have no special handling.
This article is explaining how to use casting, and how to define casting functions. For the description of Pliant compiler casting machinery, see Pliant compiler machinery article.

Usage

Implicit casting

var Int i
var Float f
f := i

Explicit casting

var Int i
var Float f
i := cast f Float

Defining casting functions

function 'cast Int' f -> i
  arg Float f ; arg Int i
  ...

If no attribut is provided, 'explicit' will be assumed. See bellow.

function 'cast uInt16_li' i -> ii
  arg uInt16 i ; arg uInt16_li ii
  implicit
  ...

An 'implicit' casting function shall be used to provide implit casting.

function cast_Int_Float i -> f
  arg Int i ; arg Float f
  extension
  ...

'extension' is the same as 'implicit', but it futhermore specify that the target data type is a superset of the source one. In this example, 'Float' is considered as a superset of 'Int'.

function 'cast Float32' f -> f32
  arg Float f ; arg Float32 f32
  reduction
  ...

'reduction' means that the source data type is a subset of the target one. In this sample, 'Float' is considered as a superset of 'Float32'.
A reduction casting function will be considered implicit only in an affectation instruction, explicit everywhe else. Here is an example:

var Float f
var Float32 g := f # accepted in an affectation
function foo h
  arg Float32 h
foo f # rejected because it's not an affectation
foo (cast f Float32) # explicit casting

function 'cast Status' f -> s
  arg Float f ; arg Status s
  explicit
  ...

An 'explicit' casting function can be used only in an ... explicit casting.