Big problem with Small programs
kris
foo at bar.com
Tue Jan 23 12:13:58 PST 2007
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.
>
> Regards,
> Todor
Certainly, and I think you'll find many who agree, along with some who
can't bear to type a few extra characters :)
But that's a different type of issue than the one we're facing. I wish
you had a fix for that one :p
More information about the Digitalmars-d
mailing list