spand
01/10/2020, 1:06 PMfun LocalDate.set(year: Int?, month: Int?, day: Int?) : LocalDate {
var tmp = this
if (year != null){
tmp = tmp.withYear(year)
}
if (month != null){
tmp = tmp.withMonth(month)
}
if (day != null){
tmp = tmp.withDayOfMonth(day)
}
return tmp
}
bezrukov
01/10/2020, 1:21 PMfun LocalDate.set(
newYear: Int = year,
newMonth: Int = monthValue,
newDay: Int = dayOfMonth
): LocalDate {
return withYear(newYear).withMonth(newMonth).withDayOfMonth(newDay)
}
then call will be:
date.set(newYear = 2019, newMonth = 3)
or
date.set(newDay = 10)
// or at least you could add default null values to your version, so you don't need to call date.set(2020, null, null)
if you want to update only year - it will be date.set(2020)
or date.set(month = 2)
if you want update only monthbezrukov
01/10/2020, 1:23 PMfun LocalDate.set(
year: Int = this.year,
month: Int = monthValue,
day: Int = dayOfMonth
): LocalDate {
return withYear(year).withMonth(month).withDayOfMonth(day)
}
my updated version without ugly new
prefix for parameter namesspand
01/10/2020, 1:24 PMkyleg
01/10/2020, 6:32 PMkyleg
01/10/2020, 6:33 PMkyleg
01/10/2020, 6:35 PMtmp
.withYear(year ?: this.year)
.withMonth(month ?: monthValue)
.withDay(day ?: dayOfMonth)
Which lets you leave your function signature alone but makes the null handling terserspand
01/13/2020, 8:15 AM