-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
```luceescript+trycf | ||
// First, let's create a simple UDF that returns a value | ||
function getName() { | ||
return "John Doe"; | ||
} | ||
// Create a reference to the getName function using ValueRef() | ||
name = ValueRef(getName); | ||
// Example 1: Basic Usage | ||
writeOutput("Direct function call: " & getName() & "<br>"); | ||
writeOutput("Using ValueRef: " & name & "<br>"); | ||
// Example 2: Using ValueRef with a more complex UDF | ||
function calculateTotal(price, quantity) { | ||
return price * quantity; | ||
} | ||
// Create a reference with preset values | ||
fixedCalculation = ValueRef(function() { | ||
return calculateTotal(10, 5); | ||
}); | ||
writeOutput("Fixed calculation result: " & fixedCalculation & "<br>"); | ||
// Example 3: Using ValueRef with a closure | ||
counter = 0; | ||
incrementCounter = ValueRef(function() { | ||
counter++; | ||
return counter; | ||
}); | ||
writeOutput("Counter value: " & incrementCounter & "<br>"); | ||
writeOutput("Counter value: " & incrementCounter & "<br>"); | ||
// Example 4: Using ValueRef in a struct | ||
person = { | ||
firstName: ValueRef(function() { | ||
return "Jane"; | ||
}), | ||
lastName: ValueRef(function() { | ||
return "Smith"; | ||
}), | ||
fullName: ValueRef(function() { | ||
// Note: This would need to be implemented differently in practice | ||
// as the ValueRef doesn't have access to the other references directly | ||
return "Jane Smith"; | ||
}) | ||
}; | ||
writeOutput("Person's full name: " & person.fullName); | ||
``` |