Earlier quoted context omitted.
Which means you don't like generic programming or templates. That in turn tells me something about the types of programmers you know. For example, neither you nor they likely use the Boost libraries. In any case, it's a bit of a distraction. "Most programmers" aren't all that good at programming, or judging what makes a good language. How important then is it that I weigh your projection of their ideas of right or wr…
Do any of your examples (generic programming, templates, Boost libraries) actually utilize the concept of testing the truthiness of an object of unknown type? I think your criticism is invalid. I like generic programming in Java, though I admittedly have not used templates or Boost libraries for anything substantial. > In Python it absolutely, positively, without a doubt is not a bad thing to have a function which ac…
[xebulon:~/tmp] dalke% cat tmp.cc
#include
template
int f(T s) {
return s ? 2 : 0;
}
main() {
std::cout
The function f() doesn't know the type, but its instantiation for f(0) (integer) and f(0.0) float know the type.C++ containers don't have a bool (or at least vector doesn't). For one, until C++11 there was no "explicit operator bool", and a simple "operator bool" was too permissive because of implicit type conversion. C++11 introduced a more contextual conversion to bool.
More and more components have explicit bool support. For example, http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/shared_p... ?
Notes: This conversion operator allows shared_ptr objects to be
used in boolean contexts, like if(p && p->valid()) {}.
[The conversion to bool is not merely syntactic sugar. It allows shared_ptrs
to be declared in conditions when using dynamic_pointer_cast
or weak_ptr::lock.]
and http://en.cppreference.com/w/cpp/memory/unique_ptr/operator_... .I do not track C++ well enough to go into any more depth than this, especially as it concerns the history and future.
I like to say "if x: ..." and not "if len(x) == 0:" in order to check if a dictionary is empty.
Following Scheme, I can understand that something like "if empty(x)" might be more explicit. But I have a decent amount of code where I do something like:
def process(a, rename=None):
if rename:
a = [rename.get(x, x) for x in a]
... do things with a ...
In this toy example, "rename=None" indicates that there is no renaming dictionary, and using {} as the renaming dictionary won't rename anything, so the "if rename" tests for both conditions correctly.Without bool, I could write it as:
if rename is not None:
since using the empty dictionary in this case is okay, but if I want the slight extra performance for the empty dictionary case I would have to write it: if rename is not None and not empty(rename):
I think that would grow tedious.BTW, I don't use "def process(a, rename={}):" because that is one of Python anti-patterns: the default values are constant over the life of the function, so
def something(a, b={}):
b[a] = a
return b
will accumulate to b rather than create a new dictionary each time. Thus, seeing a "={}" or "=[]" in a parameter list demands closer inspection because it often leads to errors.