Tuple/TypeTuple etc.

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Aug 16 17:24:13 PDT 2013


On Sat, Aug 17, 2013 at 01:23:57AM +0200, bearophile wrote:
[...]
> ----------------------
> 
> Jonathan M Davis:
> 
> >Since the built-in tuples / std.typetuple.TypeTuple and
> >std.typecons.Tuple are fundamentally different, I don't see how you
> >could possibly combine the two in a single syntax. You'd need
> >different syntaxes for each one.
> 
> You are right, they are different things, sorry.
> But let's hypothesize there is only one syntax for both of them.
> What bad things are going to happen? Can't the compiler infer by
> itself what of the two kinds you want to use?
[...]

The problem is that the compile-time tuples, let's call them CT-tuples,
can contain compile-time-only elements, like types, and also things like
integer literals and other compile-time values. But std.range.Tuple,
let's call them runtime-tuples (RT-tuples) can only contain runtime
values. So suppose you have code like this:

	template makeTuple(alias x, alias y) {
		// assuming we adopt #(...) syntax:
		alias makeType = #(x,y);
	}

	alias a1 = makeTuple!(int,string); // obviously a CT-tuple
	alias a2 = makeTuple!(int,123);    // also a CT-tuple
	alias a3 = makeTuple!(123,456);    // <-- what's this?

Is a3 a CT-tuple or an RT-tuple? I don't think it's possible for the
compiler to tell.

OTOH, thinking about it a bit more...  suppose we not only use the same
syntax for CT-tuples and RT-tuples, but make them the *same thing*, by
having the compiler turn a CT-tuple into an RT-tuple if you try to
perform runtime operations on it like assigning it to a variable. This
will fail if the CT-tuple contains types, obviously, but can it work in
other contexts? It seems like it might be possible. I'm not sure if it
will lead to problems with ambiguity.  Certainly, it would constitute a
non-trivial change to the language, which may have far-reaching
consequences.

One interesting effect is that t is an RT-tuple, then typeof(t) is a
CT-tuple, but since they're the same thing, then the type of a tuple is
another tuple. So there's a kind of curious symmetry going on here. I
don't know if this will lead somewhere interesting, or perhaps it may
lead to contradictions. Might be worth exploring... but I'd wager the
chances of this actually making it into the language are rather slim at
this point.


T

-- 
EMACS = Extremely Massive And Cumbersome System


More information about the Digitalmars-d mailing list