You can use it that way, but you don’t actually in...
# getting-started
r
You can use it that way, but you don’t actually invoke the caller, so I don’t see the point in extending the
Date
class
1
z
eeeh no sorry intent is to set calendar at the extended day 🙂 so I guess i need to keep
this
referenco for the original date and can not do lambda right?
a
can you give us an example of input/output should be when you call that function?
z
I would say this is the way.. I mean I always can do in java way but want to d oin right kotlin way 🙂
Copy code
fun Date.add(calendarUnit: Int, amount:Int): Date = let{
    val calendar = java.util.Calendar.getInstance()
    calendar.time=this
    calendar.add(calendarUnit,amount)
    return calendar.time
}
a
inside the
let
, the date is both
this
and
it
, but its fine like that
z
I meant.. as I wanted it as extension, I need reference to date as
this
.. but i liked to get callendar into lambda as
it
but i probably overcomplicate ...
r
You could do
Copy code
fun Date.add(calendarUnit: Int, amount: Int) {
  return java.util.Calendar.getInstance().apply{
      time = this
      add(calendarUnit, amount)
    }.time
}
Although, I don’t know that it’s necessarily easier to read 🤔
z
Yea! ah.. you think so?
hmm probaly so... i left it as
Copy code
fun Date.add(calendarUnit: Int, amount:Int): Date = let{
    val calendar = java.util.Calendar.getInstance()
    calendar.time=this
    calendar.add(calendarUnit,amount)
    return calendar.time
}
r
I have to say,
let
isn’t giving you any special functionality there. It’s essentially taking up space. You’re not even invoking
it
, which is the default binding it provides. You can just remove the
= let
and it functions precisely the same.
z
ok not want to prolongate the pain 🙂 but while getting my way to apply run witht etc... i thought even this as possibly propriate
Copy code
fun Date.add(calendarUnit: Int, amount:Int):Date =
        Calendar.getInstance().let {
            it.time=this
            it.add(calendarUnit,amount)
            it.time
        }