Voldemort Types

H. S. Teoh hsteoh at quickfur.ath.cx
Wed May 8 15:25:45 PDT 2013


On Wed, May 08, 2013 at 05:45:35PM -0400, Jonathan M Davis wrote:
> On Wednesday, May 08, 2013 22:46:53 =?UTF-8?B?Ikx1w61z?=.Marques 
> <luismarques at gmail.com>@puremagic.com wrote:
> > On Wednesday, 8 May 2013 at 18:41:27 UTC, Jonathan M Davis wrote:
> > > It probably _should_ document that it's returning a range.
> > > Voldemort types just make it so that the exact type is unknown.
> > 
> > So why not just declare "SomeRange generator()"? It still doesn't
> > leak out the internal struct and any automatic doc generator should
> > have a easier time extracting the type. There must be something I'm
> > missing.
> 
> You shouldn't _be_ extracting the type. The whole point of a Voldemort
> type is that you know what the API is but you generally can't
> construct the type or doing anything which involves its name (though
> the typeof operator can make it so that you can declare member
> variables with it if you absolutely have to).  You know the API,
> because you know it's a range. Its actual type is irrelevant, and you
> shouldn't need to know it.
[...]

	auto r = generator();	// get Voldemort return value
	alias RangeType = typeof(r); // convenient name for it
	RangeType r1 = r;	// copy it around as you wish

If you need to, say, store it in a struct, you could do something like
this:

	struct S {
		alias RangeType = typeof(generator());
		RangeType r;

		this() {
			this.r = generator();
		}
		...
	}

Having said that, though, it would be nice if we adopted some kind of
convention for stating things about the return type without actually
naming the return type. Some along the lines of:

	auto generator() {
		...
	}
	static assert(isInputRange!(typeof(generator())));

Or maybe an out-contract? No, wait, it has to be static, not dynamic, so
we'd need a static out-contract.

In any case, DDoc should somehow include this in the documentation so
that it's clear what attributes the return type is expected to have.


T

-- 
Nothing in the world is more distasteful to a man than to take the path
that leads to himself. -- Herman Hesse


More information about the Digitalmars-d mailing list