Most unusual/weird syntax features in non-joke languages

Like in topic - what are the weirdest or very unusual syntax features you saw in other languages than Java? Please post features only from “real” languages (no Malborge, Shakespeare etc.).

I will start with this code snippet from Haxe:


class Vec3 {

	public var x : Int;
	public var y : Int;
	public var z : Int;
	
	public function new(?x : Int, ?y : Int, ?z : Int) {
		this.x = (x == null) ? 0 : x;
		this.y = (y == null) ? 0 : y;
		this.z = (z == null) ? 0 : z;
	}
	
}

Haxe don’t support method and constructor overloading, but what we see there is something similar - in this case this method can be invoked without parameters or with 1, 2 or 3 parameters, like this:


		new Vec3(); // (0, 0, 0)
		new Vec3(1); // (1, 0, 0)
		new Vec3(1, 2); // (1, 2, 0)
		new Vec3(1, 2, 3); // (1, 2, 3)

I always though various function pointer syntax in C was whack.

// Function takes a char and returns a pointer to a
// function which is taking two floats and returns a float.
float (*GetPtr1(const char opCode))(float, float)
{

}

via

I’d start with C, and arrays. Since arrays are decayed to pointers before compilation, this syntax is very valid.


printf("%d", 5[a]);  // Where 'a' is an array

My next one is from Javascript:


console.log('5' + 3);  // 53
console.log('5' - 3);  // 2

This one is really weird, and I consider this as an undefined behaviour. See this for proof in chrome console:

The first one is because C has no concept of arrays outside of the lexer/parser, the indexing operator is syntax sugar:

a[b] -> *(a + b) // indexing is just some pointer addition, which is commutative, so the reverse is equivalent:
b[a] -> *(b + a)

The JS madness is due to String -> number coercion in the second case. In Java:


System.out.println("4" + 4); // 44  just regular concatenation due to '+' operator overloading
System.out.println("4" - 4); // ERR: The operator - is undefined for the argument type(s) String, int

// so JS silently inserts a "cast":
System.out.println(Integer.parseInt("4") - 4); // 0

This is because JS and some other languages (often in the web arena) seem to value “don’t crash, just keep running no matter what” and is not statically typed/etc, and also like super weak typing where one could have such a preposterous thing as String to number coercion.
Of course both langs have number to String coercion, which seems to be more common. (although still a bit weird in the context of an otherwise strongly-typed lang)

And this one is in Java. I’m sure most of the people don’t know this.


int[] numbers()
{
    return null;
}

is the same as


int numbers() []
{
    return null;
}

The same applies to variable decls:


int[] nums;
int nums[];

Similar to the C-ism pointer heathenry:


int* ptr;
int *ptr;

The 2nd way is unfortunately the standard practice because of dumb lexer rules regarding multiple decls on the same line:


int* var1, var2;  // var1 is a pointer, var2 is an int! AAAAAH

They should have really made both a pointers. Really. Stumbled upon that a lot of times while in lab.

Also some more things I’ve seen: First in PHP, the function names (and also class names) aren’t case sensitive, but variables are.


function add($a, $b)
{
    return $a + $b;
}

$foo = add(1, 2);
$Foo = Add(3, 4);

echo "foo is $foo"; // outputs foo is 3
echo "Foo is $Foo"; // outputs Foo is 7

And now in Python 2.x, you can make a True a False with an assignment.


>>>True = False
>>>True
False

This is no longer in Python 3, now that creates a syntax error. I was almost scared when I found that in a blog post some time ago.

A lot of rules in PHP are really beyond logic, so I’m actually not surprised.

The python isn’t really syntactical, but a semantic quirk.

And the threadkiller where these are coming from: http://stackoverflow.com/questions/1995113/strangest-language-feature
(although not all are syntactical)

So you found it! You made me :-X (mute)

;D

I’ve seen it before.

Am I the only one who giggled the very first time I instantiated a class:

Tree tree = new Tree();

“TREE TREE TREE. TREE!!!” and I finally get a tree ;D

I find most macro-languages’ syntax to be nasty, but then I’m spoiled by having experienced lisp. (although the lisp macro punctuation is often a bit noisy)

The latest is Rust’s which I’m diving into as the language becomes increasingly stable and wonderful (1.0 on the 15th!) : http://static.rust-lang.org/doc/master/book/macros.html

In Java the following infamous snippet compiles.


class Driver {  
        public static void main(String args[]) {
           http://www.github.com
           System.out.println("Hello, World!");
        }
}

“http:” is being treat as a label and the “//” starts a single line comment.

And coming from imperative languages, a lot of the syntax in Haskell is weird or unusual to me. But in a cool way! First “unusual” thing that comes to mind is how you can call a function in infix position.

let plus x y = x + y

Calling the function in prefix position would be written as

plus 5 5

. The unusual (and awesome!) part is that Haskell also allows you to write it like this, and achieve the same result:

5 `plus` 5

The next logical step: why even mention arguments? :smiley:


Prelude> let plus = (+)
Prelude> plus 4 5
9

With some other tricks you eventually reach madness: https://wiki.haskell.org/Pointfree

What really shows Haskell has some neat stuff in the area of functions/operators is when you can do stuff like this: ;D


Prelude> (+) 5 5
10

Or…


Prelude> let addFive = (+) 5
Preulde> addFive 10
15

It’s actually all quite commonplace in functional langs, but currying/partials/implicit-args/operators-are-just-functions/etc do appear magical at first to the uninitiated.

But one must approach with caution: http://www.willamette.edu/~fruehr/haskell/evolution.html :stuck_out_tongue:

How do I the opposite of medal you for showing me that nightmare?

We need some sort of “throw steaming turd” button next to “appreciate” ;D

Cas :slight_smile:

Offtopic: SMF has negative karma. ChrisM enabled it for a brief period and people got pissed off, aggresive, spiteful, insecure, etc. etc. Did you ever see a thriving social network featuring negative feedback? (StackOverflow doesn’t count, as it’s not much of a community, more of an ego-boosting KB)

Ontopic: