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.
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]typeidentifier=type-namewheretype-refinementThe "identifier" is the name
Rof 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
valueor something which is in scope only in the type refinement expression, and nowhere else. The type of the dummy variable would be the typeTreferred to by type-name. For a given valueVofT,Vwould be a value ofRiff the type refinement expression evaluates toTruewhen the dummy variable is assigned valueV.Likely, a common usage pattern would look like this:
SomeComponentParams.fppi:
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:
In this case,
F32 where value > -120 and value < 150would be a new nameless type.