Skip to content

Inheritance support for Datex classes #131

@jonasstrehle

Description

@jonasstrehle

Structs / classes do not work with inheritance combined with Storage collections.

import { Datex, property } from "unyt_core/datex.ts";
import { inferType, StorageSet } from 'unyt_core/datex_all.ts';

const C = 0;

{ // Classes
	@sync class A {
		@property a!: number;
		construct() {
			this.a = 42;
		}
	}
	@sync class B extends A {
		@property b!: number;
		construct() {
			super.construct();
			this.b = 69;
		}
	}
	const list = eternalVar("tmp1-"+C) ?? $$(new StorageSet<A>());
	if (await list.getSize() === 0) {
		await list.add(new A());
		await list.add(new B());
	}
	for await (const entry of list) {
		console.log(entry.a, entry.b)
	}
	console.log("")
}

{ // Structs
	const A = struct(
		class A {
			@property a!: number;
			construct() {
				this.a = 42;
			}
		}
	)
	type A = inferType<typeof A>;
	
	const B = struct(
		class B extends A {
			@property b!: number;
			construct() {
				super.construct();
				this.b = 69;
			}
		}
	)
	type B = inferType<typeof B>;
	const list = eternalVar("tmp2-"+C) ?? $$(new StorageSet<A>());
	if (await list.getSize() === 0) {
		await list.add(new A());
		await list.add(new B());
	}
	for await (const entry of list) {
		console.log(entry.a, entry.b)
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions