Skip to content

Instantly share code, notes, and snippets.

@jeremytregunna
Created October 4, 2016 15:31
Show Gist options
  • Select an option

  • Save jeremytregunna/8858c5d1c597a0baf7cfbfe2390ac5a3 to your computer and use it in GitHub Desktop.

Select an option

Save jeremytregunna/8858c5d1c597a0baf7cfbfe2390ac5a3 to your computer and use it in GitHub Desktop.
protocol StateType {}
func withTypes<SpecificState>(_ state genericState: StateType?, function: (_ state: SpecificState?) -> SpecificState) -> StateType {
guard let genericState = genericState else {
return function(nil) as! StateType
}
guard let specificState = genericState as? SpecificState else {
return genericState
}
return function(specificState) as! StateType
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment