Data Types
Harneet supports a variety of built-in data types to represent different kinds of values.
Integers
Integers are whole numbers, positive or negative. Harneet provides various integer types to accommodate different ranges and memory requirements.
- Literals:
42,-17,0x1A(hexadecimal),0b1010(binary) - Fixed-size Types:
int,int8,int16,int32,int64(signed integers) - Unsigned Types:
uint,uint8,uint16,uint32,uint64,uintptr(unsigned integers) - Arbitrary-precision:
bigintfor integers that do not fit in 64 bits. - Automatic Type Inference: Harneet can automatically infer the most appropriate integer type based on the value.
- Explicit Declarations: You can explicitly declare the integer type.
- Instance Methods:
.abs(),.min(),.max(),.clamp(),.toString()(on fixed-size integers).
See Numbers for comprehensive documentation on integer, bigint, and float behavior.
Floating-point Numbers
Floating-point numbers represent real numbers with decimal points.
- Literals:
3.14,-2.71,1.23e-4(scientific notation) - Types:
float32,float64(IEEE-754 compliant) - Precision: Harneet ensures precision preservation in all floating-point operations.
- Instance Methods:
.abs(),.min(),.max(),.clamp(),.round(),.floor(),.ceil(),.isNaN(),.isInf(),.toString()
See Numbers for comprehensive documentation on integer and float methods.
Arbitrary-precision Decimals
- Type:
bigdecimalfor exact decimal and rational values with arbitrary precision. - Implementation: Backed by Go's
math/big.Rat. - Construction: Use the global
bigdecimal(...)function with integers, floats, or decimal/rational strings like"0.1"or"1/3". - Instance Methods:
.abs(),.min(),.max(),.clamp(),.toString()(same names and semantics as for integers/floats, returningbigdecimal).
See Numbers for comprehensive documentation on bigdecimal behavior and examples.
Strings
Strings are sequences of characters. Harneet has robust support for Unicode and provides both regular and multiline string literals.
Regular Strings
- Literals:
"Hello World","Unicode: 你好世界 🌍","Multi\nLine" - Encoding: UTF-8 native support for full Unicode handling.
- Escape Sequences:
\n(newline),\t(tab),\"(double quote),\\(backslash) - Concatenation: Use the
+operator to join strings.
Multiline Strings
Multiline strings use backtick delimiters (`) and are raw strings that preserve formatting without interpreting escape sequences.
- Literals:
`multiline string`,`line 1\nline 2`(literal backslash-n, not newline) - Raw String Behavior: No escape sequence processing - all characters are literal
- Formatting Preservation: Maintains exact whitespace, indentation, and line breaks
- Use Cases: Perfect for SQL queries, HTML templates, JSON data, configuration files
Examples:
Key Differences: - Regular strings: "text" - interpret escape sequences like \n, \t - Multiline strings: `text` - treat all characters literally, preserve formatting
Instance Methods: .length(), .isEmpty(), .trim(), .ltrim(), .rtrim(), .toLower(), .toUpper(), .contains(), .startsWith(), .endsWith(), .indexOf(), .lastIndexOf(), .replace(), .replaceAll(), .repeat(), .padStart(), .padEnd()
See Strings for comprehensive documentation on string methods.
Booleans
Booleans represent truth values, either true or false.
- Literals:
true,false - Logical Operations:
and,or,not(Python-style with short-circuit evaluation) - No Implicit Conversion: Booleans are not implicitly converted from other types.
- Instance Methods:
.toInt(),.toString()
See Booleans for comprehensive documentation on boolean methods.
Arrays
Arrays are ordered, mutable collections of elements.
- Literals:
[1, 2, 3],["a", "b", "c"],[[1, 2], [3, 4]] - Dynamic Resizing: Arrays can grow or shrink dynamically.
- Mixed Type Support: Arrays can contain elements of different data types.
- Nested Arrays: Arrays can be nested to arbitrary depths.
- Indexing: Zero-based indexing with bounds checking.
- Instance Methods:
.length(),.indexOf(),.contains(),.concat(),.reverse(),.push(),.pop(),.shift(),.unshift(),.clear() - Module: For additional array operations, refer to the
arraysmodule in the Standard Library.
See Arrays for comprehensive documentation on array methods.
Tuples
Tuples are ordered, immutable collections primarily used for multiple return values from functions.
- Creation: Implicitly created by functions that return multiple values
- Destructuring: Can be unpacked into multiple variables
- Heterogeneous: Can contain elements of different types
- Indexing: Zero-based indexing with bounds checking
- Instance Methods:
.length(),.toArray()
See Tuples for comprehensive documentation on tuple methods.
Maps
Maps are key-value data structures that provide efficient lookups.
- Literals:
{"key": "value"},{1: "one", 2: "two"} - Type-safe: Support both typed and untyped maps
- Flexible Keys: Support strings, numbers, booleans, arrays, and structs as keys
- Reference Types: Passed by reference, not by value
- Dot-Access: Convenient
obj.keysyntax alongsideobj["key"] - Instance Methods:
.keys(),.values(),.has(),.contains(),.delete(),.deleteAll(),.putAll(),.entries()
See Maps for comprehensive documentation on map methods.
None
None is a special value that represents the absence of a value, similar to nil in Go or None in Python.
- Usage: Used extensively in error handling to indicate no error.
- Explicit Checking: Requires explicit checking (e.g.,
if err != None).
The unit Type
unit is a proper type whose single value is written and printed as unit.
- Purpose: Represents "no meaningful result" while still participating in the type system.
- Value: A single inhabitant,
unit. - Equality: All
unitvalues are equal to each other, but never equal toNone. - Truthiness:
unitis treated as falsy in conditionals, similar toNone.
Use unit for APIs and functions that conceptually return no data but should still have an explicit return type.
See The unit Type for a deeper discussion and examples.