What is, in your opinion, the most surprising, weird, strange or really "WTF" language feature you have encountered?
Please only one feature per answer.
What is, in your opinion, the most surprising, weird, strange or really "WTF" language feature you have encountered?
Please only one feature per answer.
In Java you might expect
byte b = 0;
b++;
to be equal to
byte b = 0;
b = b + 1;
But it is not. In fact you get a compiler error, as the result of the addition is of type int and therefore not assignable to the byte variable b. When using the compound operator ++
The compiler automatically inserts a cast here. So
b++;
becomes
b = (byte) b + 1;
Why does C#'s List<T>.AddRange() not let me Add elements of a subtype of T? List<T>.Add() does!
All it would take would be ONE extra line of code on Microsoft's part:
public void AddRange<S>(
IEnumerable<S> collection
) where S : T
In C#, this should at least generate a compiler warning, but it doesn't:
public int Something
{
get { return Something; }
set { Something = value; }
}
When called, it causes your app to crash, and you don't get a good stack trace, since it's a StackOverflowException.
Variable/function declarations in Javascript:
var x = 1;
function weird(){
return x;
var x = 2;
}
weird() returns undefined... x is 'taken' even though the assignment never happened.
Similarly, but not so unexpectedly
function weird2(){
var x;
return x();
function x(){ return 2 };
}
returns 2.
hoisting
of the variable & function declarations
VBScript's date/time literals (why is this still so rare?):
mydate = #1/2/2010 5:23 PM#
If mydate > #1/1/2010 17:00# Then ' ...
Edit: Date literals are relative (are they technically literals, then?):
mydate = #Jan 3# ' Jan 3 of the current year
VB.NET, since it is compiled, does not support relative date literals. Date only or time only literals are supported, but the missing time or date are assumed to be zero.
Edit[2]: Of course, there are some bizarre corner cases that come up with relative dates...
mydate = #Feb 29# ' executed on 2010-01-05, yields 2/1/2029
I've written a programming language for a client (used for experimentally driving custom hardware) with some custom types (Curl, Circuit, ...) that each have only 2 values. They are implicitly convertible to boolean, but (at the request of the client) the exact boolean value of a constant of such a type can be changed at runtime.
E.g.: The type Curl has 2 possible values: CW and CCW (clockwise and counterclockwise). At runtime, you could change the boolean value by a simple assignment statement:
ccw := true
So you could change the boolean meaning of all values of those types.
ccw
to be defined based on what is in actual data), but only if all such assignments are the same. Differences should be flagged as conflicts, unless there's some awesome way for the compiler to translate between modules. It does seem pretty out there at first glance, I'll agree, but it's a neat idea if you're dealing with input data whose sense you have no control over.
ActionScript 3:
When an object is used by its interface, the compiler doesn't recognize the methods inherited from Object
, hence:
IInterface interface = getInterface();
interface.toString();
gives a compilation error. The workaround is casting to Object
Object(interface).toString();
PHP:
.
and +
operators. It has its reasonable explanation, but still "a" + "5" = 5
seems awkward.
Java (and any implementation of IEEE754):
System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
Outputs 0.9999999999999999
->
for qualifying objects instead of the prettier .
.
+
operator for both string concatenation as well as addition. In PHP, if you see +
you know you're talking about adding numbers. ...(unless you've got arrays...)
When I was in college, I did a little bit of work in a language called SNOBOL. The entire language, while cool, is one big WTF.
It has the weirdest syntax I've ever seen. Instead of GoTo, you use :(label). And who needs if's when you have :S(label) (goto label on success/true) and :F(label) (goto label on failure/false) and you use those functions on the line checking some condition or reading a file. So the statement:
H = INPUT :F(end)
will read the next line from a file or the console and will go to the label "end" if the read fails (because EOF is reached or any other reason).
Then there is the $ sign operator. That will use the value in a variable as a variable name. So:
ANIMAL = 'DOG'
DOG = 'BARK'
output = $ANIMAL
will put the value 'BARK' on teh console. And because that isn't weird enough:
$DOG = 'SOUND'
will create variable named BARK (see the value assigned to DOG above) and give it a value of 'SOUND'.
The more you look at it, the worse it gets. The best statement I ever found about SNOBOL (from link text) is "the power of the language and its rather idiomatic control flow features make SNOBOL4 code almost impossible to read and understand after writing it. "
$animal[$dog][$bark]=$sound;
or class $animal->dog->bark=$sound;
Jun 11, 2010 at 3:40
In Haskell:
let 2 + 2 = 5 in 2 + 2
yields 5.
let 2 + 2 = 5 in 2 + 3
would yield a pattern matching failure.
Perl is full of odd but neat features.
if
may be used before or after the statement like this:
print "Hello World" if $a > 1;
if ($a > 1) { print "Hello World"; }
The same is true for foreach
:
print "Hello $_!\n" foreach qw(world Dolly nurse);
if
modifier as it makes the programs more expressive and readable. it lets you place the more important part of the statement (the condition or the action) before the other so it is prominent. Like any feature, it is useful if used judiciously and not abused.
Jan 5, 2010 at 14:22
last unless defined $row
Jan 5, 2010 at 18:24
In PHP "true", "false" and "null" are constants which normally cannot be overridden. However, with the introduction of namespaces in PHP >=5.3, one can now redefine these constants within any namespace but the global namespace. Which can lead to the following behaviour :
namespace {
define('test\true', 42);
define('test\false', 42);
define('test\null', 42);
}
namespace test {
var_dump(true === false && false === null); // is (bool) true
}
Of course if you want your trues to be true, you can always import true from the global namespace
namespace test {
var_dump(\true === \false); // is (bool) false
}
define("TRUE",false,false);define("true",true,false);
would be echo(TRUE==false);//echos true
and echo(true==false);//echos false
... TRUE
would then mean not really true, unless its lowercase.
Jun 11, 2010 at 2:39
LOLCODE!
The whole language itself. While not exactly a WTF thing, I've never come across a language which plays out in my head in a squeeky cartoony voice. Nor have I ever looked at code before and want to exclaim "aaaawwww cuuute!"
This program displays the numbers 1–10 and terminates
HAI
CAN HAS STDIO?
IM IN YR LOOP UPPIN YR VAR TIL BOTHSAEM VAR AN 10
VISIBLE SUM OF VAR AN 1
IM OUTTA YR LOOP
KTHXBYE
In PHP:
echo 'foo' == 0; // echos '1'
echo 'foo' == true; // echos '1'
echo 0 == true; // echos '0'
$foo = 'foo';
echo $foo['bar'] // echos 'f'
PHP has some of the most annoying type coercion...
In C or C++ you can have a lot of fun with Macros. Such as
#define FOO(a,b) (a+b)/(1-a)
if FOO(bar++,4) is passed in it'll increment a twice.
#define while if
(who needs loops?) #define void int
("Why is the compiler complaining about no explicit return from my void
functions?") #define main(argv, argc) (main)(argc, argv)
(switch argv and argc for no apparent reason)
Jan 4, 2010 at 7:24
void
at first; void
only really appeared with standard C. That meant there were a heck of a lot of people with old compilers who wanted to run modern C code, and #define void int
worked well enough to run some C90 code in K&R compilers.
Jan 4, 2010 at 22:37
Perl filehandle-style operator calls.
In the beginning, there was
print "foo", "bar", "baz"; # to stdout
print STDERR "foo", "bar", "baz";
Notice the ostentatious lack of a comma so that you know that's a filehandle to print-to, not a filehandle to print in a stringified manner. It's a dirty hack.
Language upgrade rolls around, they make proper OO filehandles and turn x FOO y, z, abc
into FOO->x(y, z, abc)
. Kinda cute. The same print statement effectively runs
STDERR->print("foo", "bar", "baz");
Mostly you notice this when you miss a comma, or try to run something like hashof $a, $b, $c
(subroutine call without parentheses) and forget to import the hashof
function into your namespace from its utility package, and you get a weird error message about "Can't call method 'hashof' via package 'contents of string $a
'".
IO::Handle
were created to make them more sensible -- except these objects don't work everywhere a real filehandle would. It's a good thing programmers don't need to work with files much.
Jan 31, 2010 at 7:42
In Python:
>>> a[0] = "hello"
NameError: name 'a' is not defined
>>> a[0:] = "hello"
NameError: name 'a' is not defined
>>> a = []
>>> a[0] = "hello"
IndexError: list assignment index out of range
>>> a[0:] = "hello"
>>> a
['h', 'e', 'l', 'l', 'o']
These slice assignments also give the same results:
a[:] = "hello"
a[42:] = "hello"
a[:33] = "hello"
Easy pickins, Erlang is full of them. For example, 3 forms of punctuation,
a_function(SomeVariable) ->
statements_end_with_commas(),
case PatternMatching of
0 -> now_we_end_with_semicolon;
true -> except_the_last_one
end.
%% Function definitions end with periods!
More of a platform feature than a language feature: on the iPhone, create an infinite loop with a few computations inside and run your program. Your phone will heat up and you can use it as a hand-warmer when it's cold outside.
C/C++:
The Fast Inverse Square Root algorithm takes advantage of the IEEE floating-point representation (code copied from Wikipedia):
float InvSqrt(float x)
{
union {
float f;
int i;
} tmp;
tmp.f = x;
tmp.i = 0x5f3759df - (tmp.i >> 1);
float y = tmp.f;
return y * (1.5f - 0.5f * x * y * y);
}
VBScript has so-called bracket identifiers, which are identifiers defined enclosed in square backets, like this:
[Foo]
They're quite handy, actually, as they allow you to name variables and routines after reserved words, call methods of third-party objects whose names are reserved words and also use almost any Unicode characters (including whitespace and special characters) in identifiers. But this also means that you can have some fun with them:
[2*2] = 5
[Здравствуй, мир!] = [Hello, world!]
[] = "Looks like my name is an empty string, isn't that cool?"
For[For[i=0]=[0]To[To[To[0]
[Next[To]([For[i=0])=[For[i=0]
Next
On the other hand, bracket identifiers can be a gotcha in case you forget the quotes in a statement like this:
If MyString = "[Something]" Then
because If MyString = [Something] Then
is a perfectly legal syntax. (And that's why an IDE with syntax highlighting is a must!)
More info on bracket identifiers in Eric Lippert's blog:
In C or C++, parentheses are optional for the argument to sizeof
... provided the argument isn't a type:
void foo() {
int int_inst;
// usual style - brackets ...
size_t a = sizeof(int);
size_t b = sizeof(int_inst);
size_t c = sizeof(99);
// but ...
size_t d = sizeof int_inst; // this is ok
// size_t e = sizeof int; // this is NOT ok
size_t f = sizeof 99; // this is also ok
}
I've never understood why this is!
In JavaScript (and Java I think) you can escape funny characters like this:
var mystring = "hello \"world\"";
If you want to put a carriage return into a string though, that's not possible. You have to use \n like so:
var mystring = "hello, \nworld";
That's all normal and expected- for a programming language anyway. The weird part is that you can also escape an actual carriage return like this:
var mystring = "hello, \
world";
"\r\n"
) newlines.
Jan 7, 2010 at 3:28
In earlier version of Visual Basic, functions without a "Return" statement just "Return None", without any kind of compiler warning (or error).
This lead to the most crazy debugging sessions back when I had to deal with this language on a daily basis.
String math in Perl is pretty weird.
$ perl -E '$string = "a"; $string++; say $string'
b
$ perl -E '$string = "abc"; $string++; say $string'
abd
$ perl -E '$string = "money"; $string++; say $string'
monez
$ perl -E '$string = "money"; $string--; say $string'
-1
In PowerShell, you can rename variables:
> $a = "some value"
> $b = "a"
> $c = "d"
> Rename-Item variable:$b $c
> $d
some value
Indirect indirection! Take that, PHP!
Literals work, too:
> Rename-Item variable:d e
> $e
some value
in PHP the strings letters cannot be used like in C, you need to use ord()
and chr()
in order to convert from number to char and vica versa: "a" != 97
, but ord("a") == 97
.
Although, there is one exception:
for ($i = 'a'; $i < 'z'; $i++) {
print "$i ";
}
will print letters a to y. just like you would expect as if it was C style datatypes.
however if the test condition is changed to <=
it will not print a to z as you would think, but a to yz! (total 676 items printed)
also if you change the 'z' to 'aa' which came out next after 'z' in the 676 items list, and change test condition to <
again, you will see only "a" being printed out! not a to z as you would expect.
And if you change the incrementor to $i+=2
it will print only "a" again! only way to do that is to use $i++
, $i++
in sequence, and now it works like expected.
Nevertheless, this is a nice way in PHP to output combinations of letters a-z, although its very hard to actually use it.
The bigest collection (today 1313) of decent and weird programming languages I know, you will find here: http://99-bottles-of-beer.net/ be prepared to see real weird stuff ;-) Everybody should make his one choice
Early FORTRAN where whitespace was not significant. (The anti-Python!)
DO 20 I = 1, 10
Meaning: loop from here to line 20 varying I from 1 to 10.
DO 20 I = 1. 10
Meaning: Assign 1.10 to the variable named DO20I.
Rumors are that this bug crashed a space probe.
Ruby
Time.parse
often pretends that the parsing did not fail, returns now
instead
require 'time'
Time.parse '2000-01-01 12:00:00'
# -> 2000-01-01 12:00:00 +0100
Time.parse '2000-99-01 00:00:00'
# -> ArgumentError: argument out of range ...
Time.parse 'now'
# -> 2010-08-13 21:26:13 +0200
Time.parse 'yesterday'
# -> 2010-08-13 21:26:18 +0200
Time.parse 'billion years ago'
# -> 2010-08-13 21:26:37 +0200
Java's access modifiers are a recent WTF to me (as I had to learn a bit of it).
Apparently packages are more intimate than class hierarchies. I can't define methods and attributes that are visible to sub-classes but not to other classes in the package. And why would I want to share the insides of a class to other classes?
But I can define attributes and methods that are visible to every class inside the package, but not to subclasses outside the package.
No matter how hard I think about this, I still can't see the logic. Switch over the access modifiers and make protected act like it works in C++ and keep the package private modifier as it is and it would make sense. Now it doesn't.