www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Overloading methods by constness?

reply Nathan Reed <nathaniel.reed gmail.com> writes:
Hello,

In C++, you can overload methods by the constness of the method itself:

	class Foo
	{
		void bar ();		// #1
		void bar () const;	// #2
	}
	...
	Foo f;		f.bar();	// calls #1
	const Foo g;	g.bar();	// calls #2

I am wondering if there is any way to get similiar functionality in D? 
I have tried the following:

	class Foo
	{
		void bar () { ... }
		const void bar () { ... }
	}

but get a compiler error that the "bar"s conflict.

Why would I want to do this, you might ask?  Well, I would like to use 
this to write a pair of opSlice methods.  With built-in arrays, you can 
do this:

	char[] str = "Hello, world".dup;
	char[] substr = str[0..5];
	
	const(char)[] str2 = "Hello, world";
	const(char)[] substr2 = str2[0..5];
	char[] substr3 = str2[0..5]; 	// fails

i.e., the slice returns either a char[] or a const(char)[] depending on 
the constness of the array.  However, I cannot see how to make my own 
classes do the same thing, unless I can overload opSlice by constness.

Thanks,
Nathan Reed
Aug 21 2007
next sibling parent Paul Collier <paching gmail.com> writes:
Nathan Reed wrote:
 but get a compiler error that the "bar"s conflict.

yet, so it's impossible to overload like that until mangling is changed. :( I suspect Walter is hesitant to completely break library compatibility from D 1.0 to D 2.0! That's the problem with const correctness... gotta do it all the way or not at all... ;)
Aug 22 2007
prev sibling next sibling parent reply "Craig Black" <cblack ara.com> writes:
Perhaps this should be posted as a bug?

"Nathan Reed" <nathaniel.reed gmail.com> wrote in message 
news:faga07$hj9$1 digitalmars.com...
 Hello,

 In C++, you can overload methods by the constness of the method itself:

 class Foo
 {
 void bar (); // #1
 void bar () const; // #2
 }
 ...
 Foo f; f.bar(); // calls #1
 const Foo g; g.bar(); // calls #2

 I am wondering if there is any way to get similiar functionality in D? I 
 have tried the following:

 class Foo
 {
 void bar () { ... }
 const void bar () { ... }
 }

 but get a compiler error that the "bar"s conflict.

 Why would I want to do this, you might ask?  Well, I would like to use 
 this to write a pair of opSlice methods.  With built-in arrays, you can do 
 this:

 char[] str = "Hello, world".dup;
 char[] substr = str[0..5];

 const(char)[] str2 = "Hello, world";
 const(char)[] substr2 = str2[0..5];
 char[] substr3 = str2[0..5]; // fails

 i.e., the slice returns either a char[] or a const(char)[] depending on 
 the constness of the array.  However, I cannot see how to make my own 
 classes do the same thing, unless I can overload opSlice by constness.

 Thanks,
 Nathan Reed 

Aug 22 2007
parent Nathan Reed <nathaniel.reed gmail.com> writes:
Witold Baryluk wrote:
 
 This isn't bug.
 
 Look also at the 
 http://s3.amazonaws.com/dconf2007/WalterAndrei.pdf
 
 page 38.

Good, I'm glad to see this problem is being addressed. I do have a question about the 'return' storage-class, though. Will it only work for non-member functions, or will there be a syntax for writing a member function for which 'this' has the 'return' storage-class? Although, with the new interchangeable function call syntax, this doesn't matter that much... Thanks, Nathan Reed
Aug 26 2007
prev sibling parent Witold Baryluk <baryluk smp.if.uj.edu.pl> writes:
Dnia Wed, 22 Aug 2007 14:20:19 -0500
"Craig Black" <cblack ara.com> napisa=B3/a:

 Perhaps this should be posted as a bug?
=20

This isn't bug. Look also at the=20 http://s3.amazonaws.com/dconf2007/WalterAndrei.pdf page 38. regards. --=20 Witold Baryluk, aleph0 MAIL: baryluk smp.if.uj.edu.pl JID: movax jabber.autocom.pl
Aug 26 2007