comma operator causes hard to spot bugs

Alex Rønne Petersen xtzgzorex at gmail.com
Sat Apr 21 05:25:14 PDT 2012


On 21-04-2012 14:23, Benjamin Thaut wrote:
> Hi,
> I just had a bug due to the unitentional usage of the comma operator. So
> I'm wondering if there could something be done so that the compiler
> prevents something like this:
>
> memStart = cast(T*)(m_allocator.AllocateMemory(size * T.sizeof),
> InitializeMemoryWith.NOTHING);
>
> This first excutes AllocateMemory, then throws away the result and then
> casts the enum InitializeMemory.NOTHING value to a pointer, which will
> always result in a null pointer. This took me quite some time to spot.
> What I actually wanted to do:
>
> memStart = cast(T*)(m_allocator.AllocateMemory(size * T.sizeof,
> InitializeMemoryWith.NOTHING));
>
> 1) So is it actually neccessary that enums can be casted directly to
> pointers?
> 2) Is the functionality provided by the comma operator actually worth
> the bugs it causes?

No. It's an abomination, and it blocks more novel/interesting language 
design opportunities.

>
> Kind Regards
> Benjamin Thaut


-- 
- Alex


More information about the Digitalmars-d mailing list