Meaning of .clear() for containers

Steven Schveighoffer schveiguy at yahoo.com
Tue Jan 4 11:15:57 PST 2011


On Tue, 04 Jan 2011 13:58:02 -0500, Jérôme M. Berger <jeberger at free.fr>  
wrote:

> Steven Schveighoffer wrote:
>> On Tue, 04 Jan 2011 11:53:59 -0500, Jesse Phillips
>> <jessekphillips+D at gmail.com> wrote:
>>
>>> Answering a question over on stack overflow I realized that clear()
>>> has 2 meanings.
>>>
>>> TDPL says that clear should be used to free resources of the object
>>> and place the object into an invalid state. That is failure can occur
>>> but memory corruption is prevent, similar to null for pointer types.
>>>
>>> However for container types clear() is used to empty the container. It
>>> is still valid to use the container after calling clear(), but the
>>> definition from TDPL suggest that this can not be expected.
>>
>> clear as a global function is for destroying a class/struct
>>
>> clear as a member can do anything.  clear is not a keyword.
>>
>> clear(container) -> same as delete container, but without freeing any
>> memory.
>>
>> container.clear() -> remove all elements
>>
>> This has been brought up before as a problem, I'm not sure it's that
>> terrible, but I can see why there might be confusion.
>>
> 	Uniform function call syntax?

I don't expect this to be a huge problem.  Will people who more likely  
destroy an object with:

clear(obj);

or

obj.clear();

?  To me, the first looks like you are doing an operation to the object,  
where the second looks like you are having the object do an operation.

UFC is going to cause lots of these little corner cases.  Another I can  
think of is global properties.  with @property foo(int x), do you call  
this as foo = 1; or 1.foo?

-Steve


More information about the Digitalmars-d mailing list