fn: struct
[contents]

Contents

Syntax

The syntax for struct definitions is:

f++:  
struct{options}(name)
{
	//struct block
}

n++:  
@struct{options}(name)
{
	//struct block
}

Description

The struct function is for user-defined structures/types, it takes a single parameter specifying a structure/type name and is followed by a block of code to be run whenever an object of that type is defined.

Note: Nift will skip to the first non-whitespace (ie. to the first character that is not a space, tab or newline) after a struct definition and inject it to the output file where the definition started. If you want to prevent Nift from doing this put a '!' after the definition, eg.:

@struct(s)
{
	# struct block
}!

Options

The following options are available for struct calls (more to come):

option description
f++ struct block uses f++ (default)
n++ struct block uses f++
pb parse struct block at definition time
option description

f++ example

Examples of struct being used with f++:

struct(circle)
{
	if(params.size() != 2)
		error("circle: expected 2 parameters, got $[params.size]")
	:=(int, $[params.name].radius = params.at(0))
	:=(string, $[params.name].colour = params.at(1))
}

:=(circle, c(2, "fluro green"))
console("radius: ", c.radius)
console("colour: ", c.colour)

struct(pair)
{
	if(types.size() != 2)
		error("pair: expected 2 types, got types.size()")
	if(params.size() != 2)
		error("pair: expected 2 parameters, got params.size()")
	:=(types.at(0), $[params.name].first = params.at(0))
	:=(types.at(1), $[params.name].second = params.at(1))
}

:=(pair<int, string>, p(10, "hello, world!"))
console("p.first: ", p.first)
console("p.second: ", p.second)

n++ example

Examples of struct being used with n++:

@struct(circle)
{
	if(params.size() != 2)
		error("circle: expected 2 parameters, got $[params.size]")
	:=(int, $[params.name].radius = params.at(0))
	:=(string, $[params.name].colour = params.at(1))
}

@:=(circle, c(2, "fluro green"))
@console("radius: ", c.radius)
@console("colour: ", c.colour)

@struct{f++}(pair)
{
	if(types.size() != 2)
		error("pair: expected 2 types, got types.size()")
	if(params.size() != 2)
		error("pair: expected 2 parameters, got params.size()")
	:=(types.at(0), $[params.name].first = params.at(0))
	:=(types.at(1), $[params.name].second = params.at(1))
}

@:=(pair<int, string>, p(10, "hello, world!"))
@console("p.first: ", p.first)
@console("p.second: ", p.second)

Sponsors