spasm icon indicating copy to clipboard operation
spasm copied to clipboard

Functions accepting `SumType!(string, ...)` can't accept `string`

Open denis-sh opened this issue 4 years ago • 1 comments

Starting from probably 0.2.0-beta.6 this code fails to compile:

import spasm.bindings;
import spasm.dom;
import spasm.types;

extern (C) export void _start()
{
	import std.algorithm : move;
	
	auto canvas = document.createElement("canvas").as!HTMLCanvasElement;
	document.querySelector("body").front.appendChild(canvas);
	
	auto context = canvas.getContext("2d").front.trustedGet!CanvasRenderingContext2D.move;
	context.fillStyle("green"); // Causes error
	context.fillText("Hello World!", 10, 10);
}

Compilation error:

source/app.d(13,19): Error: template spasm.bindings.html.CanvasRenderingContext2D.fillStyle cannot deduce function from argument types !()(string), candidates are:
/home/denis/.dub/packages/spasm-0.2.0-beta.7/spasm/source/spasm/bindings/html.d(552,8):        fillStyle()(ref scope SumType!(string, CanvasGradient, CanvasPattern) fillStyle)
/home/denis/.dub/packages/spasm-0.2.0-beta.7/spasm/source/spasm/bindings/html.d(555,8):        fillStyle()()
ldc2 failed with exit code 1.

This workaround is needed to call fillStyle and alike functions:

SumType!(string, CanvasGradient, CanvasPattern) color = "green";
context.fillStyle(color);

denis-sh avatar Apr 30 '20 17:04 denis-sh

I think it happened because I moved everything to scope ref, you can try to remove it from the fillStyle function to see if that helps.

Another option would be to have the binding generator generate overloads in the presence of SumTypes, and do the conversion for you. Although there might be some that have a lot of combinations.

skoppe avatar May 04 '20 21:05 skoppe