Call an object method: Difference between revisions

Content added Content deleted
(Omit Plain English)
(Replace println() with print(); replace output "syntaxhighlight" tag with "pre" tag)
Line 345: Line 345:
Example example = new Example("hello!");
Example example = new Example("hello!");
String methodResult = example.toString(); // <-- call a method
String methodResult = example.toString(); // <-- call a method
console.println($"Result from calling a method: {methodResult.quoted()}");
console.print($"Result from calling a method: {methodResult.quoted()}");


// Int funcResult = example.oneMoreThan(12); // <-- compiler error
// Int funcResult = example.oneMoreThan(12); // <-- compiler error
Int funcResult = Example.oneMoreThan(12); // <-- call a function
Int funcResult = Example.oneMoreThan(12); // <-- call a function
console.println($"Results from calling a function: {funcResult}");
console.print($"Results from calling a function: {funcResult}");


// methods and functions are also objects that can be manipulated;
// methods and functions are also objects that can be manipulated;
Line 355: Line 355:
Method<Example, <>, <String>> method = Example.toString;
Method<Example, <>, <String>> method = Example.toString;
function String() func = method.bindTarget(example);
function String() func = method.bindTarget(example);
console.println($"Calling a bound method: {func().quoted()}");
console.print($"Calling a bound method: {func().quoted()}");


// by default, a method with target T converts to a function taking a T;
// 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)
// Ecstasy refers to this as "Bjarning" (because C++ takes "this" as a param)
val func2 = Example.toString; // <-- type: function String()
val func2 = Example.toString; // <-- type: function String()
console.println($"Calling a Bjarne'd function: {func2(example).quoted()}");
console.print($"Calling a Bjarne'd function: {func2(example).quoted()}");


// the function is just an object, and invocation (and in this case, binding,
// the function is just an object, and invocation (and in this case, binding,
Line 367: Line 367:
val func3 = Example.oneMoreThan; // <-- type: function Int(Int)
val func3 = Example.oneMoreThan; // <-- type: function Int(Int)
val func4 = &func3(13); // <-- type: function Int()
val func4 = &func3(13); // <-- type: function Int()
console.println($"Calling a fully bound function: {func4()}");
console.print($"Calling a fully bound function: {func4()}");
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>


{{out}}
Output:
<pre>
<syntaxhighlight>
Result from calling a method: "This is an example with text=hello!"
Result from calling a method: "This is an example with text=hello!"
Results from calling a function: 13
Results from calling a function: 13
Line 379: Line 379:
Calling a Bjarne'd function: "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
Calling a fully bound function: 14
</pre>
</syntaxhighlight>


=={{header|Elena}}==
=={{header|Elena}}==