23 points · by apgwoz · 18y ago · 70 comments
Python: The error message passed when you call a method with the wrong number of arguments. It's just off since the method "def apple(self, steve, new_product)" has three args, but you'll call it with two "apple(steve, new_product)". So, the message that gets passed back if I pass in one or three arguments is just wrong. I actually like the explicit self in the definition, but the error message needs to say something like "method given the wrong number of args. 1 argument passed when there are 2 arguments plus implicitly passed self."
Python: The functions that should be methods. len(list) just doesn't rub me as well as list.len(). It's such a stupid criticism that I'm hesitant to post it, but whatever, it bothers me. Likewise, Ruby's int.to_f seems much nicer than a built-in function float(int). Same with int and str.
PHP: The fact that 0 == "apple". Yeah, integer 0 is equal to any string. That means: $a = 0; $b = "apple" $c = "ms" $a == $b $b != $c $a == $c
Yeah, a == b, a == c and b != c. Um? Heck, integer 0 is equal to an uninitialized variable. It just makes bugs that are so hard to find. Yeah, it stops you from having to cast in certain situations, but for the little time it takes to cast, you end up with horrible bugs.
Java: The fact that I can't do if(a) { return true } else { return false } in a method. The Java compiler will say that I don't have a return in that method because all of my returns are within a conditional even though it will always hit one of the return statements. if the Java compiler is going to check that I have an appropriate return, be smart enough to figure out that I do.
>>> print float.__class__.__name___
type
See, "float" is a type. float(int) is a constructor for a floating point number. It's not a magic built-in function, it's just a constructor for type float. Same with int and str. I can't say there's anything wrong with Ruby's int.to_f, but I actually like the Python design better.Now, as far as len() goes, I agree. It's easy to forget about such troubles, but I definitely wasted a good 10 minutes when I was learning Python trying to figure out what property or method of type list (or str--I don't remember) I should use to get its length.
fix_to_f(VALUE num) {
double val;
val = (double)FIX2LONG(num); //RSHIFTs sign
return DBL2NUM(val); //Ruby float constructor of C double value
}
It does the same thing. Ruby, by design, flows left to right instead of nested params. This way you don't have float(user.timestamp) but instead user.timestamp.to_f.public static String foo(final boolean b) { if( b ) { return "Yes"; } else { return "No"; } }
Compiles just fine.
I multiply by 1.0 to turn an int into a float:
Python 2.5.1 (r251:54863, Jul 31 2008, 23:17:40)
>>> 3/2
1
>>> 3*1.0/2
1.5
>>> float(3)/2
1.5I'm not aware of unambiguous situations where the compiler will complain; if you see an error it's usually pebkac!
if (a)
return (some && complicated || boolean && calculation);
else
return (some || other && boolean || calculation);
edit: I suppose you can get rid of the "else", but you shouldn't have to sacrifice readability for a compiler shortcoming. I'm with mdasen; the compiler should figure it out.if (a) { return true; } return false;
if (a) { return true; } is just as redundant as an if (a == true) statement.
I think that the most intrusive one is semicolon insertion. This is perfectly valid code that does not do what it looks like:
return
{
a: "foo"
};
That's quite different from: return {
a: "foo"
}; var closures = [];
for (var i = 0; i < 10; i++)
{
var j = i;
closures[i] = function() { alert(j); }
}
closures[0](); // outputs 9, WTF?
How to fix it: var closures = [];
for (var i = 0; i < 10; i++) (function()
{
var j = i*i;
closures[i] = function() { alert(j); }
})(i)
closures[0](); // outputs 0 as it should var closures = [];
for (var i = 0; i < 10; i++)
{
var j = i;
closures[i] = (function (k) { return function() { alert(k); })(j);
}
closures[0](); // outputs 0
The issue is that the var here is passed by reference. Ie, all 10 closures refer to the SAME "i" variable. So, when i is incremented, they see that it's been incremented.Outputing 9 is actually correct in the first case. If you want to trap the current value, you need to copy the value to a new reference, ie, by making it the argument of a function with a new scope.
The issue is that the var here is passed by reference. Ie, all 10 closures refer to the SAME "i" variable. So, when i is incremented, they see that it's been incremented.
No, closures refer to the j variable that doesn't get incremented, only assigned to. The assignment is by value - no vars get "passed by reference", whatever this means. The actual issue is that all invocations of the loop body alias the same var j, even though it's declared inside the block.
I usually wrap the whole loop body if it contains multiple variables and multiple closures closing over them - it's easier on the brain than fixing every closure creation spot. Kinda sad because JavaScript is almost the perfect language for my mind.
Just pass in j when you call it and you can have lexical scope easily.
But if you changed that, a TON of code would be impossible, so I'm very happy with it working that way.
Function scope is the entire point of closures. If it was lexical scope it would not be a closure, it would just be a regular function.
Many more languages with closures have lexical scope than function scope. Lexical scope is the entire point of closures in those languages, indeed everywhere except JavaScript.
Just pass in j when you call it and you can have lexical scope easily.
I don't know j when I call the closure. Give a code example.
But if you changed that, a TON of code would be impossible, so I'm very happy with it working that way.
I can't imagine any code that would become impossible, or even the slightest bit more difficult, if function scope were replaced with lexical. Indeed all code using function scope could be mechanically translated into a (hypothetical) lexically-scoped-JavaScript by moving every var declaration in every if/for/while/etc block up to the toplevel of the containing function. If you disagree, give a code example.
Woho! I just won a religious war.
The weird thing is just how the interpreter handles this:
return;
{
a: "foo";
}
That's a null return, a block (not that JS supports block-level scope, so blocks are pointless), a labeled line (line-labels aren't actually used for anything, since there's no GOTO), and on that labeled line, there's a string expression that does nothing.So, this is really like 4 different warts working together, but semicolon insertion is by far the most insidious.
Also, liberal use of parens can help prevent semicolon insertion bugs.
proc foo {x} {
# do stuff
}
but if you really want to get around this you can abuse the continuation character proc foo {x} \
{
# do stuff
} function empty_p(t)
for k,v in pairs(t) do
return true
end
return false
end
-- later on
if not empty_p(t) then
do_processing(t)
end
This is because although Lua offers a #t operator, it's only
guaranteed to be correct if you're careful about how the table is
constructed. That and the weird ipairs() behavior for non-trivial
tables drives me crazy sometimes. Here's an example: Lua 5.1.2 Copyright (C) 1994-2007 Lua.org, PUC-Rio
> t = {1,2,3}
> print(#t)
3
> for i,v in ipairs(t) do print(i,v) end
1 1
2 2
3 3
Ok so all is good. But now what happens when the table contains a nil? > t = {1,nil,3}
> print(#t)
3
> for i,v in ipairs(t) do print(i,v) end
1 1
ipairs() stops at the first nil value even though Lua knows there is
data past it. Things get more interesting for tables with non-integer
keys: > t = {}
> t['a'] = 1
> t['b'] = 2
> print(#t)
0
> t[1] = 1
> print(#t)
1
> for i,v in ipairs(t) do print(i,v) end
1 1
What's going on here is each Lua table has an array for integer valued
keys and a separate hash table for non-integer keys and # and ipairs()
only operate on the first array. To process all the keys you use
pairs(): > for k,v in pairs(t) do print(k,v) end
1 1
a 1
b 2
While at first this doesn't seem to be such a big issue, it's easy to
forget when it's safe to use the #t construct. I've made this error
so often that now whenever my Lua code doesn't do what I expect the
first thing I look for are places where I have something like: if #t > 0 then
do_processing(t)
end
or for i,v in ipairs(t)
process(v)
end
because chances are the real problem will turn out to be the particular way I initialized the table.> Ok so all is good. But now what happens when the table contains a nil?
One of the invariants for the way Lua tables are implemented is that they won't contain nil as a value, IIRC. Using a multifaceted table type for both arrays and dicts is a mixed blessing. It is really convenient much of the time, but it does lead to some quirks.
NB: The i in "ipairs" means "integer (keyed) pairs".
Common Lisp: built-in functions aren't generic. Tedious verbosity of defclass. No literal syntax for hash tables. Debuggers so much worse than they should be.
Smalltalk: having to manually implement getters/setters. Awkwardness of code generation.
Ruby: gems not having a single canonical name (what you require != what you install).
Maybe it shouldn't but this pisses me off.
(a, b, c).join(',')
instead of: ','.join((a, b, c))
>>> "abcbe".split("b")
['a', 'c', 'e']
So to get back the original string you should be able to say: ['a', 'c', 'e'].join("b") #!!! WRONG !!!
but this doesn't work and instead you have to say: >>> "b".join(['a', 'c', 'e'])
'abcbe'br = '<br>'.join
br((a, b, c))
Personally I think it makes more sense the ruby way but it's never bugged me.
The first returns [], but the second returns [''].
I hate it too.
There are arguments for doing it either way, but making indexing incompatible with the language in which it is expected to embed in is just rude.
And no, the "restrict" keyword doesn't fix much of anything.
The most annoying rule is that char * pointers can alias anything... especially when one has to deal with enormous amounts of 8-bit image data.
Time.now -1.day
Of course, what it should have been was: Time.now - 1.dayThis works as expected, though:
Time.now-1.dayhas some good responses
c# - Constraints on typed params.
public T AssignLiteItem<T>(ArchivingItem archivingItem ) where T : Item {
T myITem = (T)typeof(T).GetConstructor(new System.Type[] {}).Invoke(new Object[] { });
myITem.ItemReference = new ItemReference(archivingItem.Id);
return myITem;
}
public T AssignLiteItem<T>(ArchivingItem archivingItem ) where T : Item, new() {
T myITem = new T();
myITem.ItemReference = new ItemReference(archivingItem.Id);
return myITem;
}
I do wish the compiler handled constraints for arbitrary constructor signatures on generic types, however...
well, unless you do
from __future__ import divisionI find the Smalltalk-ishness of it a little weird when it intersperses with more traditional C code.