Call an object method: Difference between revisions

Replace println() with print(); replace output "syntaxhighlight" tag with "pre" tag
(Omit Plain English)
(Replace println() with print(); replace output "syntaxhighlight" tag with "pre" tag)
Line 345:
Example example = new Example("hello!");
String methodResult = example.toString(); // <-- call a method
console.printlnprint($"Result from calling a method: {methodResult.quoted()}");
 
// Int funcResult = example.oneMoreThan(12); // <-- compiler error
Int funcResult = Example.oneMoreThan(12); // <-- call a function
console.printlnprint($"Results from calling a function: {funcResult}");
 
// methods and functions are also objects that can be manipulated;
Line 355:
Method<Example, <>, <String>> method = Example.toString;
function String() func = method.bindTarget(example);
console.printlnprint($"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.printlnprint($"Calling a Bjarne'd function: {func2(example).quoted()}");
 
// the function is just an object, and invocation (and in this case, binding,
Line 367:
val func3 = Example.oneMoreThan; // <-- type: function Int(Int)
val func4 = &func3(13); // <-- type: function Int()
console.printlnprint($"Calling a fully bound function: {func4()}");
}
}
</syntaxhighlight>
 
{{out}}
Output:
<pre>
<syntaxhighlight>
Result from calling a method: "This is an example with text=hello!"
Results from calling a function: 13
Line 379:
Calling a Bjarne'd function: "This is an example with text=hello!"
Calling a fully bound function: 14
</pre>
</syntaxhighlight>
 
=={{header|Elena}}==
162

edits