Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Advanced Features

This section covers advanced language features in Y that provide additional flexibility and integration capabilities for complex programming scenarios.

Overview

Y includes several advanced features that go beyond basic programming constructs:

  • Constants - Compile-time constant values that cannot be changed
  • External Declarations - Interface with external libraries and system functions
  • Type Annotations - Explicit type specifications for complex scenarios

Constants

Constants in Y are immutable values known at compile time. They're useful for configuration values, mathematical constants, and other unchanging data:

const PI: f64 = 3.1415;
const MAX_USERS: i64 = 1000;
const DEBUG_MODE: bool = true;

External Declarations

The declare keyword allows you to interface with external functions, typically from C libraries or system functions:

declare printf: (str) -> void;
declare malloc: (i64) -> void;
declare strlen: (str) -> i64;

Type Annotations

While Y has type inference, explicit type annotations provide clarity and are required in certain contexts:

let processor: (str) -> bool = validate_input;
let numbers: &[i64] = &[];
let complex_type: ((i64) -> bool, &[str]) -> i64 = process_data;

Integration Features

These advanced features work together to enable:

  • System Integration - Interfacing with operating system functions
  • Library Integration - Using external C libraries
  • Performance Optimization - Compile-time constants for better optimization
  • Type Safety - Explicit type checking for complex function signatures

Example Usage

// Constants for configuration
const BUFFER_SIZE: i64 = 1024;
const DEFAULT_TIMEOUT: f64 = 30.0;

// External system functions
declare malloc: (i64) -> void;
declare free: (void) -> void;

// Complex type annotations
let data_processor: (str, (str) -> bool) -> &[str] = filter_strings;

fn main(): void {
    // Using constants
    let buffer = create_buffer(BUFFER_SIZE);

    // Using external functions
    malloc(BUFFER_SIZE);

    // Using complex types
    let filtered = data_processor("input", \(s) => is_valid(s));
}

These features enable Y to be both a high-level language with good abstractions and a systems programming language that can integrate closely with existing infrastructure.