Big problem with Small programs

Don Clugston dac at nospam.com.au
Wed Jan 24 08:11:57 PST 2007


Sean Kelly wrote:
> Todor Totev wrote:
>>> Walter Bright wrote:
>>>>   Try using enums instead of const variables, they don't take up any 
>>>> space in the object file.
>>>
>>> Thanks, but that makes only a dent in the overhead.
>>
>>
>> Actually using enums instead of consts have a very nice side effect.
>> Consider this declaration from win32:
>>
>> HANDLE CreateFileW(LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, 
>> DWORD, DWORD, HANDLE);
>>
>> Now, which invocation is correct?
>>
>> CreateFile("a", OPEN_EXISTING, FILE_SHARE_READ, null, GENERIC_READ ...)
>> or
>> CreateFile("a", OPEN_EXISTING, GENERIC_READ, null, FILE_SHARE_READ ...)
>> or even
>> CreateFile("a", GENERIC_READ, OPEN_EXISTING, null, FILE_SHARE_READ ...)?
>>
>> The compiler happily accepts all of them.
>>
>> Using enums, we have:
>>
>> enum FILE_SHARE {
>>   READ  = 1,
>>   WRITE = 2,
>>   BOTH  = READ | WRITE
>> }
>>
>> enum DISPOSITION {
>>   CREATE_ALWAYS = 1,
>>   OPEN_EXISTING = 2
>> }
>>
>> void CreateFile(char[] fileName, DISPOSITION disposition, FILE_SHARE 
>> share) {}
>>
>> int main() {
>>   // compiler allows unknown flags
>>   CreateFile("filename", cast(DISPOSITION)4, FILE_SHARE.READ);
>>
>>   // when we swap the arguments by mistake
>>   CreateFile("filename", FILE_SHARE.READ, DISPOSITION.CREATE_ALWAYS);
>>
>>   return 0;
>> }
>>
>> This way if I make an error the compiler will very helpfully tell me
>> what exactly is happening. Consider: CreateFont has 14 parameters,
>> if the IDE does not help me i'd make an error when I use it, trust me
>>
>> On the other hand, when a new version of Windows come with expanded 
>> options
>> I can just use the new numbers without breaking anything while the d 
>> package is
>> updated.
>>
>> The dotnet framework uses enums and I really like the idea.
> 
> I agree that this is a great feature of D.  And if you're willing to 
> adapt the Win32 headers to use this convention then I would gladly 
> accept them :-)
> 
> 
> Sean

As far as possible, we did that, for the ones that actually are enums. 
The problem is, most of those things aren't actually enums! They are 
complicated bitfields that get ORed together.



More information about the Digitalmars-d mailing list