Why can't we write partial Functions?

160 Views Asked by At

Let us suppose that we have a Partial Class called Foo, like this:

Partial Public Class Foo
    Partial Private Sub Bar(lorem As String)
    End Function
    Private Sub Bar(lorem As String)
        'Do something
    End Function
End Class

This is a Class called Foo and has a Partial method. My question is: why is it illegal to use Partial Functions like this:

Partial Public Class Foo
    Partial Private Function Bar(lorem As String) As Boolean
    End Sub
    Private Function Bar(lorem As String) As Boolean
       Return lorem.StartsWith("A")
    End Sub
End Class

?

2

There are 2 best solutions below

11
vcsjones On

If return values were allowed, the compiler would not be able to know how to handle return values. Imagine if the partial method weren't actually implemented. After all, a partial method doesn't have to have an implementation. It's an opportunity to.

Where would the return value come from in that case? What if the caller of the partial method tried to use the return value but it was never implemented?

3
Jonathan Allen On

You can write partial functions, you just need to change the syntax slightly.

Partial Private Sub Bar(lorem As String, ByRef result As Boolean)

This allows the compiler to remove the function call it isn't implemented.