Call an object method: Difference between revisions

Add Ecstasy example
(Add Ecstasy example)
Line 316:
 
In E, there are no distinguished "static methods". Instead, it is idiomatic to place methods on the maker of the object. This is very similar to methods on constructors in JavaScript, or class methods in Objective-C.
 
=={{header|Ecstasy}}==
Calling a method or function in Ecstasy follows roughly the same conventions as in Java and C#, but in order to prevent certain types of bugs, Ecstasy explicitly disallows calling a function <i>as if it were a method</i>.
<syntaxhighlight lang="java">
module CallMethod
{
/**
* This is a class with a method and a function.
*/
const Example(String text)
{
@Override
String toString() // <-- this is a method
{
return $"This is an example with text={text}";
}
 
static Int oneMoreThan(Int n) // <-- this is a function
{
return n+1;
}
}
 
void run()
{
@Inject Console console;
 
Example example = new Example("hello!");
String methodResult = example.toString(); // <-- call a method
console.println($"Result from calling a method: {methodResult}");
 
// Int funcResult = example.oneMoreThan(12); // <-- compiler error
Int funcResult = Example.oneMoreThan(12); // <-- call a function
console.println($"Results from calling a function: {funcResult}");
}
}
</syntaxhighlight>
 
Output:
<syntaxhighlight>
Result from calling a method: This is an example with text=hello!
Results from calling a function: 13
</syntaxhighlight>
 
=={{header|Elena}}==
The message call:
162

edits