fn: struct
[contents]

Contents

Syntax

The syntax for struct definitions is:

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

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

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.

Options

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

option description
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[0]])
	:=(string, $[params.name].colour = $[params[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[0]], $[params.name].first = $[params[0]])
	:=($[types[1]], $[params.name].second = $[params[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[0]])
	@:=(string, $[params.name].colour = $[params[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[0]], $[params.name].first = $[params[0]])
	@:=($[types[1]], $[params.name].second = $[params[1]])
}

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