Stof Docs
Star on GitHubStof WebsiteDiscordContact Us
  • 🚀Welcome
  • â„šī¸Resources & Information
  • Book
    • Data Interface
    • Introduction & Design
    • Host Environments
      • Rust
      • TypeScript
        • Extend Stof
        • Import Statement
    • Data Interchange
      • JSON to Stof
      • Importing and Parsing Data
      • Exporting Data
      • REST and Restructure
    • Configuration Files
    • Schemas
      • Renaming Fields
      • Removing Fields
      • Validation & Transformation
        • Example Access
      • Nested Schemas
    • Orchestration
  • Common Concepts
    • Objects
    • Primitive Types
    • Functions
    • Fields
    • Object Types
    • Imports
    • Error Handling
    • Units
    • Casting/Conversions
  • Reference
    • CLI
      • 🏃Run
      • đŸ§ĒTest
      • 📚CLI Libraries
        • Filesystem Library
        • Time Library
        • HTTP Library
    • Stof Language
    • Libraries
      • Standard Library
      • Array/Vector Library
      • Number Library
      • String Library
      • Object Library
      • Function Library
      • Set Library
      • Map Library
      • Data Library
      • Tuple Library
      • Blob Library
      • Boolean Library
    • đŸĒ§Formats
Powered by GitBook
On this page
  • Casting With 'as' Keyword
  • Function Signature Casting
  • Incompatible Units
  • Common Base Unit

Was this helpful?

  1. Common Concepts

Casting/Conversions

Unit conversions for a number take place when that number is cast to another unit. This is because units are types in Stof, just like an "int" or a "float".

Casting With 'as' Keyword

#[main]
fn conversion() {
    let i = 33m;
    pln(i as cm);
}
> stof run example.stof
3300cm

Function Signature Casting

fn testFunc(i: cm): m {
    pln(i);
    return i;
}

#[main]
fn conversion() {
    let i = 330mm;
    i = self.testFunc(i);
    pln(i);
}
> stof run example.stof
33cm
0.33m

Incompatible Units

Compatible units are units in the same category. For example, any length unit can be cast to any other length unit, but cannot be cast to a temperature.

If operating with or casting to units of an incompatible type, the units become "undefined" and the value is treated as unitless.

#[main]
fn conversion() {
    let i = 33cm;
    i = i as kg;
    
    pln(i.units());
    pln(i);
    
    i = i.removeUnits();
    pln(i);
}
> stof run example.stof
undefined
33undefined
33

Common Base Unit

When units are used in operations with compatible, but different units, Stof will pick a common base unit to present the returned value as. In a lot of cases, this is just the larger of the two units, which feels the most intuitive in practice.

#[main]
fn conversion() {
    pln(10cm + 1000mm + 2m + 440000um - 2000000000nm);
}
> stof run example.stof
1.54m
PreviousUnitsNextCLI

Last updated 4 months ago

Was this helpful?