2

I'm starting developing for Android using Kotlin. I have a problem with some concepts.
I use "->" in a condition statement but I don't know what that means in this example:

 XmlPullParser.START_TAG -> {...}
 XmlPullParser.TEXT -> textValue = xpp.text
 XmlPullParser.END_TAG -> {...}

All the code is:

        val factory = XmlPullParserFactory.newInstance()
        factory.isNamespaceAware = true
        val xpp = factory.newPullParser()
        xpp.setInput(xmlData.reader())
        var eventType = xpp.eventType
        var currentRecord = FeedEntry()
        while (eventType != XmlPullParser.END_DOCUMENT) {
            val tagName = xpp.name.toLowerCase()    
            when (eventType) {

              XmlPullParser.START_TAG -> {
                    Log.d(TAG, "parse: Starting tag for " + tagName)
                    if (tagName == "entry") {
                        inEntry = true
                    }
                }

                XmlPullParser.TEXT -> textValue = xpp.text

                   XmlPullParser.END_TAG -> {
                    Log.d(TAG, "parse: Ending tag for " + tagName)
                    if (inEntry) {
                        when (tagName) {
                            "entry" -> {
                                applications.add(currentRecord)
                                inEntry = false
                                currentRecord = FeedEntry()  
                            }

                            "name" -> currentRecord.name = textValue
                            "artist" -> currentRecord.artist = textValue
                            "releasedate" -> currentRecord.releaseDate = textValue
                            "summary" -> currentRecord.summary = textValue
                            "image" -> currentRecord.imageURL = textValue
                        }
                    }
                }
            }
Adinia
  • 3,722
  • 5
  • 40
  • 58
NimaAzhd
  • 162
  • 3
  • 15
  • Possible duplicate of [What is a lambda (function)?](https://stackoverflow.com/questions/16501/what-is-a-lambda-function) – Gustavo Pagani Aug 20 '19 at 07:48
  • 3
    Both comments here are simply not true. He is asking about the `when` expression. Not about something related to a lambda! – StefMa Aug 20 '19 at 07:50

1 Answers1

6

This is just the syntax for a Kotlin when expression.

Basically, it checks the condition inside the when () and executes the block after -> if it matches. You can either "inline" the code which has to be executed or wrap it into curly brackets.

See also this example (Kotlin Playground) and change the aString to another value.

StefMa
  • 3,344
  • 4
  • 27
  • 48
  • yes your right. I studied about "When()" with "is ... ->". as I understand they are the same without "is". – NimaAzhd Aug 20 '19 at 07:57
  • You have to use the `is` keyword if you want to check the type of a instance. See https://kotlinlang.org/docs/reference/typecasts.html – StefMa Aug 20 '19 at 08:00
  • ok thanks. I have another question . when we enter to "when" block and for example first statement like : "XmlPullParser.START_TAG ->{} " is true . at this time does it break from "when block" or continue with next statements until end? – NimaAzhd Aug 20 '19 at 08:23
  • It does not fall through like the Java [`switch`](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html). So it will break for the first condition it matches. I've linked the `when` documentation in the post. Please read it – StefMa Aug 20 '19 at 08:26