-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_constructor.fun
More file actions
executable file
·45 lines (36 loc) · 1.02 KB
/
class_constructor.fun
File metadata and controls
executable file
·45 lines (36 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* 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 ===
*/