Symbol lookup rules and imports

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Tue Dec 2 15:28:50 PST 2014


On Wed, Dec 03, 2014 at 01:20:59AM +0200, ketmar via Digitalmars-d wrote:
> On Tue, 2 Dec 2014 14:00:09 -0800
> "H. S. Teoh via Digitalmars-d" <digitalmars-d at puremagic.com> wrote:
> 
> > 	// mymod.d
> > 	module mymod;
> > 	struct S {
> > 		// Now everyone is happy, right...?
> > 		import std.range : isInputRange;
> > 
> > 		void method(R)(R range)
> > 			if (isInputRange!R)
> > 		{
> > 		}
> > 	}
> ah, how i HAET that! importing std.traits in module scope just to have
> preconditions in templates drives me mad. i want to be able to insert
> import statements before that `if`! and i want that imports to not
> affect template body too.
[...]

Technically, you *can* do that, but it's a lot more verbose (plus, ddoc
doesn't know how to generate docs for eponymous templates properly and
it looks ugly):

	template method(R)
	{
		import std.range : isInputRange;

		void method(R range)
			if (isInputRange!R)
		{ ... }
	}

Certainly defeats the convenient shorthand of eponymous templates. :-(


T

-- 
Talk is cheap. Whining is actually free. -- Lars Wirzenius


More information about the Digitalmars-d mailing list