This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-4.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit da40f97d85389a53fe62f5fc2e691dd342dd2670
Author: Claus Ibsen <[email protected]>
AuthorDate: Sat Feb 21 21:08:08 2026 +0100

    CAMEL-23047: camel-core - Add val function to simple
---
 .../modules/languages/pages/simple-language.adoc   | 29 +++++++---------------
 .../camel/language/simple/SimpleInitBlockTest.java | 16 ++++++++++++
 2 files changed, 25 insertions(+), 20 deletions(-)

diff --git 
a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
 
b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
index 3d02ba4c3763..2fb5b93ba8d0 100644
--- 
a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
+++ 
b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
@@ -186,7 +186,7 @@ to access Camel functionality and content of the message 
being routed.
 |`stepId` | `String` | Returns the id of the current step the `Exchange` is 
being routed. Returns `null` if there are no steps.
 |`throwException(type,msg)` | `Exception` | Deliberately throws an error. Uses 
`IllegalArgumentException` by default if no type is specified (use fully 
qualified classname).
 |`type:name.field` | `Object` | To refer to a type or field by its fully 
qualified classname. For example: `type:org.apache.camel.Exchange.FILE_NAME`.
-|`val(exp)` | `Object` |
+|`val(exp)` | `Object` | Returns the expression as a constant value.
 |`variable.key` | `Object` | To look up the variable with the given key.
 |`variable.key._OGNL_` | `Object` | To look up the variable with the given key 
and then invoke Camel _OGNL syntax_.
 |`variable[key]` | `Object` | *Deprecated* To look up the variable with the 
given key.
@@ -1179,39 +1179,28 @@ $init{
 
 Notice how the block uses the `$init{` ... `}init$` markers to indicate the 
start and end of the block.
 
-Inside the init block, then you can assign local variables in the syntax `$key 
:= ...` where you can then use simple language to declare
+Inside the init block, then you can assign local variables in the syntax `$key 
:= function` where you can then use simple language function(s) to compute
 the value of the variable.
 
-In the example below the `foo` variable is assigned a constant value of `Hello 
foo`, while `bar` variable is a string value with the dynamic value
-of the message body.
-
 [source,text]
 ----
 $init{
-  $foo := 'Hello foo'
-  $bar := 'Hi from ${body}'
+  $foo := ${upper('Hello $body}'}
+  $bar := ${header.code > 999 ? 'Gold' : 'Silver'}
 }init$
 ----
 
-You can have Java style code comments in the init block using `// comment 
here` as follows:
+IMPORTANT: The left hand side must be a function - you cannot assign a 
hardcoded literal value. Use the `val` function for this, such as `${val(123)}` 
to use `123` as the value.
 
-[source,text]
-----
-$init{
-  // foo is a beginner phrase
-  $foo := 'Hello foo'
-
-  // here we can inline functions to get the name of the user from the message 
body
-  $bar := 'Hi from ${body}'
-}init$
-----
-
-Yes you can use the full power of the simple language and all its functions, 
such as:
+You can have Java style code comments in the init block using `// comment 
here` as follows:
 
 [source,text]
 ----
 $init{
+  // say hello to my friend
   $foo := ${upper('Hello $body}'}
+
+  // either gold or silver
   $bar := ${header.code > 999 ? 'Gold' : 'Silver'}
 }init$
 ----
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleInitBlockTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleInitBlockTest.java
index cae5757d4afd..65b0d2d06cb3 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleInitBlockTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleInitBlockTest.java
@@ -145,6 +145,22 @@ public class SimpleInitBlockTest extends 
LanguageTestSupport {
         assertEquals("average: 5\n", s);
     }
 
+    @Test
+    public void testInitBlockConstant() {
+        String exp = """
+                $init{
+                  $bar := ${val(Hi from ${body})}
+                }init$
+                $bar
+                """;
+
+        exchange.getMessage().setBody("Camel");
+
+        Expression expression = 
context.resolveLanguage("simple").createExpression(exp);
+        String s = expression.evaluate(exchange, String.class);
+        assertEquals("Hi from Camel\n", s);
+    }
+
     @Override
     protected String getLanguageName() {
         return "simple";

Reply via email to