Types
This guide covers the data types available in Tx3 and how to use them.
Built-in Types
Integer (Int
)
// Integer literals123-4560
- Signed integer type
- Used for quantities, timestamps, etc.
- Supports basic arithmetic operations
Boolean (Bool
)
// Boolean literalstruefalse
- Logical values
- Used in conditions and flags
- Supports logical operations
Bytes (Bytes
)
// Hex string literals0x"deadbeef"0x"1234"
- Raw byte data
- Used for addresses, hashes, etc.
- Represented as hex strings
String (String
)
// String literals"hello""world"
- Text data
- Used for names, messages, etc.
- UTF-8 encoded
Asset Types
Native Asset (Ada
)
// ADA literalsAda(1) // 1 ADALovelace(500000) // 0.5 ADA
- Native blockchain currency
- Basic unit operations
Custom Assets
// Asset definitionasset MyToken = 0xABCDEF123.MYTOKEN;
// Asset literalsMyToken(100)
- User-defined tokens
- Requires policy ID and name
- Supports basic arithmetic
Custom Types
Records
// Record definitiontype State { lock_until: Int, owner: Bytes, beneficiary: Bytes,}
// Record constructionState { lock_until: 1234567890, owner: 0x"deadbeef", beneficiary: 0x"12345678",}
- Named field collections
- Used for structured data
- Field access via dot notation
Variants
// Variant definitiontype Result { Success: Int, Error: String,}
// Variant constructionResult::Success(42)Result::Error("failed")
- Tagged unions
- Used for alternative values
- Pattern matching support
Type Usage
In Parameters
tx transfer( amount: Int, recipient: Bytes, message: String) { // ... transaction body}
In Data Expressions
// Record constructionState { lock_until: 1234567890, owner: sender, beneficiary: receiver,}
// Variant constructionResult::Success(42)
In Asset Expressions
// ADA amountAda(1000000)
// Custom token amountMyToken(100)
Type Safety
Tx3 provides several type safety features:
-
Static Type Checking
- Types are checked at compile time
- No runtime type errors
- Clear error messages
-
Type Inference
- Types can be inferred from context
- Different semantics for Data Expressions and Asset Expressions
- Reduces type annotations
- Maintains type safety
-
Type Constraints
- Custom validation rules
- Range checks
- Format validation