Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

It would be Really Convenient to be able to declare primitive constants (floats, ints, strings) so that these are available in cross-language contexts. Right now my project uses a bunch of janky JSON files that declare our constants and we have a Python script that generates code for them, but it would be great to sunset that and have it all done with flatc, and kept within the same flatbuffer definition file where the associated data types are defined.

For example, I was thinking syntax like:

const SOME_CONSTANT_STRING: string = "hello";
const SOME_CONSTANT_FLOAT: float = 1.0;

Ideas? Thoughts?

You must be logged in to vote

Replies: 2 comments

Comment options

This is a good idea. Linking related issues/discussions:

#8439
#6374

I want to argue that this is different to default values in sense that usually const variables represent compile time constant values.

Arguably, one of the best feature Flatbuffer has is support for structs which represent fixed size struct with no vtable, and having compile time const variables would make declaring such structs with arrays much easier and readable.
eg):

struct EmbeddedMsg{
  v: [float:COMPILE_TIME_CONST_SIZE];
}

I want COMPILE_TIME_CONST_SIZE to be guaranteed to be compile time typed variable. I know that most people use enums for this but, kinda want to make it more explicit.

Also, when dealing with structs, sometimes you want to describe more complex relationship between const variables:

const NUMBER_OF_SENSORS: uint8 = 10;
const SAMPLE_FREQ: uint8 = 10;
const DATA_PER_SEC: uint8 = NUMBER_OF_SENSORS  *  SAMPLE_FREQ;

struct SensorData{
  sensor_status: [uint8:NUMBER_OF_SENSORS];
  sensor_data: [float:DATA_PER_SEC];
}

Any opinions? @aardappel

You must be logged in to vote
0 replies
Comment options

I agree having constants be declarable in schemas, usable within schemas, and output in generated code for all languages would be a great feature to have.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
💡
Ideas
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.