Treating the abusive unsigned syndrome

Michel Fortin michel.fortin at michelf.com
Fri Nov 28 04:43:47 PST 2008


On 2008-11-27 22:34:50 -0500, Andrei Alexandrescu 
<SeeWebsiteForEmail at erdani.org> said:

> One question I had is: say polysemy will be at work for integral 
> arithmetic. Should we provide means in the language for user-defined 
> polysemous functions? Or is it ok to leave it as compiler magic that 
> saves redundant casts?

I think that'd be a must. Otherwise how would you define your own 
arithmetical types so they work like the built-in ones?

	struct ArbitraryPrecisionInt { ... }

	ArbitraryPrecisionInt a = ...;
	uint b = ...;
	auto c = a & b;
	c <<= 16;
	...

Should't c be of type ArbitraryPresisionInt? And shouldn't the 
following work too?

	uint d = a & b;

That said, how can a function return a polysemous value at all? Should 
the function return a special kind of struct with a sample of every 
supported type? That'd be utterly inefficient. Should it return a 
custom-made struct with the ability to implicitly cast itself to other 
types? That would make the polysemous value propagatable through auto, 
and probably less efficient too.

The only way I can see this work correctly is with function overloading 
on return type, with a way to specify the default function (for when 
the return type is not specified, such as with auto). In the case 
above, you'd need something like this:

	struct ArbitraryPrecisionInt {
		default ArbitraryPrecisionInt opAnd(uint i);
		uint opAnd(uint i);
	}


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list