# DSDL v1.1: Introduce the concept of default field initialization and allow customizing this behavior per field

**URL:** <https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802>\
**Category:** Specification\
**Created:** [December 10, 2022, 6:59pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802 "2022-12-10T18:59:13Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [December 10, 2022, 6:59pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/1 "2022-12-10T18:59:13Z")

</div>

Currently, the DSDL specification does not dedicate much attention to the default value initialization of DSDL fields. I suggest amending it by Cyphal v1.1 as follows:

- Define and require zero-initialization of _primitive-typed_ DSDL fields.

- Extend the DSDL syntax to allow overriding the default initializer for _primitive-typed fields_ as follows:

```python
float32 PI = 3.14 # A regular constant.
float32 default_zero # A regular field, the default initializer is zero.
float32 default_custom <- 42.0 # A regular field with a custom non-zero default initializer.
float32 default_2pi <- PI * 2 # Ditto, with an expression.
float32[3] array # Default initializers for non-primitive types may be introduced later.

```

---

<div class="post-metadata">

**Author:** ![scottdixon](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/scottdixon/32/39_2.png) [@scottdixon](https://forum.opencyphal.org/u/scottdixon)\
**Post date:** [December 12, 2022, 9:03pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/2 "2022-12-12T21:03:51Z")

</div>

How do default values relate to the message version? Do we consider a change in the default value to be a change in the semantics?

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [December 13, 2022, 8:52am UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/3 "2022-12-13T08:52:15Z")

</div>

It would appear to be in line with the existing DSDL versioning policy to require at least a minor version bump upon a default value change. As for the greater question of wire compatibility, the current edition already delegates this matter to the system integrator, and the introduction of custom default initialization, as I understand it, is not going to affect it.

 ![image](https://forum.opencyphal.org/uploads/default/original/2X/c/c3e707c72dc2f57becc0cb98e23db721870e379a.png)

---

<div class="post-metadata">

**Author:** ![scottdixon](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/scottdixon/32/39_2.png) [@scottdixon](https://forum.opencyphal.org/u/scottdixon)\
**Post date:** [December 13, 2022, 5:59pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/4 "2022-12-13T17:59:38Z")

</div>

What’s the precedent for using `<-` as an assignment operator? Is this borrowed from another language?

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [December 13, 2022, 6:13pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/5 "2022-12-13T18:13:14Z")

</div>

> [@scottdixon](#):
>
> Is this borrowed from another language?

Yes, from R.

Usage of `=` is impossible because it is already used for constant definition, and in the strict sense it would not be correct because this symbol is meant to represent equality, not assignment. The well-known walrus form `:=` is risky due to its visual similarity to `=`.

I do not have strong feelings about `<-` though and if there are other ideas, please share.

---

<div class="post-metadata">

**Author:** ![scottdixon](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/scottdixon/32/39_2.png) [@scottdixon](https://forum.opencyphal.org/u/scottdixon)\
**Post date:** [December 13, 2022, 7:00pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/6 "2022-12-13T19:00:55Z")

</div>

I am concerned with the syntax here. Currently, constants are easy to find because they are the only statements with values. Adding defaults muddles this clarity. The C way to do this is to use a keyword “static” or “constexpr” for c++ but we don’t want to break all DSDL by requiring a constant keyword. I’m wondering if using the more draconian C++ initializer syntax would help make this feature visually distinct?

```auto
float32 PI = 3.14
float32 default_2pi { PI * 2 }

```

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [December 13, 2022, 7:12pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/7 "2022-12-13T19:12:16Z")

</div>

The C++ syntax is workable but I find braces a little off-putting. Should we not explore the possibility of adding a `const` qualifier in a backward-compatible way? One idea (not very thought out) is to pick some assignment operator (`<-` or `:=`) and define an alternative syntax for constant definition with a `const ` prefix. The assignment operator would only be legal to use in definitions where constants are annotated with a `const` keyword.

Legal:

```python
const float32 PI = 3.14 # New keyword
float32 angle <- PI * 0.5

```

Also legal (existing definitions):

```python
float32 PI = 3.14 # Deprecated but legal
float32 angle # Can't use assignment here

```

Not legal:

```python
float32 PI = 3.14
float32 angle <- PI * 0.5 # No way! Either remove the assignment or add const above.

```

---

<div class="post-metadata">

**Author:** ![scottdixon](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/scottdixon/32/39_2.png) [@scottdixon](https://forum.opencyphal.org/u/scottdixon)\
**Post date:** [December 14, 2022, 4:58pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/8 "2022-12-14T16:58:06Z")

</div>

Before I continue to debate the colour of the syntax shed, what about correctness and consistency of the default values. Do the current rules for rational numbers, as they relate to constants, ensure consistent representations of default values across all platforms? I think they do but I’m wondering if you’ve thought about this at all?

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [December 14, 2022, 5:24pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/9 "2022-12-14T17:24:27Z")

</div>

> [@scottdixon](#):
>
> Do the current rules for rational numbers, as they relate to constants, ensure consistent representations of default values across all platforms? I think they do but I’m wondering if you’ve thought about this at all?

There is nothing special to default values in this regard, they are to behave exactly like constant initialization expressions: within the DSDL context all computations are exact (no information loss); what happens afterward is implementation-defined. A DSDL front-end can produce a pre-computed rational number for each such initialization expression but whether it incurs information loss during compilation for the target platform is entirely out of the scope of DSDL.

Example:

```auto
float32 PI = 3.141592653589793
# PI is represented internally as 884279719003555/281474976710656.

```

One possible way to generate code:

```c
inline constexpr float PI = 884279719003555.0F / 281474976710656.0F;

```

The resulting value obtained in an IEEE754-compliant platform where `float` is `binary32` is exactly 3.1415927, which is off by 4.641020678874952e-08 from the original constant.

The above reasoning applies entirely to default initializers as well; there are no new behaviors to define, as far as I see.

---

<div class="post-metadata">

**Author:** ![scottdixon](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/scottdixon/32/39_2.png) [@scottdixon](https://forum.opencyphal.org/u/scottdixon)\
**Post date:** [December 14, 2022, 7:00pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/10 "2022-12-14T19:00:27Z")

</div>

Then I think the new keyword strategy you propose is acceptable but `const` is a terrible keyword given its ambiguity in the C++ programming language. What about `local` to stress that the value is never transmitted on the wire?

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [December 14, 2022, 7:14pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/11 "2022-12-14T19:14:57Z")

</div>

> [@scottdixon](#):
>
> `const` is a terrible keyword given its ambiguity in the C++ programming language.

This is not C++ we’re talking about, so why does it concern us? The value is a constant that is known to all users of the data type, so it makes sense to call it exactly that.

---

<div class="post-metadata">

**Author:** ![scottdixon](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/scottdixon/32/39_2.png) [@scottdixon](https://forum.opencyphal.org/u/scottdixon)\
**Post date:** [December 14, 2022, 7:17pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/12 "2022-12-14T19:17:23Z")

</div>

because `const` is guilty by association. How about `constant`?

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [December 14, 2022, 7:21pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/13 "2022-12-14T19:21:59Z")

</div>

Not sure I understand why the association matters but I am okay with `constant` as well.

---

<div class="post-metadata">

**Author:** ![skeets](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/skeets/32/1716_2.png) [@skeets](https://forum.opencyphal.org/u/skeets)\
**Post date:** [December 15, 2022, 8:51pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/14 "2022-12-15T20:51:08Z")

</div>

Not opposed to `<-` syntax but would just using the word `default` be clearer?

```auto
uint32 foo default 500

```

What about a more generalized “attribute” syntax that could expand to specify additional things in the future?

```auto
uint32 bar { default:7, min:2, max:100 }

```

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [December 15, 2022, 9:03pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/15 "2022-12-15T21:03:58Z")

</div>

> [@skeets](#):
>
> would just using the word `default` be clearer?

Probably not, this is not Ada.

> [@skeets](#):
>
> What about a more generalized “attribute” syntax that could expand to specify additional things in the future?
> 
> ```auto
> uint32 bar { default:7, min:2, max:100 }
> 
> ```

Future-proofness is nice to have, but this specific approach might be in conflict with the set notation adopted in DSDL.

---

<div class="post-metadata">

**Author:** ![skeets](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/skeets/32/1716_2.png) [@skeets](https://forum.opencyphal.org/u/skeets)\
**Post date:** [December 15, 2022, 10:03pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/16 "2022-12-15T22:03:43Z")

</div>

Is it the attribute idea or just the syntax that you object to?  
There could be other ways to do the syntax:

```auto
uint32 bar (default=7, min=2, max=100)

```

---

<div class="post-metadata">

**Author:** ![skeets](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/skeets/32/1716_2.png) [@skeets](https://forum.opencyphal.org/u/skeets)\
**Post date:** [December 15, 2022, 10:06pm UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/17 "2022-12-15T22:06:52Z")

</div>

Brainstorming…

```auto
uint32 bar
bar.default=7
bar.min=2
bar.max=100

```

(That kind of makes the case where you just want to specify the default kind of klunky though)

---

<div class="post-metadata">

**Author:** ![scottdixon](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/scottdixon/32/39_2.png) [@scottdixon](https://forum.opencyphal.org/u/scottdixon)\
**Post date:** [December 16, 2022, 12:13am UTC](https://forum.opencyphal.org/t/dsdl-v1-1-introduce-the-concept-of-default-field-initialization-and-allow-customizing-this-behavior-per-field/1802/18 "2022-12-16T00:13:58Z")

</div>

huh. min, max, and default as a single attribute of a field is compelling…
