clear() and UFCS

deadalnix deadalnix at gmail.com
Fri May 25 07:50:42 PDT 2012


Le 25/05/2012 15:21, Alex Rønne Petersen a écrit :
> On 25-05-2012 15:17, deadalnix wrote:
>> Le 25/05/2012 15:04, Alex Rønne Petersen a écrit :
>>> So I was writing a container class and in some test code, I called
>>> container.clear(). Reasonable enough, right? Hell, it even compiled!
>>>
>>> Turns out, my program completely broke at runtime. Apparently, I'd
>>> forgotten to implement clear() on my container class, and
>>> *object.clear() was being called instead*.
>>>
>>> I don't blame UFCS for this. It just happened to be the reason this
>>> compiled at all. But, what I do think is: clear is the *ABSOLUTELY MOST
>>> HORRIBLE NAME EVER* for a function that actually finalizes and zeroes an
>>> object. Did it not seem obvious to call it, I don't know, *finalize*?
>>> And even ignoring the naming, having a symbol called clear in the
>>> 'global' namespace is absolute insanity.
>>>
>>> Am I the only person with this opinion?
>>>
>>
>> Why is UFCS involved here ?
>
> Since the clear method didn't exist on the type, the call was mapped to
> the clear function in the global scope. This is UFCS.
>

OK, I get it ! Weird side effect :D

BTW, where is the documentation about clear and emplace ? I can't find 
it on dlang.org .


More information about the Digitalmars-d mailing list