A new vision of local functions
- Dominant language
- No language data
- Stars
- 328
- Forks
- 71
- PR merge metrics
- No merged PRs in 30d
Description
I suggest to use this pattern as an alternative to local functions:
```VB.NET
Class Class1
#Domain FuncGroup1
Dim x as Integer
Dim y as Integer
Out Sub Foo1()
Dim z = 1
x = 1
Call Foo2(z)
End Sub
Sub Foo2(z as integer)
Dim x = 10
y +=1
Console.WriteLine(x + FuncGroup1.x + y + z)
End Sub
#End Domain
Sub Foo3()
Call Foo1()
End Sub
End Class
```
Notes:
1. We can define variables local to the domain, and they are seen only from any block inside the domain.
2. If there is a var with the same name in the domain, we can reference the var to the domain `FuncGroup1.x`
3. All functions are private to thee domain, so they can be called only from members inside the domain. This means that Foo2 can only be called from Foo1 or itself, but not from Foo3.
4. If you want a function to be visible outside the domain, add `Out' after the modifier of the function such as `Public Out`, `Private Out`. This means the Foo1 can be called from Foo3.
This have advantages over C# local functions and previous VB6 GoSub:
1. The code is organized and more readable.
2. unlike local function, not all vars are visible to the domain function, but we still have the advantage of avoiding passing long param lists.
3. unlike local function, we can pass some params byval. Foo2 can change the domain vars x and y but it can't change the z var defined in Foo1 and passed ByVal.
I think this can make us avoid using complex lambdas, and achieve the desired local function ability.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.