Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (lambda_expression) without bindings to locals.scm. #346

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rwjblue
Copy link

@rwjblue rwjblue commented Sep 7, 2023

When you have a lambda expression like:

val fruits = List("apple", "banana", "avocado", "papaya")

val countsToFruits = fruits.groupBy(fruit => fruit.count(_ == 'a'))

for ((count, fruits) <- countsToFruits) {
  println(s"with (fruits) 'a' × $count = $fruits")
}

The fruit => fruit.count(_ == 'a') lambda expression (note: without wrapping parenthesis) does not create a (binding) node. Its syntax tree is:

    (lambda_expression) ; [5:18 - 47]
     parameters: (identifier) ; [5:18 - 22]
     (call_expression) ; [5:27 - 47]
      function: (field_expression) ; [5:27 - 37]
       value: (identifier) ; [5:27 - 31]
       field: (identifier) ; [5:33 - 37]
      arguments: (arguments) ; [5:38 - 47]
       (infix_expression) ; [5:39 - 46]
        left: (wildcard) ; [5:39 - 39]
        operator: (operator_identifier) ; [5:41 - 42]
        right: (character_literal) ; [5:44 - 46]

That example just happens to be exactly one of the example from https://www.scala-lang.org/ (see playground) 😁.

When you have a lambda expression like:

```scala
val fruits = List("apple", "banana", "avocado", "papaya")

val countsToFruits = fruits.groupBy(fruit => fruit.count(_ == 'a'))

for ((count, fruits) <- countsToFruits) {
  println(s"with (fruits) 'a' × $count = $fruits")
}
```

The `fruit => fruit.count(_ == 'a')` lambda expression (note: without
wrapping parenthesis) does not create a `(binding)` node. Its syntax
tree is:

```
    (lambda_expression) ; [5:18 - 47]
     parameters: (identifier) ; [5:18 - 22]
     (call_expression) ; [5:27 - 47]
      function: (field_expression) ; [5:27 - 37]
       value: (identifier) ; [5:27 - 31]
       field: (identifier) ; [5:33 - 37]
      arguments: (arguments) ; [5:38 - 47]
       (infix_expression) ; [5:39 - 46]
        left: (wildcard) ; [5:39 - 39]
        operator: (operator_identifier) ; [5:41 - 42]
        right: (character_literal) ; [5:44 - 46]
```

That example just _happens_ to be **exactly** one of the example from
https://www.scala-lang.org/ ([see playground](https://scastie.scala-lang.org/S0bkCiXkQiuOMXnlhWn46g)) 😁.
Copy link
Collaborator

@ckipp01 ckipp01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @rwjblue! If I'm not mistaken can you try to add this to some of the hightlight query tests which should verify that this indeed is capturing the way we'd want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants