Compact method syntax #241

Closed
thekid opened this Issue Mar 24, 2012 · 2 comments

Comments

Projects
None yet
2 participants
Owner

thekid commented Mar 24, 2012

Scope of Change

A compact syntax for simple methods will be introduced

Rationale

Slightly less return $this.member; typing - especially in value objects.

Functionality

The -> expression; notation is used as a shorthand for { return expression; }.

public class Person {
  protected string $name;

  public string getName() -> $this.name;
}

// Equivalent of
public class Person {
  protected string $name;

  public string getName() {
    return $this.name;
  }
}

Combined with the compact assignment syntax described in RFC #240 and the fluent interface shorthand in RFC #252 we can now write value object as follows:

public class Person {
  protected string $name;

  public __construct($this.name= null) { }
  public void setName($this.name) { }
  public this withName($this.name) { }
  public string getName() -> $this.name;
}

Security considerations

n/a

Speed impact

None, resolved during compile time.

Dependencies

n/a

Related documents

  • Dart's "=> e; shorthand"
  • RFC #240 - Compact assignment syntax
  • XP Language's anonymous functions use this (slightly different, e.g. in return #{ $a -> $a + 1 }.(2); // 3 it separates the arguments from the body, but this still has chances for recognition).
Member

mrosoiu commented Mar 26, 2012

+1 Cool!

Owner

thekid commented Apr 7, 2012

After implementing lambda support in thekid/xp-js, I think we should really align their syntax and this one better. Following the idea that -> X is a shorthand for { return X }, the long form of lambdas should be changed and the -> removed from it, as now it feels kind of duplicate here.

Old form

$func= #{ $a -> $a * $mul };
$func= #{ $a -> { 
  return $a * $mul; 
}};

New form

$func= #{ $a -> $a * $mul };
$func= #{ ($a) { 
  return $a * $mul;
}};

...or maybe even omit the enclosing {} around the long form:

$func= #{ $a -> $a * $mul };
$func= #($a) { 
  return $a * $mul;
};

@thekid thekid added a commit to thekid/compiler that referenced this issue Apr 25, 2012

@thekid thekid - Implement compact method syntax, "-> <e>" as shorthand for "{ retur…
…n <e> }"

# See xp-framework/rfc#241
6fb2301

thekid referenced this issue in xp-lang/compiler Apr 26, 2012

Merged

Compact syntax #15

thekid closed this May 5, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment