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 n++
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)

struct(vector)
	if(params.size() != 2)
		error("vector: expected 2 parameters, got ", params.size())

	int $[params.name].size = params.at(0)

	for{!s}(int i=0; i<$[params.name].size; i+=1; forget(i))
		"types.at(0)" $[params.name][$[i]](params.at(1))

	function{pb}($[params.name].str)
		join($[params.name], " ")

	function{pb}($[params.name].print)
		console($[params.name].str())

	function{pb, !s}($[params.name].push_back)
		"@types.at(0)" $[params.name][\$[$[params.name].size]](params.at(0))
		++($[params.name].size)

	function{pb}($[params.name].pop_back)
		--($[params.name].size)
		forget($[params.name][\$[$[params.name].size]])
			

vector<int> v(5, 4);

v.push_back(32);
console(v.str())
console(v.size)

v.pop_back();
console(v.str())
console(v.size)

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(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)