google / google/emboss

`requires` on a field cannot reference other fields

Open
#124 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
92
Forks
30
PR merge metrics
No merged PRs in 30d

Description

Suppose I had the following Emboss definition. Such a definition creates a requirement bound on the value of `bar` while also exposing `max_value` virtual field programmatically via `Foo::max_value()`. It allows developers to keep the minimum or maximum bounds defined in one place and referred to elsewhere, including their code.

```
struct Foo:
let max_value = 128
0 [+1] UInt bar
[requires: 0 <= this <= max_value]
```

However, this is currently not possible. According to the [language reference](https://github.com/google/emboss/blob/master/doc/language-reference.md#requires), "for `[requires]` on a field, other fields may not be referenced."

There is a workaround:

```
struct Foo:
[requires: 0 <= bar <= max_value]
let max_value = 128

0 [+1] UInt bar
```

While this accomplishes the same goal, I would say it's not as good as being able to refer to other fields from a `[requires]` on a field itself. If I had multiple of these requirement blocks, it could get pretty unwieldy to have them all at the top instead of closer to where they semantically matter.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.