Support cross-type integral equality
- Dominant language
- Python
- Stars
- 92
- Forks
- 30
- PR merge metrics
- No merged PRs in 30d
Description
In the text below, we consider the following Emboss definition:
```
enum OpCodeGroup:
# ...
FOO = 0x08
# ...
enum OpCodeCommand:
# ...
BAR = 0x0039
# ...
bits OpCode(group: UInt:6, command: UInt:10):
0 [+6] UInt ogf
6 [+10] UInt ocf
```
We would like to pass constant values to the `group` and `command` parameters of the `OpCode` type. We could store these values in virtual fields within the struct we are composing using the `let` keyword. However, these values are better placed inside the `OpCodeGroup` and `OpCodeCommand` 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:
1. If we use the `UInt:6` and `UInt: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)
2. If we use the enumeration types for both parameters and fields, Emboss generates an `EnumView` which doesn’t allow writes, making `ogf` and `ocf` read only
3. If we use the enumeration types for the parameters and `UInt` types for the fields, adding a check like `[requires: ogf == group && ocf == command]` won’t compile because the types are different
For 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 explicit `int(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.
Contributor guide
Assessment
This issue has not been assessed yet.