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++:

  1. struct(circle)
  2. {
  3. if(params.size() != 2)
  4. error("circle: expected 2 parameters, got $[params.size]")
  5. :=(int, $[params.name].radius = params.at(0))
  6. :=(string, $[params.name].colour = params.at(1))
  7. }
  8.  
  9. :=(circle, c(2, "fluro green"))
  10. console("radius: ", c.radius)
  11. console("colour: ", c.colour)

  1. struct(pair)
  2. {
  3. if(types.size() != 2)
  4. error("pair: expected 2 types, got types.size()")
  5. if(params.size() != 2)
  6. error("pair: expected 2 parameters, got params.size()")
  7. :=(types.at(0), $[params.name].first = params.at(0))
  8. :=(types.at(1), $[params.name].second = params.at(1))
  9. }
  10.  
  11. :=(pair<int, string>, p(10, "hello, world!"))
  12. console("p.first: ", p.first)
  13. console("p.second: ", p.second)

  1. struct(vector)
  2. if(params.size() != 2)
  3. error("vector: expected 2 parameters, got ", params.size())
  4.  
  5. int $[params.name].size = params.at(0)
  6.  
  7. for{!s}(int i=0; i<$[params.name].size; i+=1; forget(i))
  8. "types.at(0)" $[params.name][$[i]](params.at(1))
  9.  
  10. function{pb}($[params.name].str)
  11. join($[params.name], " ")
  12.  
  13. function{pb}($[params.name].print)
  14. console($[params.name].str())
  15.  
  16. function{pb, !s}($[params.name].push_back)
  17. "@types.at(0)" $[params.name][\$[$[params.name].size]](params.at(0))
  18. ++($[params.name].size)
  19.  
  20. function{pb}($[params.name].pop_back)
  21. --($[params.name].size)
  22. forget($[params.name][\$[$[params.name].size]])
  23.  
  24. vector<int> v(5, 4);
  25.  
  26. v.push_back(32);
  27. console(v.str())
  28. console(v.size)
  29.  
  30. v.pop_back();
  31. console(v.str())
  32. console(v.size)

n++ example

Examples of struct being used with n++:

  1. @struct(circle)
  2. {
  3. if(params.size() != 2)
  4. error("circle: expected 2 parameters, got $[params.size]")
  5. :=(int, $[params.name].radius = params.at(0))
  6. :=(string, $[params.name].colour = params.at(1))
  7. }
  8.  
  9. @:=(circle, c(2, "fluro green"))
  10. @console("radius: ", c.radius)
  11. @console("colour: ", c.colour)

  1. @struct(pair)
  2. {
  3. if(types.size() != 2)
  4. error("pair: expected 2 types, got types.size()")
  5. if(params.size() != 2)
  6. error("pair: expected 2 parameters, got params.size()")
  7. :=(types.at(0), $[params.name].first = params.at(0))
  8. :=(types.at(1), $[params.name].second = params.at(1))
  9. }
  10.  
  11. @:=(pair<int, string>, p(10, "hello, world!"))
  12. @console("p.first: ", p.first)
  13. @console("p.second: ", p.second)