« Return to Thread: idea: val x, y {... x = ... y = ...}

idea: val x, y {... x = ... y = ...}

by Rob Dickens-2 :: Rate this Message:

Reply to Author | View in Thread

Dear Martin and All,

One good thing Java still has going for it IMO is its ability, thanks to its 'blank finals', always to let you confine the scope of local variables to where they're used - thus resulting in more maintainable code.

For example,

void javaVersion() {
  final int x, y, z; {
    final int a = 1 + 2;
    x = a + 1;
    final int b = 3 + 4;
    y = a + b;
    z = a - b;
  }
  point(x, y, z);
}

To my knowledge, this is currently best translated as,

def scalaVersion() {
  val (x, y, z) = {
    val a = 1 + 2
    val _x = a + 1
    val b = 3 + 4
    (_x, a + b, a - b)
  }
  point(x, y, z)
}

Note the requirement for the shadow val, _x.

Assuming it were feasible to support, and imagining cases with more complex expressions, wouldn't the following syntax be more agreeable?

def scalaxVersion() {
  val x, y, z {
    val a = 1 + 2
    x = a + 1
    val b = 3 + 4
    y = a + b
    z = a - b
  }
  point(x, y, z)
}

--
Rob, Lafros.com

 « Return to Thread: idea: val x, y {... x = ... y = ...}