Tutorial :Strangest language feature



Question:

What is, in your opinion, the most surprising, weird, strange or really "WTF" language feature you have encountered?

Please only one feature per answer.


Solution:1

This is nothing strange or surprising, but it is something that made me always say WTF:

Case sensitivity in syntax, or in identifier names.

Most languages that have it just seem to have it because C has it. There is no good reason for it.


Solution:2

In SQL Server you may end up with a nasty surprise if you use select * in your production code. Using select * is not considered as a good practice anyway but it is good to know of some of the interesting behaviour.

See question “select * from table” vs “select colA,colB,etc from table” interesting behaviour in SqlServer2005 for more details


Solution:3

Haskell's use of Maybe and Just. Maybe a is a type constructor that returns a type of Just a, but Maybe Int won't accept just an Int, it requires it to be a Just Int or Nothing. So in essence in haskell parlance Just Int is about as much of an Int as an apple is an orange. The only connection is that Just 5 returns a type of Maybe Interger, which can be constructed with the function Just and an Integer argument. This makes sense but is about as hard to explain as it can theoretically be, which is the purpose of haskell right? So is Just really JustKindaLikeButNotAtAll yea sorta, and is Maybe really a KindaLooksLikeOrIsNothing, yea sorta again.

-- Create a function that returns a Maybe Int, and return a 5, which know is definitly Int'able  >  let x :: Maybe Int; x = 5;  <interactive>:1:24:      No instance for (Num (Maybe Int))        arising from the literal `5' at <interactive>:1:24      Possible fix: add an instance declaration for (Num (Maybe Int))      In the expression: 5      In the definition of `x': x = 5    >  Just 5    Just 5  it :: Maybe Integer        -- Create a function x which takes an Int  >  let x :: Int -> Int; x _ = 0;  x :: Int -> Int  -- Try to give it a Just Int  >  x $ Just 5                       <interactive>:1:4:      Couldn't match expected type `Int' against inferred type `Maybe t'      In the second argument of `($)', namely `Just 5'      In the expression: x $ Just 5      In the definition of `it': it = x $ Just 5  

Good luck reading this, I hope its right.


Solution:4

In MySQL string comparisons are case-insensitive.

> SELECT * FROM blah WHERE foo = 'BAR';  > SELECT * FROM blah WHERE foo = 'Bar';  > SELECT * FROM blah WHERE foo = 'bAr';  

Are all equivelent. Not only that they will match any value of foo that looks like 'bar' (e.g. if foo = 'bar' it will match for BAR, baR, bAR, etc.).


Solution:5

Not so long ago, when I first descoverd the C Language in my CS class, it was very strange to see the way pointers behaived. we just wrote programs and guess what it would do, until they get the right behavior


Solution:6

The C preprocessor and its usages. Specifically preprocessor metaprogramming and using the preprocessor to generate portable code -- total mindfcuk.


Solution:7

Labeled break and continue statements in Java..

They allow you to break out of multiple control-blocks with a single break.


Solution:8

C# has a feature called "extension methods", which are roughly analogous to Ruby mix-ins - Essentially, you can add a method to any pre-existing class definition (for instance, you oould add "reverse()" to String if you were so inclined). That alone is fine- The "Weird" part is that you can add these extension methods, with a method body and everything, to an interface. On the one hand, this can be handy as a way to add a single method to a whole swath of classes which aren't part of the same inheritance tree. On the other, you're adding fleshed out methods to interfaces, essentially breaking the very definition of an interface.


Solution:9

Python's ternary operator

In Python, the C ternary operator (C++ example: bool isNegative = i < 0 ? true : false;) is available as syntactic sugar:

>>> i = 1  >>> "It is positive" if i >= 0 else "It is negative!"  'It is positive'  >>> i = -1  >>> "It is positive" if i >= 0 else "It is negative!"  'It is negative!'  

It's not really strange but a feature. The odd thing is the changed order (A if CONDITION else B) in comparison to the (IMO more logical) order in C (CONDITION ? A : B).


Solution:10

In C, a[b][c] is exactly the same thing as c[b[a]].


Solution:11

In PHP, you have to explicitly reference globals and explicitly use this-> for class variables. Makes refactoring fun. You cannot promote a variable/argument to a global or a class member without finding all points of usage.


Solution:12

Perl

my %h1 = map { $_ => 1 } qw/foo bar baz/;    // construct an 'is_member' type lookup table  my %h2 = map { "$_" => 1 } qw/foo bar baz/;  

the second line is a syntax error even though to even an experienced perl programmer it looks like it would be identical. The downside to perl always trying to do what you mean, not what you said.


Solution:13

this made me stunning

#define _ -F<00||--F-OO--;  int F=00,OO=00;main(){F_OO();printf("%1.3f\n",4.*-F/OO/OO);}F_OO()  {              _-_-_-_         _-_-_-_-_-_-_-_-_      _-_-_-_-_-_-_-_-_-_-_-_    _-_-_-_-_-_-_-_-_-_-_-_-_-_   _-_-_-_-_-_-_-_-_-_-_-_-_-_-_   _-_-_-_-_-_-_-_-_-_-_-_-_-_-_  -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_  _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_  _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_  _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_   _-_-_-_-_-_-_-_-_-_-_-_-_-_-_   _-_-_-_-_-_-_-_-_-_-_-_-_-_-_    _-_-_-_-_-_-_-_-_-_-_-_-_-_      _-_-_-_-_-_-_-_-_-_-_-_         _-_-_-_-_-_-_-_             _-_-_-_  }  


