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

236
Views
Are Kotlin functions really first class types?

Does the fact that this doesn't compile mean that they're not quite first class types?

fun foo(s: String): Int = s.length
// This won't compile.
val bar = foo

Is there a way to do this without resorting to OO?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Does the fact that this doesn't compile mean that they're not quite first class types?

No, it doesn't.

In Kotlin, to reference a function or a property as a value, you need to use callable references, but it is just a syntax form for obtaining a value of a function type:

fun foo(s: String): Int = s.length

val bar = ::foo // `bar` now contains a value of a function type `(String) -> Int`

Once you get that value, you are not limited in how you work with it, which is what first-class functions are about.

over 4 years ago · Santiago Trujillo Report

0

You can use reference to the function :::

fun foo(s: String): Int = s.length

val bar = ::foo

And then invoke it:

bar("some string")
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!