www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - clear works better than it ought to considered i never defined it

reply "Vlad Levenfeld" <vlevenfeld gmail.com> writes:
A weird thing happened:

I was building a dictionary struct which contains a custom array 
of values and a differently-typed custom array of keys. Both of 
them implicitly define "clear" by aliasing a backing array.

The dictionary type doesn't have clear, though. And it doesn't 
alias anything. Yet when I call clear on it, not only does it 
work, but it clears both of the private contained arrays. I can't 
find any free clear function in Phobos.

Not that I'm complaining, but... what's going on here?
Jul 27 2014
parent reply "Mike" <none none.com> writes:
On Monday, 28 July 2014 at 06:27:44 UTC, Vlad Levenfeld wrote:
 A weird thing happened:

 I was building a dictionary struct which contains a custom 
 array of values and a differently-typed custom array of keys. 
 Both of them implicitly define "clear" by aliasing a backing 
 array.

 The dictionary type doesn't have clear, though. And it doesn't 
 alias anything. Yet when I call clear on it, not only does it 
 work, but it clears both of the private contained arrays. I 
 can't find any free clear function in Phobos.

 Not that I'm complaining, but... what's going on here?
I'm not sure, but is your code calling the runtime `clear` method: https://github.com/D-Programming-Language/druntime/blob/master/src/object.di#L519 It has been marked for deprecation in the upcoming 2.066 release. Mike
Jul 27 2014
parent "Vlad Levenfeld" <vlevenfeld gmail.com> writes:
Yep, just checked in the debugger. So, that's actually a bad 
thing, then. Good thing its being deprecated!
Jul 27 2014