Call an object method: Difference between revisions

update example
(Add Ecstasy example)
(update example)
Line 345:
Example example = new Example("hello!");
String methodResult = example.toString(); // <-- call a method
console.println($"Result from calling a method: {methodResult.quoted()}");
 
// Int funcResult = example.oneMoreThan(12); // <-- compiler error
Int funcResult = Example.oneMoreThan(12); // <-- call a function
console.println($"Results from calling a function: {funcResult}");
 
// methods and functions are also objects that can be manipulated;
// note that "function String()" === "Function<<>, <String>>"
Method<Example, <>, <String>> method = Example.toString;
function String() func = method.bindTarget(example);
console.println($"Calling a bound method: {func().quoted()}");
 
// by default, a method with target T converts to a function taking a T;
// Ecstasy refers to this as "Bjarning" (because C++ takes "this" as a param)
val func2 = Example.toString; // <-- type: function String()
console.println($"Calling a Bjarne'd function: {func2(example).quoted()}");
 
// the function is just an object, and invocation (and in this case, binding,
// as indicated by the '&' operator which requests a reference) is accomplished
// using the "()" operator
val func3 = Example.oneMoreThan; // <-- type: function Int(Int)
val func4 = &func3(13); // <-- type: function Int()
console.println($"Calling a fully bound function: {func4()}");
}
}
Line 356 ⟶ 374:
Output:
<syntaxhighlight>
Result from calling a method: "This is an example with text=hello!"
Results from calling a function: 13
Calling a bound method: "This is an example with text=hello!"
Calling a Bjarne'd function: "This is an example with text=hello!"
Calling a fully bound function: 14
</syntaxhighlight>
 
162

edits