Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

63
Views
how to set custom return type nullable in kotlin

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
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!