# Xojo & Closures

**URL:** <https://ifnotnil.com/t/xojo-closures/704>\
**Category:** General Programming\
**Created:** [5 July 2020 17:05 UTC](https://ifnotnil.com/t/xojo-closures/704 "2020-07-05T17:05:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Garry](https://ifnotnil.com/user_avatar/ifnotnil.com/garry/32/2_2.png) [@Garry](https://ifnotnil.com/u/Garry)\
**Post date:** [5 July 2020 17:05 UTC](https://ifnotnil.com/t/xojo-closures/704/1 "2020-07-05T17:05:01Z")

</div>

Am I right in thinking that Xojo doesn’t support closures?

I ask because I am currently neck deep in my compiler trying to implement them and it suddenly occurred to me that I might not need to bother. After all, Xojo as a language has done pretty well without them.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [5 July 2020 17:31 UTC](https://ifnotnil.com/t/xojo-closures/704/2 "2020-07-05T17:31:27Z")

</div>

It does not 🙂

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [5 July 2020 17:41 UTC](https://ifnotnil.com/t/xojo-closures/704/3 "2020-07-05T17:41:21Z")

</div>

Swift does, and I’m still trying to wrap my head around where/why and how to use them.

---

<div class="post-metadata">

**Author:** ![Garry](https://ifnotnil.com/user_avatar/ifnotnil.com/garry/32/2_2.png) [@Garry](https://ifnotnil.com/u/Garry)\
**Post date:** [5 July 2020 17:43 UTC](https://ifnotnil.com/t/xojo-closures/704/4 "2020-07-05T17:43:43Z")

</div>

> [@npalardy](#):
>
> It does not 🙂

Thought so.

> [@DaveS](#):
>
> Swift does, and I’m still trying to wrap my head around where/why and how to use them.

Ditto. Lots of languages do use them but anything you can do with a closure you can do using classes. There are times when it’s more succinct to use them but personally I don’t ever use them.

If I understand correctly, they are essentially functions declared within other functions that capture local variables in the scope of their enclosing function.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [5 July 2020 18:08 UTC](https://ifnotnil.com/t/xojo-closures/704/5 "2020-07-05T18:08:13Z")

</div>

Java UI used them a lot basically to implement the events  
They call them anonymous functions

---

<div class="post-metadata">

**Author:** ![Rick.A](https://ifnotnil.com/user_avatar/ifnotnil.com/rick.a/32/217_2.png) [@Rick.A](https://ifnotnil.com/u/Rick.A)\
**Post date:** [7 July 2020 18:35 UTC](https://ifnotnil.com/t/xojo-closures/704/6 "2020-07-07T18:35:25Z")

</div>

Lambdas or Anonymous functions are a kind of function used as parameter for a high order function. A Closure is a function with “bound” variable values that can be passed around. A Closure is a Lambda, a Lambda is not necessarily a Closure (need to have bound values).  
So in a pseudo-Xojo, you could do things like:

```
Function MyFun( f As Function(x As Integer, y As Integer) As Double, s As String)
  Msgbox s + f(x,y).ToString
End

MyFun( (10, 20) (
  Dim n As Integer = x*y
  Dim r As Double = n / 10
  Return r ), "This is x*y/10")

MyFun( (11, 21) (
  Dim n As Integer = x+y
  Dim r As Double = n / 11
  Return r ), "This is x+y/11")
```

---

<div class="post-metadata">

**Author:** ![Rick.A](https://ifnotnil.com/user_avatar/ifnotnil.com/rick.a/32/217_2.png) [@Rick.A](https://ifnotnil.com/u/Rick.A)\
**Post date:** [7 July 2020 19:01 UTC](https://ifnotnil.com/t/xojo-closures/704/7 "2020-07-07T19:01:20Z")

</div>

```
Function GetAClosure(lim As Integer) As Function // lim will be bound to the resulting function
  Return Function(x As Integer -> (If x<lim Then Msgbox "true")) // Function() is value, Function()() is call
End

Dim LessT10 As Function = GetAClosure(10)
Dim LessT20 As Function = GetAClosure(20)

LessT10(5) // true
LessT10(10) 
LessT10(15) 
LessT10(20)
LessT10(25)
LessT20(5) // true
LessT20(10) // true
LessT20(15) // true
LessT20(20) 
LessT20(25)
```

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [7 July 2020 21:47 UTC](https://ifnotnil.com/t/xojo-closures/704/8 "2020-07-07T21:47:26Z")

</div>

Cant say that I’ve really missed them (some folks - brock for instance) would have you believe that “OMG you cannot possibly write decent code without them !” 😛

Would they be handy - for some things sure

Generics would be something I’d rather see implemented before closures and lambdas

---

<div class="post-metadata">

**Author:** ![Garry](https://ifnotnil.com/user_avatar/ifnotnil.com/garry/32/2_2.png) [@Garry](https://ifnotnil.com/u/Garry)\
**Post date:** [7 July 2020 21:54 UTC](https://ifnotnil.com/t/xojo-closures/704/9 "2020-07-07T21:54:30Z")

</div>

Thanks for the further clarification @Rick.A. I’ve decided against implementing them in my language - honestly it mostly because I never use them and I can’t be arsed to rip up the pretty fast stack-based local variable lookup my VM currently uses to shoehorn in heap-managed locals which closures would necessitate.

---

<div class="post-metadata">

**Author:** ![Rick.A](https://ifnotnil.com/user_avatar/ifnotnil.com/rick.a/32/217_2.png) [@Rick.A](https://ifnotnil.com/u/Rick.A)\
**Post date:** [8 July 2020 00:27 UTC](https://ifnotnil.com/t/xojo-closures/704/10 "2020-07-08T00:27:54Z")

</div>

It is very useful for some contexts. You can think as storing a function in a variable and passing around to be used by some other process. Imagine that you have a task like sorting things in some unusual way besides ascending and descending in alphabetic order, so instead of writing many sorting functions, you can write a “generic” function to sort data and one parameter is a function to classify if a\<b in some unusual way. You could store one way as Dim sortAsc As Function… Dim sortDesc As Function… Dim sortInsane As Function… and pass those “names” as a parameter to the sort routine. Small expressions can be written just in place like myArray.sort((x,y)-\>(x\<y))

---

<div class="post-metadata">

**Author:** ![Rick.A](https://ifnotnil.com/user_avatar/ifnotnil.com/rick.a/32/217_2.png) [@Rick.A](https://ifnotnil.com/u/Rick.A)\
**Post date:** [8 July 2020 00:31 UTC](https://ifnotnil.com/t/xojo-closures/704/11 "2020-07-08T00:31:23Z")

</div>

I agree that in the list of Xojo desired missing features, Generics takes a bit more precedence.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [8 July 2020 00:44 UTC](https://ifnotnil.com/t/xojo-closures/704/12 "2020-07-08T00:44:30Z")

</div>

Not holding my breath about either 🙂

---

<div class="post-metadata">

**Author:** ![Garry](https://ifnotnil.com/user_avatar/ifnotnil.com/garry/32/2_2.png) [@Garry](https://ifnotnil.com/u/Garry)\
**Post date:** [8 July 2020 06:46 UTC](https://ifnotnil.com/t/xojo-closures/704/13 "2020-07-08T06:46:21Z")

</div>

I see the utility. I’ll think about it. Need to stop adding features get 1.0 of the compiler done though. As a dynamic language, Possum supports generics.
