Meaning of .clear() for containers

Steven Schveighoffer schveiguy at yahoo.com
Fri Jan 7 06:10:38 PST 2011


On Fri, 07 Jan 2011 07:54:06 -0500, so <so at so.do> wrote:

>> 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.
>
> Thanks for the example, this semantical issue was bugging me for a while  
> (not the OOP vs whatever flamewar).
>
> Is there a semantical difference between "function(obj)" and  
> "obj.function()"?
> If we are not clear on this simple thing, we should just stop here :)

Yes, there is a huge semantic difference.  One is a member function the  
other is a free function.  Currently, you must use function(obj) for a  
free function and obj.function() for a member function.

The one exception is for arrays, for which arr.fn() is translated to  
fn(arr).

Theoretically, we are going to get something called uniform function call  
syntax, where that property of arrays is spread to all types (including  
classes and structs), and at that point, we are going to start having  
conflicts.  The obvious thing to do in this case is that an actual member  
function always overrides a free function.  But this would cause confusion  
with things like 'clear', where a member function can override the  
semantic meaning of a common function.

My personal opinion is that UFC is likely going to be more trouble than  
it's worth (recent developments have significantly lowered the value of  
UFC, such as final interface functions), but it is in TDPL, so we'll see  
if D sticks to that plan.

-Steve


More information about the Digitalmars-d mailing list