1

In Visual Basic we can use With Expression like this:

 With theCustomer
        .Name = "Coho Vineyard"
        .URL = "http://www.cohovineyard.com/"
        .City = "Redmond"
 End With

I'm looking for something like this. Is it possible in Kotlin?

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
mohammad
  • 1,248
  • 3
  • 15
  • 27
  • 1
    https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/with.html, https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/apply.html – JB Nizet Jan 03 '19 at 08:45

4 Answers4

3

You can use with function from the Kotlin Standard library, e.g.:

with(theCustomer) {
    name = "Coho Vineyard"
    url = "http://www.cohovineyard.com/"
    city = "Redmond"
}

with() returns some result. It makes code cleaner.

Also you can use apply extension function:

theCustomer.apply { 
    name = "Coho Vineyard"
    url = "http://www.cohovineyard.com/"
    city = "Redmond"
}

apply - declared on Any class, it could be invoked on instances of all types, it makes code more readable. Use when need to utilize an instance of the object (modify properties), express the chain of calls. It differs from with() in that it returns Receiver.

Sergio
  • 27,326
  • 8
  • 128
  • 149
3

Kotlin provides multiple, so called, scope functions. Some of them make use of a function literal with receiver, which make it possible to write similar code as provided by you in Visual Basic. Both, with and apply are suitable for this case. It's interesting to note that with returns some arbitrary result R while apply always returns the concrete receiver on which the function has been invoked.

For your example, let's consider both functions:

  1. with

    Using with, we can write the code as follows:

    val customer = Customer()
    with(customer) {
        name = "Coho Vineyard"
        url = "http://www.cohovineyard.com/"
        city = "Redmond"
    } 
    

    The last expression of the lambda passed to with here is an assignment, which, in Kotlin, returns Unit. You could assign the result of the with call to some new variable which would then be of type Unit. This is not useful and the whole approach is not very idiomatic since we have to separate the declaration from the actual initialization of customer.

  2. apply

    With apply on the other hand, we can combine declaration and initialization as it returns its receiver by default:

    val customer = Customer().apply {
        name = "Coho Vineyard"
        url = "http://www.cohovineyard.com/"
        city = "Redmond"
    }
    

As you can see, whenever you want to initialize some object, prefer apply (extension function defined on all types). Here's another thread on the differences between with and apply.

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
0

Something like this?

    with(theCustomer) {
        Name = "Coho Vineyard"
        URL = "http://www.cohovineyard.com/"
        City = "Redmond"
    }

But with requires non-nullable parameter. I suggest using let or apply instead.

    theCustomer?.apply{
        Name = "Coho Vineyard"
        URL = "http://www.cohovineyard.com/"
        City = "Redmond"
    }

or

    theCustomer?.let{ customer ->
        customer.Name = "Coho Vineyard"
        customer.URL = "http://www.cohovineyard.com/"
        customer.City = "Redmond"
    }
phatnhse
  • 3,870
  • 2
  • 20
  • 29
  • 1
    `with` definitely does not _require_ a non-null receiver, its type parameter `T` has no upper bound. Of course, it is true that using it on a nullable reference will (obviously) result in the lambda having a nullable `this`. – Salem Jan 03 '19 at 09:14
0

Dim Total As Integer Dim I As Integer

For I = 1 To Len(TextBox1.Text)

Select Case Mid$(TextBox1.Text, I, 1)
     Case "ء" : Total = Total + 1
            Case "ا" : Total = Total + 1
            Case "أ" : Total = Total + 1
            Case "آ" : Total = Total + 1
            Case "ب" : Total = Total + 2
            Case "ج" : Total = Total + 3
            Case "د" : Total = Total + 4
            Case "ه" : Total = Total + 5
           Case "ة" : Total = Total + 5
End Select
Next
TextBox2.Text = Total