Ananiya
11/23/2020, 12:30 PMoperator fun String.times(i: Int){
var string: String? = null
repeat(i){
string = this
}
return string
}
fun main(){
print("hello"*8)
}
It suppose to print 8x but it returns null from the fun or print only 1 helloCicero
11/23/2020, 12:32 PMwasyl
11/23/2020, 12:34 PMstring += this
and not string = this
Rob Elliot
11/23/2020, 12:37 PMoperator fun String.times(i: Int): String {
var string = ""
repeat(i){
string += this
}
return string
}
Mikael Alfredsson
11/23/2020, 12:37 PMAnaniya
11/23/2020, 12:38 PMMikael Alfredsson
11/23/2020, 12:39 PMoperator fun String?.times(i: Int) = this?.repeat(i)
Ananiya
11/23/2020, 12:40 PMCicero
11/23/2020, 12:40 PMRob Elliot
11/23/2020, 12:51 PM"hello" * 1
has type String?
, which I’d find more of a pain than occasionally having to do val x: String? = null; x?.times(1)
Mikael Alfredsson
11/23/2020, 12:55 PMMikael Alfredsson
11/23/2020, 12:57 PMoperator fun String.times(i: Int) = this.repeat(i)
and the compiler will tell you if you are trying to call it on a nullable stringAnaniya
11/23/2020, 12:59 PMCicero
11/23/2020, 1:02 PMlouiscad
11/23/2020, 3:36 PMrepeat
extension for String
?Ananiya
11/23/2020, 4:00 PMlouiscad
11/23/2020, 4:02 PMrepeat
typed? blob thinking upside downlouiscad
11/23/2020, 4:05 PM*
).Ananiya
11/23/2020, 4:06 PM