Call an object method: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(12 intermediate revisions by 7 users not shown)
Line 171:
Output from object1: Example of calling an instance method
Output from object1: Example of calling an instance method from an alias</pre>
 
 
=={{header|C}}==
C has structures and it also has function pointers, which allows C structures to be associated with any function with the same signature as the pointer. Thus, C structures can also have object methods.
Line 236 ⟶ 238:
(. 1 (equals 2)) ; alternative style</syntaxhighlight>
=={{header|COBOL}}==
{{Works with|COBOL 2002}}
COBOL has two ways to invoke a method: the <code>INVOKE</code> statement and inline method invocation.
There are two ways to invoke a method: the <code>INVOKE</code> statement and inline method invocation.
<syntaxhighlight lang="cobol">*> INVOKE
<syntaxhighlight lang="cobolfree">*> INVOKE statements.
INVOKE FooClass "someMethod" RETURNING bar *> Factory object
INVOKE foomy-instanceclass "anotherMethodsome-method" RETURNING bar *> InstanceFactory object
USING BY REFERENCE some-parameter
RETURNING foo
INVOKE my-instance "another-method" *> Instance object
USING BY REFERENCE some-parameter
RETURNING foo
 
*> Inline method invocation.
MOVE FooClassmy-class::"someMethodsome-method"(some-parameter) TO bar foo *> Factory object
MOVE foomy-instance::"anotherMethodanother-method"(some-parameter) TO barfoo *> Instance object</syntaxhighlight>
 
To call factory methods of objects of an unknown type (such as when you may have a subclass of the class wanted), it is necessary to get a reference to the class's factory object by calling the <code>"FactoryObject"</code> method.
<syntaxhighlight lang="cobolcobolfree">INVOKE foosome-instance "FactoryObject" RETURNING foo-factory
*> foo-factory can be treated like a normal object reference.
INVOKE foo-factory "someMethod"</syntaxhighlight>
 
=={{header|CoffeeScript}}==
While CoffeeScript does provide a useful class abstraction around its prototype-based inheritance, there aren't any actual classes.
Line 260 ⟶ 268:
foo.instanceMethod() #=> 'Baz'
Foo.staticMethod() #=> 'Bar'</syntaxhighlight>
 
 
=={{header|Common Lisp}}==
In Common Lisp, classmethods are methods that apply to classes, rather than classes that contain methods.
Line 285 ⟶ 295:
Value of x^2: 100
</pre>
 
 
=={{header|D}}==
<syntaxhighlight lang="d">struct Cat {
Line 323 ⟶ 335:
assert(d.dynamicMethod() == "Woof!");
}</syntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
This example shows the creation of a simple integer stack object. The object contains "Push" and "Pop" static methods.
 
<syntaxhighlight lang="Delphi">
 
{Simple stack interface}
 
type TSimpleStack = class(TObject)
private
FStack: array of integer;
protected
public
procedure Push(I: integer);
function Pop(var I: integer): boolean;
constructor Create;
end;
 
 
{ TSimpleStack implementation }
 
constructor TSimpleStack.Create;
{Initialize stack by setting size to zero}
begin
SetLength(FStack,0);
end;
 
function TSimpleStack.Pop(var I: integer): boolean;
{Pop top item off stack into "I" returns False if stack empty}
begin
Result:=Length(FStack)>=1;
if Result then
begin
{Get item from top of stack}
I:=FStack[High(FStack)];
{Delete the top item}
SetLength(FStack,Length(FStack)-1);
end;
end;
 
procedure TSimpleStack.Push(I: integer);
{Push item on stack by adding to end of array}
begin
{Increase stack size by one}
SetLength(FStack,Length(FStack)+1);
{Insert item}
FStack[High(FStack)]:=I;
end;
 
 
procedure ShowStaticMethodCall(Memo: TMemo);
var Stack: TSimpleStack; {Declare stack object}
var I: integer;
begin
{Instanciate stack object}
Stack:=TSimpleStack.Create;
{Push items on stack by calling static method "Push"}
for I:=1 to 10 do Stack.Push(I);
{Call static method "Pop" to retrieve and display stack items}
while Stack.Pop(I) do
begin
Memo.Lines.Add(IntToStr(I));
end;
{release stack memory and delete object}
Stack.Free;
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
10
9
8
7
6
5
4
3
2
1
Elapsed Time: 10.571 ms.
</pre>
 
 
 
=={{header|Dragon}}==
Making an object of class and then calling it.
Line 359 ⟶ 460:
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;
 
Line 407 ⟶ 503:
val func4 = &func3(13); // <-- type: function Int()
console.print($"Calling a fully bound function: {func4()}");
}
}
}
</syntaxhighlight>
 
