Socordia
  • Intro
    • The Compiler
  • The Basics
    • Variables
      • Arrays
    • Conditional Flow
    • Loops
    • Functions
    • Data
      • Casting
      • Enumerations
      • Ranges/Aliases
      • Structs
      • Tuples
      • Unit of Measure Types
  • Mixins
  • Accessibility
  • Primitive Datatypes
  • Extended
    • Conditional
    • Inline IL
    • Operator Overloading
    • Unions
    • OOP
      • Classes
      • Interfaces
Powered by GitBook
On this page
  1. The Basics
  2. Data

Unit of Measure Types

A unit of measure type can gives a literal a typed unit. So you can use it to ensure that you are working with the right number unit.

To define a simple unit type:

<unit_def> ::= "unit" <name> "of" <primitiveType> ";"

A simple example for a definition and usage of an unit type:

unit seconds of f32;
unit minutes of f32;

func main() {
    let duration = 12<seconds>;
    
    print("The duration is: " + duration); // prints the duration with the unit measurement type
    print(toMinutes(duration)); // convert the duration to minutes and print with the unit type 
}

func toMinutes(value: unit seconds): unit minutes => value / 60;

You can automaticly convert a unit to another unit by providing conversion rules:

rules for unit seconds
{
    from seconds to minutes = seconds / 60;
    from minutes to seconds = derive from seconds; // automaticly derive conversion rule. In this case the opposite of seconds: minutes * 60
}
PreviousTuplesNextMixins

Last updated 5 months ago