Added class inheritance and constructors. (0.16.0)
This commit is contained in:
parent
34a7968d18
commit
a56a351da6
6 changed files with 238 additions and 5 deletions
45
examples/class_constructor.fun
Executable file
45
examples/class_constructor.fun
Executable file
|
|
@ -0,0 +1,45 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-01
|
||||
*/
|
||||
|
||||
/*
|
||||
* Demonstrates class constructors named _construct(this, ...)
|
||||
* The constructor is called automatically after instance creation,
|
||||
* receiving 'this' and all header parameters as arguments.
|
||||
*/
|
||||
|
||||
print("=== class constructor demo ===")
|
||||
|
||||
class Counter(number start)
|
||||
// default field value (will be overwritten by _construct)
|
||||
value = 0
|
||||
|
||||
// Runs automatically with (this, start)
|
||||
fun _construct(this, s)
|
||||
this.value = s
|
||||
|
||||
fun inc(this)
|
||||
this.value = this.value + 1
|
||||
return this.value
|
||||
|
||||
c = Counter(10)
|
||||
print("initial value=" + to_string(c.value)) // expect 10
|
||||
print("after inc=" + to_string(c.inc())) // expect 11
|
||||
|
||||
print("=== done ===")
|
||||
|
||||
/* Expected output:
|
||||
=== class constructor demo ===
|
||||
initial value=10
|
||||
after inc=11
|
||||
=== done ===
|
||||
*/
|
||||
59
examples/inheritance_demo.fun
Executable file
59
examples/inheritance_demo.fun
Executable file
|
|
@ -0,0 +1,59 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-01
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inheritance demo:
|
||||
* - Child extends Parent
|
||||
* - Parent initializes fields in its constructor
|
||||
* - Child overrides/extends behavior and fields
|
||||
*/
|
||||
|
||||
print("=== inheritance demo ===")
|
||||
|
||||
class Parent(number start)
|
||||
value = 0
|
||||
fun _construct(this, s)
|
||||
this.value = s
|
||||
|
||||
fun describe(this)
|
||||
return "Parent(value=" + to_string(this.value) + ")"
|
||||
|
||||
fun hello(this)
|
||||
return "Hello from Parent()"
|
||||
|
||||
class Child(number start) extends Parent
|
||||
bonus = 5
|
||||
|
||||
fun _construct(this, s)
|
||||
// child's constructor runs after parent's fields were merged
|
||||
this.value = this.value + this.bonus
|
||||
|
||||
fun describe(this)
|
||||
return "Child(value=" + to_string(this.value) + ", bonus=" + to_string(this.bonus) + ")"
|
||||
|
||||
p = Parent(10)
|
||||
print(p.describe()) // expect: Parent(value=10)
|
||||
|
||||
c = Child(10)
|
||||
print(c.describe()) // expect: Child(value=15, bonus=5)
|
||||
print(c.hello())
|
||||
|
||||
print("=== done ===")
|
||||
|
||||
/* Expected output:
|
||||
=== inheritance demo ===
|
||||
Parent(value=10)
|
||||
Child(value=15, bonus=5)
|
||||
Hello from Parent()
|
||||
=== done ===
|
||||
*/
|
||||
2
examples/namespaced_mod.fun
Normal file → Executable file
2
examples/namespaced_mod.fun
Normal file → Executable file
|
|
@ -3,7 +3,7 @@
|
|||
// never be executed, but it shows that it is not wrong.
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
|
|
|
|||
0
examples/threads_demo.fun
Normal file → Executable file
0
examples/threads_demo.fun
Normal file → Executable file
Loading…
Add table
Add a link
Reference in a new issue