Line 456 ⟶ 552:
IO.puts(obj |> ObjectCall.concat("Hello ", "World!"))
</syntaxhighlight>
 
=={{header|EMal}}==
{{trans|Wren}}
<syntaxhighlight lang="emal">
type MyClass ^|we are defining a new data type and entering in its static context|^
fun method = void by block do writeLine("static method called") end
model ^|we enter the instance context|^
fun method = void by block do writeLine("instance method called") end
end
type CallAnObjectMethod
var myInstance = MyClass() ^|creating an instance|^
myInstance.method()
MyClass.method()
</syntaxhighlight>
{{out}}
<pre>
instance method called
static method called
</pre>
 
=={{header|Factor}}==
In Factor, there is no distinction between instance and static methods. Methods are contained in generic words and specialize on a class. Generic words define a <i>method combination</i> so methods know which object(s) to dispatch on. (But most methods dispatch on the object at the top of the data stack.) Under this object model, calling a method is no different than calling any other word.
Line 622 ⟶ 738:
Hello static world!
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn FBClassDemo
// Class
ClassRef class = fn MutableStringClass
// Cocoa base class name as human-readable string
print fn StringFromClass( class )
// Instantiate
CFMutableStringRef mutStr = fn MutableStringNew
// Method with single argument
MutableStringSetString( mutStr, @"Hello, World!" )
print mutStr
// Method with multiple arguments
MutableStringReplaceAllOccurrencesOfString( mutStr, @"World", @"Rosetta Code" )
print mutStr
end fn
 
fn FBClassDemo
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
NSMutableString
Hello, World!
Hello, Rosetta Code!
</pre>
 
=={{header|Go}}==
Go distances itself from the word "object" and from many object oriented concepts. It does however have methods. Any user-defined type in Go can have methods and these work very much like "instance methods" of object oriented languages. The examples below illustrate details of Go methods and thus represent the concept of instance methods.
Line 2,083 ⟶ 2,231:
 
What is your name ?</pre>
 
=={{header|V (Vlang)}}==
Vlang uses objects without classes (sometimes known as class-free or classless):
 
1) Structs can have methods assigned to them or be embedded in other structs.
 
2) Vlang also uses modules. Functions and structs can be exported from a module, which works somewhat similar to the class method.
 
<syntaxhighlight lang="Zig">
// Assigning methods to structs
struct HelloWorld {}
 
// Method's in Vlang are functions with special receiver arguments at the front (between fn and method name)
fn (sh HelloWorld) say_hello() {
println("Hello, world!")
}
 
fn (sb HelloWorld) say_bye() {
println("Goodbye, world!")
}
 
fn main() {
 
// instantiate object
hw := HelloWorld{}
// call methods of object
hw.say_hello()
hw.say_bye()
}
</syntaxhighlight>
 
{{out}}
<pre>
Hello, world!
Goodbye, world!
</pre>
 
Exporting functions from modules:
 
Create a module:
<syntaxhighlight lang="Zig">
// myfile.v
module mymodule
 
// Use "pub" to export a function
pub fn say_hi() {
println("hello from mymodule!")
}
</syntaxhighlight>
 
Import and use the module's function in your code:
<syntaxhighlight lang="Zig">
import mymodule
 
fn main() {
mymodule.say_hi()
}
</syntaxhighlight>
 
{{out}}
<pre>
hello from mymodule!
</pre>
 
=={{header|Wren}}==
Note that it's possible in Wren for instance and static methods in the same class to share the same name. This is because static methods are considered to belong to a separate meta-class.
<syntaxhighlight lang="ecmascript"wren>class MyClass {
construct new() {}
method() { System.print("instance method called") }
Line 2,100 ⟶ 2,313:
static method called
</pre>
 
=={{header|XBS}}==
You can call object methods using two types of structures. Classes and Objects.
Line 2,164 ⟶ 2,378:
Zig does not have classes nor objects. Zig's structs, however, can have methods; but they are not special. They are only namespaced functions that can be called with dot syntax.
 
<syntaxhighlight lang="zig">const asserttesting = @import("std").debug.asserttesting;
 
pub const ID = struct {
Line 2,194 ⟶ 2,408:
};
 
assert(person1.// test getAge() ==method 18);call
asserttry testing.expectEqual(ID@as(u7, 18), person1.getAge(person2) == 20);
try testing.expectEqual(@as(u7, 20), ID.getAge(person2));
}</syntaxhighlight>
 
=={{header|zkl}}==
In zkl, a class can be static (there will only exist one instance of the class). A function is always a member of some class but will only be static if it does not refer to instance data.
9,476

edits