www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Type-qualified functions?

reply Sean Eskapp <eatingstaples gmail.com> writes:
How does one avoid code duplication in a snippet code like this:

class A{}

void foo(const A, void delegate(const A) fn)
{
	// some stuff
	// ...
	// ...
}

void foo(A, void delegate(A) fn)
{
	// exact same stuff, with different qualifiers
	// ...
	// ...
}
Jan 21 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 21 Jan 2011 09:08:58 -0500, Sean Eskapp <eatingstaples gmail.com>  
wrote:

 How does one avoid code duplication in a snippet code like this:

 class A{}

 void foo(const A, void delegate(const A) fn)
 {
 	// some stuff
 	// ...
 	// ...
 }

 void foo(A, void delegate(A) fn)
 {
 	// exact same stuff, with different qualifiers
 	// ...
 	// ...
 }

templates: void foo(T)(T, void delegate(T) fn) { } This parameterizes foo based on T, which could be A, const A, or int, or whatever works to compile the function. You could further restrict which templates can be instantiated with a template constraint (say for instance, to restring foo to only instantiate when T is A or const(A) ). -Steve
Jan 21 2011
parent reply Sean Eskapp <eatingstaples gmail.com> writes:
 templates:

 void foo(T)(T, void delegate(T) fn)
 {
 }

 This parameterizes foo based on T, which could be A, const A, or int, or
 whatever works to compile the function.

What if the parameters are more general, for instance the first parameter is always a Foo, the second is a delegate which takes a Foo.Bar, but they're always qualified the same way?
Jan 21 2011
next sibling parent Jesse Phillips <jessekphillips+D gmail.com> writes:
Sean Eskapp Wrote:

 
 templates:

 void foo(T)(T, void delegate(T) fn)
 {
 }

 This parameterizes foo based on T, which could be A, const A, or int, or
 whatever works to compile the function.

What if the parameters are more general, for instance the first parameter is always a Foo, the second is a delegate which takes a Foo.Bar, but they're always qualified the same way?

Their is probably better ways to do this... void main() { foo(new Foo, (int) { }); } void foo(T, U)(T t, void delegate(U) fn) if(__traits(compiles, fn(t.bar))) { } class Foo { int bar() { return 0; } }
Jan 21 2011
prev sibling parent Christopher Nicholson-Sauls <ibisbasenji gmail.com> writes:
On 01/21/11 14:43, Sean Eskapp wrote:
 
 templates:

 void foo(T)(T, void delegate(T) fn)
 {
 }

 This parameterizes foo based on T, which could be A, const A, or int, or
 whatever works to compile the function.

What if the parameters are more general, for instance the first parameter is always a Foo, the second is a delegate which takes a Foo.Bar, but they're always qualified the same way?

I believe this is the sort of thing the 'inout' qualifier was meant for, except I don't know if it works with a void return type. It's also worth noting that mutable and immutable are both castable to const, so if there is literally no difference between the two functions, see if you can get away with just the one using const. -- Chris N-S
Jan 22 2011