Jon Stewart on 18 Jul 2003 19:14:01 -0000 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: [ALACPP] oh, the tragedy |
> Jon, Jon, Jon. > > You do know, of course, just how evil you're being? Being evil is a requirement for working with COM. So, yes. > Jeez! > > Why not a nice, simple assign, then the condition? Twice the line count, > thrice as easy to read. > > const bool* val = VarWrapperClass(somePtr->Array[0]); > if(somePtr && 1 == somePtr->Count && val) ... Because that's incorrect. The order of the two lines needs to be reversed, otherwise there's no point in testing somePtr and somePtr->Count -- I need to know that somePtr->Array[0] is valid first. AND because I only want to do something if the conversion succeeds, i.e. val is not null. Semantically, the if-statement is guarding against the user (the COM client) calling this method incorrectly via the IDispatch interface, so if they screwed up the arguments, fail the if. I could write: if(somePtr && 1 == somePtr->Count) { const bool* val = VarWrapperClass(somePtr->Array[0]); if(val) { ... do stuff with *val ... return S_OK; } } return DISP_E_TYPEMISMATCH; but that just sucks. Instead, I defined this function: template<class T> T ToType(DISPPARAMS *args, uint index) { if (args && index < args->cArgs) { return T(VarWrapperClass(args->rgvarg[index])); return 0; } and then write my conditionals as such: if(const bool* val = ToType<const bool*>(args, 0)) { ... do stuff with *val ... return S_OK; } >From here I can write: const bool* val = ToType<const bool*>(args, 0); if (val) { } Happy? :-) > I really, really hate it when people use the side effect of assignment (ie, > the return value). It just adds obfuscation. I wish they'd defined the > language so that assign didn't have a return value, by the rule that each > function should either have a return value or a side effect. OK, so I don't > always believe in the rule, but still. > > And from such a big Functional Programming person. And our const-nazi! Oh, fo > r > shame. For shame. Hey! What's not const-correct about this? ;-) Notice I only allow implicit conversion of pointers to const objects, to ensure r-value usage only. Jon -- Jon Stewart stew1@xxxxxxxxxxx _______________________________________________ alacpp mailing list alacpp@xxxxxxxxxxx http://lists.ellipsis.cx/mailman/listinfo/alacpp