I have several types defined and two are organized into list (field and option). Functions are defined to help define entries (addfield and addoption). I'm able to define fields and works as expected; however, for options I get an error when the list has more than one element.
Here is the full dhall file:
let FTypes = < U8 | U16 | U32 | F32 | BITS >
let Translate = List { mapKey : Text, mapValue : Text }
let Bits = < Bit0 | Bit1 | Bit2 | Bit3 | Bit4 | Bit5 | Bit6 | Bit7 >
let bits2Natural
: Bits → Natural
= λ(b : Bits) →
merge
{ Bit0 = 0
, Bit1 = 1
, Bit2 = 2
, Bit3 = 3
, Bit4 = 4
, Bit5 = 5
, Bit6 = 6
, Bit7 = 7
}
b
let Field =
let TypeBits = { bit : Natural, width : Natural }
in { Type =
{ startbyte : Natural
, type : FTypes
, translate : Optional Translate
, typebits : Optional TypeBits
}
, default = { translate = None Translate, typebits = None TypeBits }
}
let FieldList = List { mapKey : Text, mapValue : Field.Type }
let Option = { dlc : Natural, fields : FieldList, telemjson : Optional Text }
let OptionList = List { mapKey : Text, mapValue : Option }
let addfield = λ(n : Text) → λ(f : Field.Type) → { mapKey = n, mapValue = f }
let addoption = λ(k : Text) → λ(v : Option) → { mapKey = k, mapValue = v }
let testoption : OptionList =
[ addoption "0"
{ dlc = 4
, fields =
[ addfield "field0" Field::{startbyte = 1, type = FTypes.U16 }
, addfield "field1" Field::{startbyte = 3, type = FTypes.U8 }
]
, telemjson = Some
( "{\"sensor1\":{"
++ "\"time\":\"@(timestamp)\","
++ "\"id\":\"@(option)\","
++ "\"temp\":@(field0.value),"
++ "\"unit\":\"@(field1.value)\""
++ "}}"
)
}
, addoption "1"
{ dlc = 2
, fields = [ addfield "field0" Field::{startbyte = 1, type FTypes.U8 } ]
, telemjson = Some
("{\"sensor2\":{\"value\":@(field1.value)}}")
}
]
in testoption
And here is the error output:
Error: Invalid input
trial2.dhall:60:9:
|
60 | { dlc = 2
| ^
unexpected '{'
expecting ',', ->, :, ], or whitespace
If I remove the second option (lines 59-64), it will work?
What am I doing wrong? How can I define multiple Option
for an OptionList
.
The parse error could use improvement, but there is a typo in the record starting on line 60 due to a missing
=
sign here:It should be:
… and if you make that change then the file successfully parses and type-checks.