Re: [GD-General] Eiffel
Brought to you by:
vexxed72
From: Jesse J. <jes...@mi...> - 2001-12-23 23:56:00
|
At 11:47 PM -0500 12/22/01, Thatcher Ulrich wrote: >On Sat, 22 Dec 2001, Jesse Jones wrote: > > It allows you to write code like this: >> >> Editor* editor = ...; >> boldButton->SetCallback(boost::bind(&Editor::SetStyle, editor, >> kBold)); >> >> italicButton->SetCallback(boost::bind(&Editor::SetStyle, > > editor, kItalic)); > >That's nice, but I don't think that addresses Kent's complaint. Like I said, it's not a replacement for closures. >Let's >say you wanted to toggle the bold mode on boldButton.callback; how >would you write: > > struct anon { > static Editor* editor; > anon(Editor* e) : editor(e) {} > static void callback() { > int s = editor->GetStyle(); > s ^= kBold; > editor->SetStyle(s); > } > } instance(editor); > boldButton->SetCallback(instance::callback); > >I'm not claiming that's great (in fact it stinks :). You can't write it inline like Kent wants to (although you might be able to with the lambda library). In general I think the best approach is to put the logic in a member function and have the callback call that. >An example of how the STL is not "really easy to use": > > std::hash_map<string, int> my_hash; > > ... > string my_key = something...; > int my_value = something...; > > // I want to see if my_key is in my_hash, and if so, whether > // its value matches my_value. > std::hash_map<string, int>::iterator it = my_hash.find(my_key); > if (it != my_hash.end() && (*it).second == my_value) > { > // Match. > } else { > // Mismatch. > } Try: if (my_hash.count(my_key) > 0) // match else // mismatch >I think I got that right. The iterator is just obfuscation. Whereas >the way you'd do it in Perl or Lua, the seemingly obvious: > > if (my_hash[my_key] == my_value) { > // Match. > } else { > // Mismatch. > } > >compiles, and leaks memory! Compiles, but shouldn't leak memory. > I've seen code like the above, written by >professional C++ coders. I probably wrote code like that myself the >first half-dozen times I used map or hash_map. A hash container >should not define operator[] the way std::hash_map does; it's just >programmer abuse. It's not the way I would have written it, but calling it programmer abuse is way too strong. >I find that kind of thing is endemic to the STL. The STL was designed >(cleverly) as a demonstration of generic programming, rather than as a >nice container library. >Unfortunately, people use it as a container >library, and IMO it stinks for that. It's far better than any other container library I've seen... -- Jesse |