assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

Automatically create hidden classes for interfaces

Open t57ser opened this issue 7 years ago • 5 comments

Let's take the following example:

export class I32Bounds {
	public left: i32;
	public top: i32;
	public width: i32;
	public height: i32;
	constructor() {}
}

export function createI32Bounds(bounds: i32[]): I32Bounds {
	let i32Bounds: I32Bounds = {
		left: bounds[0],
		top: bounds[1],
		width: bounds[2],
		height: bounds[3],
	};
	return i32Bounds;
}

Using this style gives a comfortable way to work with objects like in JS. But it gets a bit more complex when you have nested objects. (one class per obj) What would be amazing is if AS could create the required classes from a provided interface.

export interface I32Bounds {
	left: i32;
	top: i32;
	width: i32;
	height: i32;
	nest: { a: i32 }
}

export function createI32Bounds(bounds: i32[]): I32Bounds {
	let i32Bounds: I32Bounds = {
		left: bounds[0],
		top: bounds[1],
		width: bounds[2],
		height: bounds[3],
		nest: { a: 12 }
	};
	return i32Bounds;
}

This would make working with objects a lot more comfortable. If serialization would be possible on this (https://github.com/AssemblyScript/assemblyscript/issues/292) One could easily create objects within AS and pass them as a string to JS.

t57ser avatar Oct 08 '18 12:10 t57ser

There is some initial support for object literals here: https://github.com/AssemblyScript/assemblyscript/blob/master/src/compiler.ts#L6510

Still some TODO comments there, though, especially when constructors are involved. Can think about interfaces again once there is some initial support in general, which is currently blocked by GC/runtime.

dcodeIO avatar Oct 09 '18 00:10 dcodeIO

So interfaces are totally unimplemented, or just interface literals like the one illustrated here? How about in e.g. arguments:

function foo({bar, baz}: {bar: i32, baz: string})

I might need to step away from my TypeScript project to fix this. I really need features like statically resolved union types and interface literals, and I need my TypeScript to compile to WASM.

sqykly avatar Oct 29 '18 10:10 sqykly

It is actually trivial to implement these specific examples. Generate a random unique identifier for the interface literal, hoist it to the global scope, replace the interface literal inside the instance or argument with the random type name. There are vanilla JS interpreters and JITs that do this for all object literals that aren't class instances, so this could easily be applied to support all object literals as well. If I write this out, will you pull it?

The only problematic cases, I believe, would be providing object literals to arguments that have a bunch of overloads.

sqykly avatar Oct 29 '18 11:10 sqykly

Is this still planned?

mariusa avatar Nov 15 '22 13:11 mariusa

@mariusa, I'm really not sure. We discussed it when there was the Working Group a year or so back, but I think its on the back burner for now

JairusSW avatar Dec 07 '22 05:12 JairusSW