Open fsareshwala opened 2 years ago
I ran into a similar use case where I wanted to use an enum as an integral constant. A slightly reduced example:
enum FrameType:
-- RFCOMM Frame Type
[maximum_bits: 8]
UIH = 0xEF
bits Control:
0 [+8] UInt control_byte
0 [+4] UInt frame_type_1
4 [+1] UInt p_f
5 [+3] UInt frame_type_2
# Note: emboss doesn't provide a way to cast an enum to an integral constant
# so we hardcode a value instead, also bit shift doesn't work but that's solvable
# with multiplication.
# control_byte == (FrameType.UIH | (1 << 4))
# or
# let flow_control_enabled = FrameType.UIH + 16
# control_byte == flow_control_enabled
let flow_control = control_byte == 0xFF
In the text below, we consider the following Emboss definition:
We would like to pass constant values to the
group
andcommand
parameters of theOpCode
type. We could store these values in virtual fields within the struct we are composing using thelet
keyword. However, these values are better placed inside theOpCodeGroup
andOpCodeCommand
enumerations, respectively. Doing so allows the values to be defined once across the entire codebase and made accessible within both Emboss and C++ code. The following situations arise here:UInt:6
andUInt:10
types for parameters, the arguments must be hardcoded, and we no longer have a single definition of the constants across the codebase (they have to be defined elsewhere again)EnumView
which doesn’t allow writes, makingogf
andocf
read onlyUInt
types for the fields, adding a check like[requires: ogf == group && ocf == command]
won’t compile because the types are differentFor obvious reasons, we don’t want to use option 1 to hardcode constants. Enumerations shouldn’t be writable so option 2 makes sense as it is. In order to support this use case, Emboss should support cross-type integral equality as implied in option 2.
Potential Implementation The
.emb
language syntax is updated to allow explicitint(Foo.Bar)
conversions to allow for integral type coercion. We want to keep Emboss' strict type safety but want to be able to compare across only integral types.