Skip to content

Instantly share code, notes, and snippets.

@chris-hatton
Last active December 31, 2015 21:36
Show Gist options
  • Save chris-hatton/2136362ed37d0d4219b0 to your computer and use it in GitHub Desktop.
Save chris-hatton/2136362ed37d0d4219b0 to your computer and use it in GitHub Desktop.
Significance of top-level definition of overloaded functions
protocol ProtocolA {}
protocol ProtocolB {}
func overloadingTest <T where T: ProtocolA>( obj: T ) -> String
{
return "It's an implementation of Protocol A"
}
func overloadingTest <T where T: ProtocolB>( obj: T ) -> String // This compiles fine, and the appropriate 'topLevelTest' function is bound
{
return "It's an implementation of Protocol B"
}
func aFunction()
{
func nestedOverloadingTest <T where T: ProtocolA>( obj: T ) -> String
{
return "It's an implementation of Protocol A"
}
func nestedOverloadingTest <T where T: ProtocolB>( obj: T ) -> String // Fails to compile with 'Definition conflicts with previous value'
{
return "It's an implementation of Protocol B"
}
}
@chris-hatton
Copy link
Author

Why can the compiler handle overloaded functions defined at top-level; but not against those inside the scope of another function?

@blixt
Copy link

blixt commented Nov 6, 2015

My wild guess is that nested functions become scoped values assigned to a reference to the function, rather than using regular function/method resolution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment