SHARE
TWEET

typecheck2

a guest Feb 15th, 2018 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package example
  2.  
  3. import com.fasterxml.jackson.databind.JsonNode
  4. import com.fasterxml.jackson.databind.ObjectMapper
  5.  
  6. import java.util.stream.Stream
  7. import java.util.stream.StreamSupport
  8.  
  9. import org.w3c.dom.Document
  10. import org.w3c.dom.Element
  11. import org.w3c.dom.Node
  12. import org.w3c.dom.NodeList
  13. import javax.xml.parsers.DocumentBuilderFactory
  14. import java.io.ByteArrayInputStream
  15. import java.io.InputStream
  16. import java.io.StringReader
  17.  
  18. data class Event(val id: Int)
  19.  
  20. val stringToEvent: (String) -> Event = { s -> Event(s.toInt()) }
  21. val dummyToEvent: (Document) -> Event = { _ -> Event(1) }
  22. val jsonToEvent: (JsonNode) -> Event = { j -> Event(j.get("id").asInt()) }
  23.  
  24. fun elementGen(opt: String): Any {
  25.     return when(opt) {
  26.         "string" -> "1"
  27.         "json" -> {
  28.             val mapper = ObjectMapper()
  29.             mapper.readTree("{\"id\": 1 }")
  30.         }
  31.         "xml" -> {
  32.             val xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<id>1</id>"
  33.             val src = ByteArrayInputStream(xml.toByteArray())
  34.             DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src)
  35.         }
  36.         else -> throw RuntimeException("Option not supported")
  37.     }
  38. }
  39.  
  40. fun main(args : Array<String>) {
  41.  
  42.     val parser = when (args[0]) {
  43.         "string" -> stringToEvent   // I get
  44.                                     // /home/gbenincasa/develop/ihmc/higherOrderFun/src/main/kotlin/Example.kt: (49, 11): Out-projected type 'Function1<*, Event>' prohibits the use of 'public abstract operator fun invoke(p1: P1): R defined in kotlin.Function1'
  45.                                     // However tt builds if I remove this line
  46.         "json" -> jsonToEvent
  47.         "xml" -> dummyToEvent
  48.         else -> throw RuntimeException("Option not supported")
  49.     }
  50.  
  51.     print(parser(elementGen(args[0])))
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top