Solution:14

Smalltalk:

Have a class method in a class Test, that returns a constant string:

method1      ^ 'niko'  

You should expect that this method constantly returns the string 'niko' whatever happens. But that is not the case.

s := Test method1   

(Set s to 'niko'.)

s at: 4 put: $i.  

(Set s to 'niki'.)

s := Test method1  

(Set s to 'niki' again.)

So, what happens is that the second line of code permanently changed method1 to return 'niki' rather than 'niko', even though the source code of the method was not updated.


Solution:15

Try, except, else

try:     pass  except:  pass  else:    pass  finally: pass  

If no exception was caught the else part is executed.

Makes sense, but at first I really hadn't any clue what it does.

Example:

def show_square(string):    try:      n = int(string, 10)    except ValueError:      print "I can't do that, Dave."    else:      print n * n  


Solution:16

shift;

sometimes you see it in the very first line of a perl method to get read of self pointer


Solution:17

In ColdFusion

Struct (aka Java HashMap) is passed by reference.

You'd have thought other data type behaves like Java...

Array is passed by value, wtf!

List is just a plain old comma-separated string!


Solution:18

In Unity,

GameObject.Find("MyObject")  

will return your object normally. However, if you do something like this:

GameObject.Find("MyObject").active = false;  //do other stuff  if (GameObject.Find("MyObject").active)  {      //do stuff  }  

Then you will get a null reference. In Unity iPhone, this code will often work fine in the editor but will cause a SIGBUS when running from the iPhone. The problem is that GameObject.Find() will only locate active objects, so even if you're just checking to see if it's active, you are effectively calling if (null.active) .

To make it work right, you've got to store it prior to making it inactive.

GameObject obj = GameObject.Find("MyObject");  obj.active = false;  //do other stuff  if (obj.active)  {      //do stuff  }  

Arguably that's better practice anyway, but the way Unity treats inactive objects in general is quite weird. It appears to unload a large portion of the inactive object (textures, etc.) but not all of it, so inactive objects can still eat up a lot of memory.


Solution:19

In J, most primitives (a.k.a. functions) are monadic (one argument) or dyadic (two arguments, one to the left, one to the right). But the amend primitive takes 3 (I think it's the only one, besides foreigns). It's understandable that it would take 3, but it just looks... wrong at first.

vector =: i. 10   NB. Vector will be 0 1 2 3 4 5 6 7 8 9  (10) (0) } vector NB. Will yield 10 1 2 3 4 5 6 7 8 9  


Solution:20

In awk, arrays start at index 1, which is confusing the least.


Solution:21

In Python, at least for me, this was very wft! the first time I saw it:

>>> "ja " * 5  'ja ja ja ja ja '  

You can multiply strings! WTF??

PS: I think this is because x * n means: n times x so, 5 times "ja " is "ja ""ja ""ja ""ja ""ja " and because you can concatenate strings like this:

>>> "ja ""ja ""ja ""ja ""ja "  'ja ja ja ja ja '  

That two codes have the same result (and maybe are just the same)


Solution:22

The implict variables\constants and mutable constants in ruby


Solution:23

What datatype is foo?

SELECT TOP 1     NULL AS foo  INTO     dbo.bar  FROM     sys.columns --trivial  

Why does everything go to zero?

SELECT CAST('' AS int), CAST('' AS datetime), CAST('' AS float)  

...except this

SELECT CAST('' AS decimal)  


Solution:24

In C#, the following code generates compiler error "Cannot convert from method group to Delegate". Though the logic behind is reasonable, it still feels strange to me.

control.BeginInvoke(delegate { DoSomething(); });  


Solution:25

A Java source file can end with the character \u001a (control-Z).


Solution:26

In Java, there is some inconsistency as to how Strings handle the == operator depending on how it was constructed.

String a = "Hello";  String b = "Hello";  System.out.println(a == b ); // prints true.  String c = new String("Hello");  String d = new String("Hello");   System.out.println(c == d ); // prints false  


Solution:27

Here is one about python:

>>> print 07  7  >>> print 08    File "<stdin>", line 1      print 08             ^  SyntaxError: invalid token  

Ain't that a beauty?

Especially unthoughtful when you think of how human write dates, which has the following effect:

datetime.date(2010,02,07) # ok  datetime.date(2010,02,08) # error!  

(the reason is that 0x is interpreted as octal, so print 010 prints 8!)


Solution:28

In Scheme there are no reserved identifiers. So, the following expression evaluates to 1:

((lambda (lambda) lambda) (let ((let 1)) let))  

Note that there is a restriction on definitions within a given scope: no definition may redefine an identifier used to define identifiers within that scope, so the following is a syntax error:

(begin (define define 1) define)  


Solution:29

Well the first thing that came to my mind was 'noop', my brain did the same thing when I first came across it!


Solution:30

In J, foreigns (!:) are various functions bunched together. The left argument is a category, where as the right are often (but not always) incremental values for different... stuff. For example:

      2!:55 NB. Close console      9!:10 NB. Set print precision      6!:0  NB. Actual time      6!:2  NB. Execution time      4!:3  NB. Loaded scripts  

Of course, the smart thing is to wrap them, but some you just commit to memory. BTW, all of those are, come to think of it, triadic, with 2 arguments to the right and one to the left. None of the above will work unless you give them a final valid argument.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »