Skip to main content

Getting Started with Flex

This tutorial walks through a complete Flex module so you can see how packages, modules, imports, and declarations fit together. For conceptual background on each piece, see What Is Flex?.

Example Flex File

The Flex code below should be found in the file MyExamplePackage_v1/MyExampleModule.flex.

module Tangram::MyExamplePackage.MyExampleModule

import Tangram::MyHelpfulConstants.ConversionFactors (feet2meters)
import Tangram::MyHelpfulConstants.Approximations as Approx

const pi: float64 = Approx.PI_8PLACES;

// A position using imperial units
message struct PositionUS {
    latitudeDegrees: float64;
    longitudeDegrees: float64;
    altitudeFeet: float32;
}

// A position using metric units
message struct PositionMetric {
    latitudeRadians: float64;
    longitudeRadians: float64;
    altitudeMeters: float32;
}

/* Convert a position that uses imperial units into a position
 * that uses metric units.
 */
transform US_to_Metric(i : PositionUS) -> PositionMetric {
    let degrees2radians = pi / 180.0;
    PositionMetric {
        latitudeRadians = i.latitudeDegrees * degrees2radians;
        longitudeRadians = i.longitudeDegrees * degrees2radians;
        altitudeMeters = i.altitudeFeet * feet2meters;
    };
}

The module begins with a module header, followed by two import statements, and then a list of declarations: the constant pi, the message structs PositionUS and PositionMetric, and the transform US_to_Metric.

Minor Notes
  • Flex supports single line // and multi-line /* */ comments.
  • Identifiers may contain alphanumeric characters and underscores. They must begin with a letter or underscore.
  • Identifiers are case-sensitive.

Note that US_to_Metric appears last in the example since its definition refers to the other three declarations. The order in which declarations appear is unimportant, but it is generally good practice to place a declaration before its uses.

Where to Go Next