1

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

?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175

2 Answers2

2

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?

vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • It could use `default(T)` or `Nothing` as the missing return value. – Jonathan Allen Aug 11 '16 at 18:10
  • @JonathanAllen: it could, but then that's the compiler making a semantic decision for the user. The compiler currently doesn't allow a method with a return type to not have a return statement in all code paths. Your suggestion would violate that rule, effectively. – siride Aug 11 '16 at 18:12
  • @JonathanAllen, I agree. Also, a special syntax could be designed to specify the return value if it is not implemented. – Lajos Arpad Aug 11 '16 at 18:12
  • @siride, you have a point, however, I can imagine the utopia of a syntax specifying the return value. – Lajos Arpad Aug 11 '16 at 18:13
  • 1
    I'm just saying that it could be done. I wouldn't actually want it to work that way. – Jonathan Allen Aug 11 '16 at 18:15
  • 3
    @LajosArpad: but why? What problem is it solving? You are better off using derived classes with virtual/abstract functions if you want this behavior. Partial types and methods are a convenience for dealing with generated code, not a mechanism for behavior management. – siride Aug 11 '16 at 18:15
  • @siride, I am curious about it. If there is provably no valid use-case, then that's the answer for not allowing partial functions. – Lajos Arpad Aug 11 '16 at 18:17
  • @LajosArpad: I could start by asking why you are asking this question. It sounds like you do have something you want solved. – siride Aug 11 '16 at 18:22
  • @siride, no, I am simply thinking about it. I have a habit of trying to understand things while waiting for a longer operation, like solution build. In this case I found the time that way to ask a question I was wondering about for a long time. – Lajos Arpad Aug 11 '16 at 18:24
  • All of this discussion is essentially why C# 7 is getting [generators](https://www.infoq.com/news/2016/04/CSharp-7). Presumably VB.NET will, too. – vcsjones Aug 11 '16 at 18:28
  • @vcsjones, basically, a generator could be the answer to the problem of what should an unimplemented partial function return. As a result, you are right in stating that this question is closely related to generators. – Lajos Arpad Aug 11 '16 at 18:34
1

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.

Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
  • That's a Sub, which acts like a Function. It is a good idea, but a work-around. I wonder about the reason Functions are not allowed to be partial. It is either a provable cause or a documented intention, or there is no specific reason. – Lajos Arpad Aug 11 '16 at 18:15
  • @LajosArpad: there's a very good reason. A function returns a value, on which other could depend. If the function is removed because it is never implemented, then the code that depends on it is left with..nothing. This is not semantically valid. As vcsjones points out, the compiler could just put in the default value, but that's guessing user's intent. It's not a safe substitution and is not mirrored anywhere else in the language. – siride Aug 11 '16 at 18:21
  • @siride, that's a good point. I wonder whether a special syntax would be useful here, something which would be mandatory for partial function declarations. – Lajos Arpad Aug 11 '16 at 18:23