I made a personal data type "email" how can I make this type nullable in value.
For example.
private lateinit var a: email?
or
fun makeEmailAddress(a: String): email? {
// TODO
}
CodePudding user response:
lateinit var
uses nulls behind the scenes to represent the uninitialized value, so they cannot be of nullable types from the point of view of the program. That said, this limitation is in general not a problem because most of the time the point of using lateinit var
is so that you can use non-null types while still delaying initialization.
If you need a nullable property, you can simply drop the lateinit
modifier and initialize it to null
when declaring it:
private var a: Email? = null
For the function declaration, the return type Email?
should just work:
fun makeEmailAddress(a: String): Email? {
// return whatever you need here, either null or an Email instance
}
Note that types are capitalized in Kotlin by convention, so I suggest you rename email
to Email
(I used the capitalized version in the sample code above).