[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] doc/poke.texi: Add example for struct field initializers
From: |
Mohammad-Reza Nabipoor |
Subject: |
[PATCH] doc/poke.texi: Add example for struct field initializers |
Date: |
Mon, 31 Jan 2022 21:03:42 +0330 |
2022-01-31 apache2 <?@?.?>
* doc/poke.texi (Field Initializers): Add more explanation and
an example.
---
Hi, Jose.
I'm sending this patch, on behalf of apache2.
Thanks
ChangeLog | 5 +++++
doc/poke.texi | 22 +++++++++++++++++++++-
2 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index d30a569d..fafc399a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2022-01-31 apache2 <?@?.?>
+
+ * doc/poke.texi (Field Initializers): Add more explanation and
+ an example.
+
2022-01-30 Bruno Haible <bruno@clisp.org>
Update the pager automatically when the window height changes.
diff --git a/doc/poke.texi b/doc/poke.texi
index 19660b95..82400126 100644
--- a/doc/poke.texi
+++ b/doc/poke.texi
@@ -10863,7 +10863,10 @@ type Packet =
@};
@end example
-Note how the syntax is different than the one used for constraints.
+Note how the syntax is different than the one used for constraints:
+A single equals sign initializes without constraints,
+instead of the double equals used for initialization with
+an implicit constraint (see below).
When a field in a struct type has an initializer, the struct
constructor will use the initializer expression as the initial value
for the field. For example:
@@ -10927,6 +10930,23 @@ type Packet =
It is considered good practice to design struct types in a way that a
constructor with no arguments will result in something usable.
+Note also that field initializer are full expressions that can refer to
+other variables in scope, including previous field names, and call functions:
+@example
+(poke) type Packet =
+ struct
+ @{
+ byte marker == 0xff;
+ byte prioritized_length; // most important bits!
+ byte length = prioritized_length : length >= prioritized_length;
+ string printable_length = format ("%i16x", length);
+ byte[length] payload;
+ @}
+
+(poke) Packet @{prioritized_length=5@}
+Packet
@{marker=255UB,prioritized_length=5UB,length=5UB,printable_length="0005",payload=[0UB,0UB,0UB,0UB,0UB]@}
+@end example
+
@node Field Labels
@subsection Field Labels
--
2.34.1
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [PATCH] doc/poke.texi: Add example for struct field initializers,
Mohammad-Reza Nabipoor <=