-2

for all the examples on the internet i cant figure out when and how is kotlins let ran?

if(phones.size == 0){
        phones.add("")
    }
return phones[0]

so if phones list size is 0, we add empty string and return that instead.

Now how would one do same with let ?

phones.let {
    return ""
}

does this work with size 0, or do i have to have null list?

do i need return keyword, if yes, where?

is the above fun always going to return empty string? or just when phones is null?

when is this let code block even ran?

Clomez
  • 1,410
  • 3
  • 21
  • 41
  • 3
    The original code doesn't take nullness into account - only whether the list is empty or not. So I don't see how `let` is relevant if what you want is to turn the original code into kotlin(?). – Michael Mar 22 '19 at 11:34
  • 1
    All your questions can be answered by just testing it, and reading the documentation of let. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/let.html, https://kotlinlang.org/docs/reference/idioms.html#execute-if-not-null – JB Nizet Mar 22 '19 at 11:35
  • Or reading the source code of let – EpicPandaForce Mar 22 '19 at 12:32

2 Answers2

1

Update:

val cakes = listOf("carrot", "cheese", "chocolate")

fun main(args: Array<String>) {
    var cakesEaten = 0

    while (cakesEaten < 3) {  // 1
        cakesEaten ++

        val result = cakes?.let{ 

                 if(cakesEaten == 2) {
                     "HeyLo"
                 } else {
                  2   
                 }
            } 

        println("result value = $result")

        when(result) {
          is String -> println(" result variable is a String")

          is Int -> println(" result variable is Integer")
        }

      }
}
result value = 2
 result variable is Integer
result value = HeyLo
 result variable is a String
result value = 2
 result variable is Integer

Original post

If your 'phones' Object is a Nullable type,

 val result = phones?.let{ 
                 // this block runs only if phones object is not null
                 // items can be accessed like it.size
                 // expression result will be returned. no need to mention return.
                 if(it.size == 0) {
                       it.add("")
                       it[0]   
                 } else it.size
             }

result value will be either it[0] or it.size and its type will be Any.

But if this the functionality you need you can check Markos solution.

Mahi
  • 472
  • 5
  • 7
  • "will be either it[0] or it.size" but that's not what the OP's code does - it only returns `it[0]`, never `it.size`. " its type will be inferred at run time" - what does that mean? Types in "inferred" by the compiler at compile time. At runtime, types aren't inferred, they are known exactly and any mismatch like in cast expressions cause a runtime error. – Erwin Bolwidt Mar 23 '19 at 09:44
  • here, if block is an expression, so it can be a string or int, which will be passed onto result @ErwinBolwidt – Mahi Mar 24 '19 at 10:40
  • There is nothing like that in the OP's question. The op's expression returns a String – Erwin Bolwidt Mar 24 '19 at 10:48
  • @ErwinBolwidt I have updated the post with working example and results. – Mahi Mar 24 '19 at 11:22
0

If you're interested in how to write your logic in Kotlin's FP idiom, it doesn't involve let at all:

phones.takeIf { it.isEmpty() }?.add("")
return phones[0]

However, I don't find this idiom better than what you started out with.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436