-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #412 from ccrma/2023-ctors
support for constructors and destructors
- Loading branch information
Showing
46 changed files
with
3,012 additions
and
1,534 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// specifying and overloading class constructors | ||
// requires chuck-1.5.2.0 or higher | ||
|
||
// a class | ||
class Foo | ||
{ | ||
// a member variable (this will be initialized before | ||
// any actual constructors, as Foo's "pre-constructor") | ||
1 => int num; | ||
|
||
// constructor 1 "default" | ||
fun @construct() | ||
{ | ||
13 => num; // set num to something | ||
<<< "constructor 1:", num >>>; | ||
} | ||
|
||
// constructor 2 | ||
fun @construct( int x ) | ||
{ | ||
x => num; | ||
<<< "constructor 2:", x >>>; | ||
} | ||
|
||
// constructor 3 (alternate method to define) | ||
fun void Foo( int x, int y ) | ||
{ | ||
x*y => num; | ||
<<< "constructor 3:", x, y >>>; | ||
} | ||
} | ||
|
||
// declare a Foo, invoke constructor 1 | ||
Foo f0; | ||
// declare a Foo, invoke constructor 1 | ||
Foo f1(); | ||
// declare a Foo, invoke constructor 2 | ||
Foo f2(15); | ||
// instantiate a Foo, invoke constructor 3 | ||
new Foo(8,9) @=> Foo @ f3; | ||
|
||
<<< f0.num, f1.num, f2.num, f3.num >>>; | ||
|
||
// can also invoke constructor for each element in array | ||
Foo array1(2)[3]; | ||
// print each element's num | ||
for( auto f : array1 ) <<< f.num, "" >>>; | ||
|
||
// instantiate an array of Foo, invoking constructor for each | ||
new Foo(6,5)[4] @=> Foo array2[]; | ||
// print each element's num | ||
for( auto f : array2 ) <<< f.num, "" >>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// example: defining basic class constructors + destructor | ||
|
||
// a class | ||
class Foo | ||
{ | ||
// a member variable | ||
1 => int num; | ||
|
||
// constructor "default" | ||
fun @construct() { 2 => num; } | ||
|
||
// another constructor | ||
fun @construct( int x ) { x => num; } | ||
|
||
// yet another constructor | ||
fun @construct( int x, int y ) { x*y => num; } | ||
|
||
// alternate way of define a constructor | ||
fun void Foo( int x, int y, int z ) { x*y*z => num; } | ||
|
||
// destructor | ||
fun @destruct() { <<< "destructor:", this.num >>>; } | ||
} | ||
|
||
// constructor "default" | ||
Foo f0; | ||
// constructor "default" | ||
Foo f1(); | ||
// another constructor | ||
Foo f2(15); | ||
// yet another constructor | ||
new Foo(8,9) @=> Foo @ f3; | ||
// yet another constructor | ||
new Foo(10,11,12) @=> Foo @ f4; | ||
<<< f0.num, f1.num, f2.num, f3.num, f4.num >>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// example of class destructor | ||
|
||
// a class | ||
class Foo | ||
{ | ||
// class destructor | ||
fun @destruct() | ||
{ | ||
<<< "destructor called...", "" >>>; | ||
} | ||
} | ||
|
||
Foo foo; | ||
// when `foo` goes out of scope, the destructor should be called... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
// i++ - doesn't fully work yet (1.2.0.1) | ||
// post increment operator | ||
|
||
// starting value | ||
4 => int i; | ||
// assign, then increment i | ||
i++ => int j; | ||
|
||
<<<"printing i, then j">>>; | ||
<<<i>>>; | ||
<<<j>>>; | ||
if ( i == 5 && j == 4) <<<"success">>>; | ||
// print (i should be 1 higher than j) | ||
<<< i >>>; | ||
<<< j >>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.