UDAs - Restrict to User Defined Types?

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Nov 8 20:17:24 PST 2012


On Thu, Nov 08, 2012 at 10:45:05PM -0500, Nick Sabalausky wrote:
> On Thu, 08 Nov 2012 10:05:30 +0100
> Jacob Carlborg <doob at me.com> wrote:
> > 
> > I think we should only allow user defined types marked with
> > @attribute, i.e.
> > 
> > @attribute struct foo {}
> > @attribute class foo {}
> > @attribute interface foo {}
> > @attribute enum foo {}
> > 
> > And so on.
> > 
> 
> I completely agree. I really hate when languages "play it loose" and
> leave things up to arbitrary convention. It's like duck/structural
> typing: the *one* thing I hate about D ranges is that they don't force
> you to explicitly say "Yes, I *intend* this to be an InputRange" (what
> are we, Go users?). I don't want to see the same unhelpful sloppiness
> here.

Actually, I just thought of a solution to the whole duck-typing range
thing:

	struct MyRange {
		// Self-documenting: this struct is intended to be a
		// range.
		static assert(isInputRange!MyRange,
			"Dude, your struct isn't a range!"); // asserts

		@property bool empty() { ... }
		@property auto front() { ... }
		int popFront() { ... }
	}

	struct WhatItShouldBe {
		// Junior programmer modifies the function signatures
		// below and the compiler gives him a scolding.
		static assert(isInputRange!WhatItShouldBe,
			"Dude, you just broke my range!"); // passes

		@property bool empty() { ... }
		@property auto front() { ... }
		void popFront() { ... }
	}

	void main() {
		auto needle = "abc";
		auto x = find(WhatItShouldBe(), needle);
	}


T

-- 
I see that you JS got Bach.


More information about the Digitalmars-d mailing list