Hi there. Are there any plans on using the return type of a generic to search for matches. Best explained with an example:
proc default[T]: T =
result
proc add(x, y: int): int =
x + y
echo add(2, default())
Or maybe even the more advanced matching:
proc default[T]: T =
result
proc add[T](x, y: T): T =
x + y
echo add(2, default())
I'm guessing the second case is harder because it might break on auto? If I would to implement this in a pull request would it be considered? Thank you
People keep requesting overloading on return types which is similar to your proposal but I don't like ever more complexity in the compiler/language. I won't necessarily reject PRs though, it is an interesting experiment for sure. (We can enable it via .experimental.)
I'm guessing the second case is harder because it might break on auto?
That shouldn't stop you, the feature can only deal with a subset of the language to begin with:
proc p[T](): T = discard
var x = p() # yay not valid, needs: var x: int = p()