Slag:Data Types

From Plasmaworks

Jump to: navigation, search

Slag has three categories of data types.


Contents

Primitives

Primitives are simple numbers and are used individually as well as being the basic building blocks of compounds and classes. Primitives are assigned by-value. There are 7 primitive types:

Name Description Literal Values
Int64 A 64-bit signed integer. In general use Int32 instead. Int64(348), 0xffff0000ff00ff00
Int32 A 32-bit signed integer with a range of +/- 2.15 billion. Use this for general purpose whole numbers. 6, -2, 0xffff8000, 0b11010
Char A 16-bit unsigned integer used primarily to represent Unicode characters. Range is 0..65535. Prints out as a symbol instead of as a character code. 'A', Char(65)
Byte An 8-bit unsigned integer with range 0..255. Used primarily for storing raw binary data. Byte(255)
Real64 A 64-bit double-precision floating point number. Use this for general purpose real numbers. -2.0, 3.14
Real32 A 32-bit single-precision floating point number. Use this in favor of Real64 only if significant space can be conserved. Real32(6.28)
Logical A binary data type with values true and false exactly like Java or C#. The literal values lt, gt, eq resolve to the integer constants 1, -1, and 0 respectively. true, false


Compounds

Example:

 compound Vector2( Real64 x, Real64 y )
 ...
 local Vector2 a, b(0,0)
 println( a == b )   # prints: true
 b.x = 5   # ERROR - can't reassign compound elements
 b = Vector2( 5, b.y )


Compound basics

  • Compounds are composite groups ("clumps") of primitives and other compounds - no references are allowed. Compounds cannot be codependent - if A contains a B, then B can't contain an A.
  • Compounds are by-value types. Assigning "x = y" makes a copy of compound y and stores it as x.
  • Compounds have no overhead. There is no memory allocation when you create one (since they're either on the stack or part of a larger object) and there is no hidden metadata. A compound containing 2 Int32 values takes up exactly 8 bytes of memory.
  • Compound members can be individually read but not individually written - to alter a compound, it must be reassigned as a compound with different values.
  • Compounds are useful when:
    • There are a small number of members (2 to 4).
    • There is a large amount of data (e.g. 10,000 Vector2 values).
    • Multiple references to the same data are unnecessary.
  • Compounds cannot contain methods but there is a trick to allow something similar. See Compound Managers in the Singletons section.
  • Compounds cannot be extended. However, you can make a compound which contains another type of compound and then adds additional members.

Technical notes

  • Compounds may not contain object references because primitives and compounds are kept on a data stack while objects are kept on a separate reference stack. Allowing compounds to store references would make garbage collection much more complex.
  • Allowing compound elements to be reassigned would introduce a huge pitfall: commands like "hero.position().x = 5" would compile fine but the "x = 5" would be altering the temporary value returned on the stack, not the hero's actual position.


Objects

Objects are bundles of data and methods (functionality) that operate on that data. Objects are dynamically allocated, assigned by-reference, and can be composed of primitives, compounds, and other object references. See Classes and Objects for more information.