Skip to content

Instantly share code, notes, and snippets.

@samuelgoto
Last active November 29, 2017 22:24
Show Gist options
  • Save samuelgoto/84b6f34be7e64a83cf0dd3282d3a4d1a to your computer and use it in GitHub Desktop.
Save samuelgoto/84b6f34be7e64a83cf0dd3282d3a4d1a to your computer and use it in GitHub Desktop.

Heavily inspired by kotlin and groovy, what would the following syntax simplication enable?

a {}

// syntact sugar for

a(function() {});

?

@samuelgoto
Copy link
Author

samuelgoto commented Oct 30, 2017

Kotlin disallows break and continue inside blocks:

fun unless(condition: Boolean, block: () -> Unit) {
  if (condition) {
    block()
  }
}

fun main(args: Array<String>) {
  println("Hello, world!")
  while (true) {
    unless(true) {
      println("hello")
      // Error:(11, 12) 'break' or 'continue' jumps 
      // across a function or a class boundary
      // break

      // 'return' is not allowed here
      // return
    }
  }
}

@samuelgoto
Copy link
Author

fun case(condition: Boolean, block: () -> Unit) {
    println("This gets called!")
    block()
}

fun select(condition: Boolean, block: () -> Unit) {
  block()
}

fun main(args: Array<String>) {
  var expr: Boolean = true;
    
  select (expr) {
    case (true) {
        println("Totally true")
    }
    case (true) {
        println("Totally false")
    }
  }
}

@samuelgoto
Copy link
Author

samuelgoto commented Nov 29, 2017

Interesting use case in constructors for Ruby:

https://mixandgo.com/blog/mastering-ruby-blocks-in-less-than-5-minutes

let car = new Car() {
  ::color = 1;
  ::size = "large";
}

Could that lead to something like abstract classes?

sort([2, 3, 1, 4], new Comparator() {
  ::compare do (a, b) {
    return a < b;
  }
})

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