Proposal for {}-expressions in JavaFX

I have wrote a proposal to send it to openjfx-compiler project… But I found that some of features I propose is already implemented in the language… see upcoming post. I feel my self as Gray being overtaking by Bell😦 But positive side is that I am on the same page with people creating this language🙂
Anyway that is what I wanted to sent to openjfx-compiler mail.list:

Proposal to have {} to evaluate to some value.

I was inspired for this idea by if-then-else construct.
As far as I remember it was the old ALGOL-60 language which enables “IF cond THEN v1 ELSE v2” first, and that time it looks exactly the same syntax.
However I think it would only look OK in Pascal or Modula-2, but not in JavaFX.
Because JavaFX tends to be expression language I propose to have { } construct to evaluate to some value. How it could help?
1. if-then-else will not be needed any longer one could write:

	var result = if (operation = "-") {x-y} else {x+y}
	or even better example:
	var result = if (operation = "-") {x-y} else if (operation = "+") {x + y} else if (operation = "*") {x * y} else if (operation ="/") { if(y == 0) {throw DivisionByZeroException{};} else {x/y}};

2. we could write as simple as (w/o return):

	function mediana(a: Integer, b:Integer) {
		(a + b) / 2 // NOTE: semicolon (;) to be ommited
	}

3. we could put some additional computation in { } only the last one operator(expression) in { } will be a result expression.

	/**
	 * Overflow protected version of mediana
	 */ 	
	function mediana(a: Integer, b:Integer) {
		var sumOfHalves = a/2 + b/2;
		var remainders = a%2 + b%2;
		sumOfHalves + remainders
	}

4. I have some ideas about JavaFX variant of servlets/JSP. Look! how pretty could look {}-expression in the strings, it could substitute both JSP’s expression (<%= %>) and JSP’s scriptlet (<% %>):

	<%
		String title = request.getAttribute("title");
	%>
	<html>
<%
	if ( (!"".equals(title) {
%>
		<title><%= escape(title) %></title>
<%
	}
	else {
%>
		<title>Home Page</title>
<%

	}
%>
	...

-- BECOMES --
	out.write("<html>
		<title>{
			var title = request.getAttribute("title");
			if ("" <> title) {
				"<title>{escape(title)}</title>" //NOTE: this "if" is an "if" between 2 string expressions!
			}
			else {
				"<title>Home Page</title>"
			} 
		}</title>
	...

	");

5. the only exception where there should not be {}-expression is finally {}-block of try-catch-finally:

	transactionOK = false;
	try {
		...
		...
		transactionOK = true;
		someValue // some expression
	}
	catch(SomeExceptionToHandle e) {
		transactionOK = true;
		someOtherValue // some expression
	}
	catch(SomeCriticalExpection e) {
		throw AnotherException{};	
	}
        finally {
		if (treansactionOK) {
			t.commit();
		}
		else {
			t.rolback();
		}
		// here expression may not be set
	}

6. (OPTIONALY) for cycles for and while, evaluation of {}-block may automaically create sequence:

	var primesUpTo100:Integer[] = while(x <= 100) {
		if (isPrime(x)) {
			x++
		}
		else {
			x++;
			null
		}
	}

-- OR LESS TRICKY --
	var primesUpTo100 = while(x <= 100) {
		if (isPrime(x)) {
			var prime = x;
			x = x + 1;
			prime
		}
		else {
			x = x + 1;
			null
		}
	}

7. The similair feature exists in C language: it is comma operator (,) however its idea is vice-versa — to unstructurize code and write everithing in one line: y = (x=1, z=2, x*z);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: