Random syntax tweaks!

Speaking of Rust, it allows the syntax that OP wanted:


if !(someVar > 20) // Don't need parenthesis because block statements require braces.
{
}

(In fact, if configured, it throws compiler warnings when you have unneccessary parenthesis.)

And macro_rules! is just awesome.

For last LD I used it to expand this:


tile!(1 Grass : TileGrass(0,1))

Into a full Tile class with texture coordinates etc., plus a static instance (Grass) and ID (1).

Keep in mind, though, guys, the more complex the syntax becomes, the harder it’s going to be for newbies to get into the language. That’s one of the problems of learning scala.

Here are a couple of different ways of printing out each String in an array:


val strings = "this is a string with different things in it".split(" ")

// Way 1:
for (str <- strings) Console.print(str)
// Way 2:
strings.foreach { str => Console.print(str) }
// Way 3:
strings.foreach((str) => {
  Console.print(str);
})
// Way 4:
strings.foreach((str) => Console.print(str))
// Way 5:
strings.foreach(Console.print(_))
// Way 6:
strings.foreach(Console.print)

Scala’s syntax is rich, that’s a plus on the one hand and a minus on the other.
It makes it harder to learn and sometimes you wonder even as experienced programmer why some syntax doesn’t work.

Personally, I’m all for a concise simple syntax, which is extensible. Lisp and it’s dialects are incredibly good at that. Concise, simple and very extensible. It shows the perfect combination of minimalism and extensibility :slight_smile:

There is a talk “Growing a Language” by a genious “Guy Steele”, who worked a lot with Lisp-languages and helped with designing a lot of languages, one of them being java. I really strongly recommend watching it completely :slight_smile:

A very simple one that I miss in java is the ?? operator from C#. If used at the right places it can make the code more concise without loosing any readability.

From http://msdn.microsoft.com/en-us/library/ms173224.aspx

Here is a silly example:


public class Foo
{
	private string _instance;

	public string GetInstance1(){
		if(_instance == null){
			_instance = "instance";
		}
		return _instance;
	}

	public string GetInstance2(){
		return _instance == null ? (_instance = "instance") : _instance;
	}

	public string GetInstance3(){
		return _instance ?? (_instance = "instance");
	}
}

This ?? operator just tries to hide a problem instead of fixing it. Getting rid of null and using something like a Option type is way better in my opinion.

Groovy has the ?: operator for nullifying nulls:

ifThisIsNull ?: useThisValueInstead

Actually it means if the first value evaluates to false (as null does) then use the second value.

One other feature I would like from Groovy is language level support for collections…

Exactly. It’s called the Billion Doller Mistake by the “creator” of [icode]null[/icode], Tony Hoare.

While I won’t comment on the issue of having null references at all (I don’t know enough about the issue to make an useful statement), we have to deal with null references anyway, no matter if null references are a good design decision or not. The coalesce operator can help shortening code (at least I have seen many places in production, where this operator was applied or could have been used to make classes morre concise). So while it might be here to counter bad design decision in our programming languages, I still find it a very useful small tool. I definitely can live without it in java, but I like getting rid of useless clutter. If there is a simpler way of expressing code that has a similar readability, I’m happy to use it, and this applies to ?? if you use it at the right places.

Dart has really nice constructors and methods with default values:


String method(String foo, {String bar:'default value', String ball:'bat'})

var variable, number, name;
SomeObject(this.variable, this.number, this.name);

nulls are awesome. They’re fail-fast and don’t have any additional overhead. If you want something different in a specific context you use a proxy/sentinel. When will language designers learn that it’s impossible to protect programmers from themselves.

And let’s not forget the wonderful Groovy safe dot operator:

thing?.field?.subField

which evaluates to null if thing or field are null, rather than giving an NPE. This feature of Groovy is indispensable in my day job (navigating large domain models).

I’ve always longed for being able to explicitly create pointers. I think technically every variable in Java is already a pointer, but it would be nice if we could reference and all that stuff like in C languages.

You can do something a bit like raw pointers with Unsafe: http://stackoverflow.com/questions/8820164/is-there-a-way-to-get-a-reference-address?lq=1
It’s not really worth attempting 99.9% of the time though.

I’d prefer a colon instead of semi-colon.


public void init(int width, int height): System.out.println(width, height);

I think this makes more sence.

With JDK8 you can effectively have a pointer to a method inside the language spec.

Multidimensional arrays!
int[,] stuff = new int[5,2];
or if that’s too hard for the JDK writers to cope with then
int[5,2] stuff = new int[];
would do.

Would love default arguments for constructors. Would save a ton of boilerplate coding.

Would be very nice to declare multiple groups of modifiers in declarations eg. static/private/transient etc. wrapped in {} which would save a ton of boilerplate coding too.

Ability to add “finally” to method declarations eg.


public void blah() {
...
} finally {
}

and lastly I’d like “not null” to be part of the language spec instead of just an optionally implemented @annotation and while we’re at it, proper design by contract. Bing!

Cas :slight_smile:

or instead of dealing with null, use option monad :smiley:

You could use other languages hides in the woods :persecutioncomplex:

The only situation I find hiding nulls is useful is if you have a scripting language that you want to be fault tolerant. Of course it does nothing to correct errors. Contracts can be very useful…too bad virtually nobody thinks so.

What about nested functions?


public List<Point> blah(int t) {

    // blahblah preconditions here

    List<Point> p = new ArrayList<>();
    for(int i = 0; i < t; i++)
        p.add(initPoint());

    Point initPoint() { // uses those preconditions, can't easily move to private method
        /*   */
    }
}

Yeah, it’s mostly for convenience, but they can clean up some code quite a bit, esp. if your code is so designed that you can’t really pull it out into a private method (which is kinda “ehh” anyway).


public List<Point> blah(int t) {

    // blahblah preconditions here

    final class $ { public Point initPoint() {
    	/*   */
    } } $ _ = new $();

    List<Point> p = new ArrayList<>();
    for(int i = 0; i < t; i++)
        p.add(_.initPoint());
}

Viewtiful :persecutioncomplex: