[swift-evolution] [proposal] Allow function argument type to be omitted when passing a default value from which it can be inferred

Tino Heth 2th at gmx.de
Wed Jun 8 04:58:16 CDT 2016


If seen several suggestions to extend inference now, and although it is convenient, it can make it harder to understand what's happening.
Imho it would be possible to combine the benefits of both ways without their disadvantages:
Xcode already has the ability to correct some errors and warnings, but always asks the user first ("hey, shall I replace this var with let?"), and this is good in general, because silently changing code could lead to errors (when I declare something as constant, it's most likely an error when I try to modify the value afterwards).

Coming back to this specific proposal:
>  func proposeEvolution(evolution: Evolution, proposalManager = ProposalManager())
is an easy case - but what about
func proposeEvolution(evolution: Evolution, proposalManager = someFunctionWhoseReturnTypeIsntObvious())

It would be possible to create a tool that complements the source by adding the type, but I don't think Swift-evolution is the right place to discuss this, as it's not actually about the language itself.
But: Swift has the benefit that not only the development of the language, but also the compiler, IDE and all essential tools are driven by a single company. Therefore it easier (at least I hope so) to evolve language and tools side by side, and it would be possible to add features that only work in combination.
One problem with inferred information is that its origin can't be recognized unless it is annotated, so that tools could see that (for example) the user didn't care wether x is a variable or constant, and could change the type whenever it makes sense to do so.
The only way to achieve this now would be a special comment like
/*?*/let x = 0
which, of course, is way to ugly to be useful.

I did not investigate how big the performance-penalty for compilation of code that makes heavy use of type inference is, but I guess "caching" could help here as well.
This could be done by adding special characters (I've no good idea for this, so I'll use the general-purpose special character ;-):
func proposeEvolution(evolution: Evolution, proposalManager: #ProposalManager = someFunctionWhoseReturnTypeIsntObvious())


More information about the swift-evolution mailing list