Thursday, December 19, 2013

Super Inline Functions?

I did some more thinking about what would be awesome to have in .NET and remembered something about my new implementation of SupaChat Server. For a lot of the commands, it tries to find a group object for a group ID specified somewhere in the arguments array. Therefore, I have to have code like this in those case blocks:

Dim TargetGroup As Group = Groups.Find(Function(g) g.ID = _
 Args(1))
If TargetGroup Is Nothing Then
 Sender.SendData("*G* Group doesn't exist!")
 Return
End If
If Not Sender.RepUser.InGroup(TargetGroup) Then
 Sender.SendData("*G* You're not there!")
 Return
End If

In addition to being simplified by the breaker subs I talked about last time, this is kind of a pain and is very repetitive. I would like to always define variables for things like the target group, target user, and sending rank and have them only set by one line - a "set-up" call if you will. However, that would require a new kind of inline sub. It's hard to explain, but I was thinking of something like this:

Breakable Sub HandleCommand(Command As String, Args() As String)
 Dim TargetGroup As Group
 ' other variables
 Inline Breaker(1) Sub GetGroup(ByInline TargetGroup As Group, _
  ArgPos As Integer, NeedsPresent As Boolean)
  TargetGroup = Groups.Find(Function(g) g.ID = Args(ArgPos))
  ' etc from code in last section
 End Sub
 Select Case UCase(Command)
  Case "LEAVEGROUP"
   GetGroup(1, True)
   ' other instructions
  ' other commands
 End Select
 ' other stuff
End Sub

That "inline" sub inside the command handler sub is only visible and runnable in the context of that one sub. Variables passed ByInline pass a double-strong pointer, so setting where it points inside the inline sub would change what it points to back in the root sub. Inline subs must appear after all local variables they use. When invoking inline subs, ByInline parameters cannot be specified; they are taken from the current accessible variable set.

This would be an amazing feature. Get on it, Microsoft!

No comments:

Post a Comment