Language

Types

Most of the features in this document requires to include '/pliant/language/compiler.pli' module.

Defining a new data type

type Pair
  field Int x y <- 0

The '<-' parameter is optinal and provides initialization value for the fields.

When a new type is created, Pliant shall reverse fields ordering and insert padding bytes to optimize fields alignment. If this is not expected because the type is used by another C program as an example, you can use 'packed' instruction to desable any initiative on Pliant side.

type XColor
  packed
  field uInt pixel
  field uInt16 red green blue
  field uInt8 flags pad

Just as for functions, it should be possible to use 'later' to escape from a circular reference between types, also I just could not find an example at document writing time.
There is also a 'generic_level' instruction to force the level in Pliant generic functions machinery, but I've never used it, so it's unlikely that you will.
Please drop an email to hubert.tonneau@fullpliant.org if you get to the point where you would need to use so better understand these features.

Virtual fields

One key point of Pliant syntax is to enable no difference at syntax level between a field and a method. This is very important, because in a real software life cycle, a field becoming a method is a classical event, so avoiding to have to change all applications using it is a serious bonus.
This is so much true that it often happen the other way round: since developers are aware of this issue, they prevent access to any field right from the beginning and require to use methods for everything ... with a lot of no use glue code as a result.
Using the same syntax for fields and methods is the right way to resolve this dilemma in most situations.

Here is a sample:

type Vector
  field Array:Float comp

method v size -> i
  arg Vector v ; arg Int i
  i := v:comp size

var Vector v
console v:size eol

In this example, we see that 'size' method is used exactly the same way as if it was a real field.
Let's go one step futher:

method v 'size :=' i
  arg_rw Vector v ; arg Int i
  check i>=0
  v:comp size := i

var Vector v
v size := 3

Through defining 'size :=' method, we turn 'size' to a full virtual field that can also be assigned a new value.
The only limit is that I have not implemented yet the meta programming glue code that would enable 'size' to work in a function that want write access to it:

function foo i
  arg_rw Int i
  ...

var Vector v
foo v:size # does not work at the moment

Getting the type of something

You can get the type of any data using 'typeof' keyword:

var Pointer:Type t :> typeof 3
console t:name eol

Looks simple, but be warned that it's not so:

   •   

'typeof' will return the type of the real data, so will drop links and references.

   •   

'typeof' works at compiler semantic level, not at execution level.

Here are two sample to illustrate this:

var Pointer:Int a
console typeof:a:name eol

will print 'Int', not '(Pointer Int)'

module "/pliant/graphic/image/prototype.pli"
module "/pliant/graphic/image/pixmap.pli"
var Link:ImagePixmap img :> new ImagePixmap
var Link:ImagePrototype p :> img
console typeof:p:name eol

will print 'ImagePrototype', not 'ImagePixmap'.

In the previous sample, if you want to get 'ImagePixmap', just do:

console (entry_type addressof:p):name eol

Studying Pliant standard types implementation

Most Pliant data types are defined in /pliant/language/type/ part of the source tree.