Call an object method: Difference between revisions

m
no edit summary
mNo edit summary
Line 2,103:
 
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
hello := HelloWorld{}
// call methods of object
hello.say_hello()
hello.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.
291

edits