www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to pass list of strings as compile-time parameters?

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
I'm trying to write a template function for doing member-wise
comparisons between two objects, with an optional list of members to
ignore. But I can't seem to figure out the syntax for passing a list of
strings (or an AA of strings) to the function?

I tried this:

	bool compareByMemb(string[] ignores, T)(T obj1, T obj2) {
		foreach (name; __traits(getAllMembers, T)) {
			...
		}
	}

but the compiler complains:

	Error: arithmetic/string type expected for value-parameter, not string[]

What gives?


T

-- 
Recently, our IT department hired a bug-fix engineer. He used to work for
Volkswagen.
Apr 24 2012
next sibling parent reply Trass3r <un known.com> writes:
 	bool compareByMemb(string[] ignores, T)(T obj1, T obj2) {
 		foreach (name; __traits(getAllMembers, T)) {
 			...
 		}

In this particular case you could try foo(T, U...)(T obj1, T obj2, U ignores)
Apr 24 2012
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Apr 24, 2012 at 07:39:42PM +0200, Trass3r wrote:
	bool compareByMemb(string[] ignores, T)(T obj1, T obj2) {
		foreach (name; __traits(getAllMembers, T)) {
			...
		}

In this particular case you could try foo(T, U...)(T obj1, T obj2, U ignores)

Sure, but U always string. Though I suppose I *could* use a signature constraint to enforce that... T -- Guns don't kill people. Bullets do.
Apr 24 2012
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Apr 24, 2012 at 10:47:53AM -0700, H. S. Teoh wrote:
 On Tue, Apr 24, 2012 at 07:39:42PM +0200, Trass3r wrote:
	bool compareByMemb(string[] ignores, T)(T obj1, T obj2) {
		foreach (name; __traits(getAllMembers, T)) {
			...
		}

In this particular case you could try foo(T, U...)(T obj1, T obj2, U ignores)

Sure, but U always string. Though I suppose I *could* use a signature constraint to enforce that...

Well actually, U is a type tuple... but I want to constrain it to be always a tuple of strings. How would that work? T -- It is widely believed that reinventing the wheel is a waste of time; but I disagree: without wheel reinventers, we would be still be stuck with wooden horse-cart wheels.
Apr 24 2012
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 04/24/2012 07:37 PM, H. S. Teoh wrote:
 I'm trying to write a template function for doing member-wise
 comparisons between two objects, with an optional list of members to
 ignore. But I can't seem to figure out the syntax for passing a list of
 strings (or an AA of strings) to the function?

 I tried this:

 	bool compareByMemb(string[] ignores, T)(T obj1, T obj2) {
 		foreach (name; __traits(getAllMembers, T)) {
 			...
 		}
 	}

 but the compiler complains:

 	Error: arithmetic/string type expected for value-parameter, not string[]

 What gives?


 T

Try using an alias parameter (with an optional type constraint). imo the template instantiation semantics needs a clean-up.
Apr 24 2012