2

I'm not too deep in Gosu, so this code confused me. I've googled, etc., but to no avail. What are the backslashes doing in this snippet?

GET_STUFF.execute(\ -> {
      var gig = withPackagedGig(request.id, request.signature, \ k -> {
        k.addItem("Log_RetrievedGig");
        return k;
      });
      myObj = toObj(gig);
    })
Michael
  • 41,989
  • 11
  • 82
  • 128
Jonathan M
  • 17,145
  • 9
  • 58
  • 91
  • Possible duplicate of [Line continuation characters in JavaScript](https://stackoverflow.com/questions/10514873/line-continuation-characters-in-javascript) – the_storyteller Feb 26 '19 at 15:19
  • 10
    It doesn't look like a java code – Limmy Feb 26 '19 at 15:20
  • 1
    @the_storyteller, but there wouldn't be characters *after* the line continuation character, would there? – Jonathan M Feb 26 '19 at 15:20
  • Yeah, this wouldn't compile in java 8 (obviously not 8, `var`), but I'm not sure the backslash would work here as intended in pure java. It would qualify as an illegal character – Rogue Feb 26 '19 at 15:20
  • 2
    This is not valid Java syntax in any Java version so far. Something else must be processing it before the compiler and using the backslashes to escape the spaces for some reason. – Michael Feb 26 '19 at 15:21
  • I misread the question, thinking it was Javascript. That _is_ valid Javascript. – the_storyteller Feb 26 '19 at 15:21
  • @Michael, good to know. I'll go back to the person that provided it. Thanks. – Jonathan M Feb 26 '19 at 15:21
  • @the_storyteller, good to know. What would the backslashes do in this code if it were javascript? – Jonathan M Feb 26 '19 at 15:22
  • @JonathanM Look at question the_storyteller linked. – Malt Feb 26 '19 at 15:23
  • @JonathanM, then it would be a line continuation character (see possible duplicate answer.) It isn't necessary for line continuation, but is valid syntax. – the_storyteller Feb 26 '19 at 15:23
  • @the_storyteller, ah, yes, I thought you were referring to a different use. It definitely could be line continuation in js, but it wouldn't make sense for there to be characters after the slash in that case. I'm guessing this is somehow corrupted Java (as the provider said it was Java, and it has typed variables elsewhere, along with annotations). – Jonathan M Feb 26 '19 at 15:25
  • Thanks for your help, all. – Jonathan M Feb 26 '19 at 15:26
  • Which version of Java? The use of `var` means it's either Java 10 (very new), or it's Javascript. – the_storyteller Feb 26 '19 at 15:26
  • @the_storyteller, I'll have to get that from the provider. – Jonathan M Feb 26 '19 at 15:27
  • 1
    As you all suspected, it's not pure Java, but a custom variant called Gosu: https://gosu-lang.github.io/. I've updated the tags. – Jonathan M Feb 26 '19 at 15:30
  • 2
    Sorry for the wild goose chase. – Jonathan M Feb 26 '19 at 15:30

2 Answers2

6

The backslash is just part of the syntax of a block.

\ -> { is equivalent to Java's () -> { - a lambda taking no parameters.

\ k -> { is equivalent to Java's k -> { - a lambda taking one parameter whose type is inferred.

Michael
  • 41,989
  • 11
  • 82
  • 128
3

This question was originally posted when thinking the code was Java. Upon discovering it is Gosu, the answer is given by the docs.

Lambdas in Gosu are set off by the backslash.

Jonathan M
  • 17,145
  • 9
  • 58
  • 91