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

214
Views
¿Cómo hacer referencia al constructor parametrizado en Kotlin?

Así que tengo esta definición:

 sealed interface ParseResult<out R> { data class Success<R>(val value: R) : ParseResult<R> data class Failure(val original: String, val error: Throwable) : ParseResult<Nothing> }

Quiero envolver ciertos elementos en un Éxito. Y sé que puedo hacerlo así...

 list.map{ParseResult.Success(it)}

Pero, ¿hay alguna manera de usar una referencia de constructor?

 list.map(ParseResult::Success) //this won't compile
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede usar una referencia de constructor si agrega una import :

 import somepackage.ParseResult.Success

O incluso un typealias:

 typealias Success<R> = ParseResult.Success<R>

Entonces puedes hacer:

 list.map(::Success)

La idea aquí es hacer que ParseResult.Success se pueda referir con un nombre simple.

Sin embargo, no poder hacer ParseResult::Success me parece un error. Comparar:

 class Outer { class Nested } class OuterGeneric<T> { class Nested } fun main() { val x = Outer::Nested // works val y = OuterGeneric::Nested // error }
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!