Skip to content

Const Declarations

Harneet supports Go-like const declarations for creating immutable values. Constants are evaluated at compile time and cannot be reassigned after declaration.

Basic Syntax

Basic Constants
1
2
3
4
const PI = 3.14159
const MaxRetries = 5
const AppName = "Harneet Lang"
const IsDebug = true

Type Inference and Explicit Types

Constants support both type inference and explicit type annotations:

Type Annotations
// Type inference (recommended)
const InferredInt = 42
const InferredFloat = 3.14
const InferredString = "hello"
const InferredBool = true

// Explicit type annotations
const ExplicitInt int = 42
const ExplicitFloat float64 = 3.14
const ExplicitString string = "hello"
const ExplicitBool bool = true

Immutability

Constants are immutable and cannot be reassigned:

Immutability
1
2
3
4
5
6
const ReadOnlyValue = 42

function main() {
    // This will cause a compile-time error
    ReadOnlyValue = 100  // Error: cannot assign to const variable 'ReadOnlyValue'
}

Expression Evaluation

Constants can be initialized with expressions:

Expressions
1
2
3
4
const Sum = 10 + 20 + 30
const Product = 5 * 6
const Concatenated = "Hello" + " " + "World"
const LogicalResult = true and false

Export Rules (Go-like Convention)

Constants follow Go-like export conventions based on naming:

Export Conventions
1
2
3
4
5
6
7
8
// Exported (accessible from other packages)
const ExportedConstant = "This can be accessed from other packages"
const PI = 3.14159
const MaxConnections = 100

// Package-private (not exported)
const privateConstant = "This is package-private"
const debugMode = true

Constants with title-case names (starting with uppercase letter) are exported and can be accessed from other packages.

Usage Examples

Mathematical Constants

Math Constants
1
2
3
4
5
6
7
const PI = 3.14159265359
const E = 2.71828182846
const GoldenRatio = 1.61803398875

function calculateCircleArea(radius float64) float64 {
    return PI * radius * radius
}

Configuration Constants

Config Constants
package main
import fmt
const MaxConnections = 100
const DefaultTimeout = 30
const ServerName = "Harneet Server"
const EnableLogging = true

function getServerConfig() {
    fmt.Printf("Server: %s\n", ServerName)
    fmt.Printf("Max Connections: %d\n", MaxConnections)
    fmt.Printf("Timeout: %d seconds\n", DefaultTimeout)
    fmt.Printf("Logging: %t\n", EnableLogging)
}

String Constants

String Constants
1
2
3
4
5
6
7
8
9
package main
import fmt
const WelcomeMessage = "Welcome to Harneet!"
const ErrorPrefix = "[ERROR]"
const SuccessPrefix = "[SUCCESS]"

function logSuccess(message string) {
    fmt.Printf("%s %s\n", SuccessPrefix, message)
}

Differences from Variables

Feature Variables (var) Constants (const)
Mutability Mutable Immutable
Zero Values Supported Not supported (must have value)
Reassignment Allowed Compile-time error
Scope Block/function scope Block/function scope
Export Rules Same (title-case) Same (title-case)

Best Practices

  1. Use descriptive names: MaxRetries instead of MAX_RETRIES
  2. Group related constants: Keep configuration constants together
  3. Use type inference: Let the compiler infer types when obvious
  4. Follow export conventions: Use title-case for exported constants
  5. Prefer constants for fixed values: Use constants for values that never change

Error Cases

Const Errors
1
2
3
4
5
6
7
8
9
// Error: const declaration must have a value
const MissingValue  // ❌ Error

// Error: cannot assign to const variable
const Value = 42
Value = 100  // ❌ Error

// Error: type mismatch
const TypeMismatch int = "string"  // ❌ Error

Integration with Other Features

Constants work seamlessly with other Harneet features:

  • Functions: Can be used as default parameter values
  • Control Flow: Can be used in if conditions and loop bounds
  • Expressions: Can be used in any expression context
  • Type System: Full type checking and inference support
  • Modules: Export/import following Go-like conventions