Skip to content

Add types representing ranges of numbers #980

@zimri-leisher

Description

@zimri-leisher

FPP should support "refinement types" https://en.wikipedia.org/wiki/Refinement_type

Apparently this has already been done in Haskell, TypeScript, Rust and Scala (although I don't know if they are first-class language features, or just libraries inside of the language).

A good design should start by looking at how these languages implement this feature.

Adding this would implicitly allow parameter and command argument verification. It could also be used in cases such as port arg types, port return types, telemetry types, etc for various use cases, many of which I don't know if we've fully explored.

As a first draft, I propose a syntax/semantics somewhat like:

[ dictionary ] type identifier = type-name where type-refinement

The "identifier" is the name R of the refined type.

"type-refinement" is a type refinement expression. A type refinement expression must evaluate to a Boolean. We could create a "dummy variable" called value or something which is in scope only in the type refinement expression, and nowhere else. The type of the dummy variable would be the type T referred to by type-name. For a given value V of T, V would be a value of R iff the type refinement expression evaluates to True when the dummy variable is assigned value V.

Likely, a common usage pattern would look like this:

SomeComponentParams.fppi:

type ValidTemperatureRange = F32 where value > -120 and value < 150
param HEATER_ON_MIN: ValidTemperatueRange

So these types would often be defined right next to the parameter. Even better, we could allow "anonymous" definitions of these types, so you could say:

param HEATER_ON_MIN: F32 where value > -120 and value < 150

In this case, F32 where value > -120 and value < 150 would be a new nameless type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions