Kotlin: Iterate over components of object

后端 未结 3 1292
长发绾君心
长发绾君心 2020-12-30 20:55

Each data class object has a component for each property like component1, component2, etc.. I was wondering if there is any way in Kotlin to iterate over each component of a

3条回答
  •  情歌与酒
    2020-12-30 21:47

    here is an idea...i am not satisfied with it...but nonetheless here it is.

    it has some pros and cons:

    • pros:
      • type-safe/compile-time checks
        • adding/removing fields to/from the data class causes compiler errors at field-iteration sites
      • no boiler-plate code needed
    • cons:
      • won't work if default values are defined for arguments

    declaration:

    data class Memento(
        val testType: TestTypeData,
        val notes: String,
        val examinationTime: MillisSinceEpoch?,
        val administeredBy: String,
        val signature: SignatureViewHolder.SignatureData,
        val signerName: String,
        val signerRole: SignerRole
    ) : Serializable
    

    iterating through all fields:

    val iterateThroughAllMyFields: Memento = someValue
    Memento(
        testType = iterateThroughAllMyFields.testType.also { testType ->
            // do something with testType
        },
        notes = iterateThroughAllMyFields.notes.also { notes ->
            // do something with notes
        },
        examinationTime = iterateThroughAllMyFields.examinationTime.also { examinationTime ->
            // do something with examinationTime
        },
        administeredBy = iterateThroughAllMyFields.administeredBy.also { administeredBy ->
            // do something with administeredBy
        },
        signature = iterateThroughAllMyFields.signature.also { signature ->
            // do something with signature
        },
        signerName = iterateThroughAllMyFields.signerName.also { signerName ->
            // do something with signerName
        },
        signerRole = iterateThroughAllMyFields.signerRole.also { signerRole ->
            // do something with signerRole
        }
    )
    

提交回复
热议问题