Skip to content

Commit 362f556

Browse files
Array<T>: count and rest constructors (#53)
`new Array<number>(10)` makes ten elements and `new Array(10, 20, 30)` those three, beside `new Array([..])`. Needs the compiler's constructor-overload inference (#715) and optional-parameter overload fit (#716). Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 200e4ad commit 362f556

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

‎src/generics/lib.generics.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -449,14 +449,14 @@ class Array<T> {
449449
constructor(private data: T[] = []) {
450450
}
451451

452-
// constructor(count: int = 0) {
453-
// this.data = [];
454-
// this.data.length = count;
455-
// }
456-
457-
// constructor(...args: T[]) {
458-
// this.data = args;
459-
// }
452+
constructor(count: int = 0) {
453+
this.data = [];
454+
this.data.length = count;
455+
}
456+
457+
constructor(...args: T[]) {
458+
this.data = args;
459+
}
460460

461461
public [index: int]: T;
462462

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function main() {
2+
3+
const a = new Array<number>(10);
4+
5+
assert(a.length === 10 && a[0] === 0);
6+
7+
const b = new Array(10, 20, 30);
8+
9+
assert(b.length === 3 && b[0] === 10 && b[1] === 20 && b[2] === 30);
10+
11+
console.log("ALL DONE");
12+
}

0 commit comments

Comments
 (0)