Skip to content

Instantly share code, notes, and snippets.

@geraldcroes
Created December 9, 2017 16:56
Show Gist options
  • Select an option

  • Save geraldcroes/6ffa82303b3f04850321e3c8467c9e64 to your computer and use it in GitHub Desktop.

Select an option

Save geraldcroes/6ffa82303b3f04850321e3c8467c9e64 to your computer and use it in GitHub Desktop.
Embedded companion object in an interface
interface FileParser {
companion object {
fun createFromFileName(fileName: String) =
when (fileName.substringAfterLast('.')) {
"xml" -> XmlFileParser()
"json" -> JsonFileParser()
else -> throw Exception("I don't know how to deal with $fileName.")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment