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

215
Views
Inicializar variables con la expresión ".()" en Kotlin

Estoy usando Ktor para hacer solicitudes de websocket.
El método "websocket(...)" que utilizo toma dos expresiones lambda.

ingrese la descripción de la imagen aquí

Entonces, como solo puedo llamar a uno con corchetes, tengo que dar el otro a través de una variable. Así que probé esto:

 val request: HttpRequestBuilder.() -> Unit val socket = ktorHttpClient.webSocket(request = request) { }

Los tipos de datos encajan ahora pero, por supuesto, todavía dice "La 'solicitud' de variable debe inicializarse", así que traté de inicializarla:

 val request: HttpRequestBuilder.() -> Unit = HttpRequestBuilder.()

Ahora el error desapareció, pero dentro de los últimos corchetes dice "Esperando una expresión", ¿qué me estoy perdiendo aquí?

PD: Tal vez incluso haya alguien que pueda explicar en detalle cómo funciona la inicialización cuando se usa ".()".

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Como tu mismo dijiste, necesitas pasar dos lambdas. Puedes hacerlo directamente:

 ktorHttpClient.webSocket({ /* request lambda */ }) { /* block lambda */ }

O dividirlo como lo hizo:

 val request: HttpRequestBuilder.() -> Unit = { /* request lambda */ } val socket = ktorHttpClient.webSocket(request) { /* block lambda */ }

Puede encontrar más información sobre cómo inicializar lambdas/tipos de función aquí: https://kotlinlang.org/docs/lambdas.html#instantiating-a-function-type

over 4 years ago · Santiago Trujillo Report

0

Simplemente use parámetros con nombre con lambdas:

 ktorHttpClient.webSocket(request = {}, block = {})
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!