default args

This commit is contained in:
Alex Tkachman
2011-10-19 07:42:31 +02:00
parent 1b4bebaaf0
commit 33d09eb1df
10 changed files with 475 additions and 96 deletions
@@ -0,0 +1,49 @@
fun reformat(
str : String,
normalizeCase : Boolean = true,
uppercaseFirstLetter : Boolean = true,
divideByCamelHumps : Boolean = true,
wordSeparator : String = " "
) =
(normalizeCase, uppercaseFirstLetter, divideByCamelHumps, wordSeparator)
trait A {
fun bar2(arg: Int = 239) : Int
fun bar(arg: Int = 240) : Int = bar2(arg/2)
}
open abstract class B() : A {
fun foo(arg: Int = 239 + 1) : Int = arg
abstract fun foo2(arg: Int = 239) : Int
override fun bar2(arg: Int) : Int = arg
}
class C() : B() {
override fun foo2(arg: Int) : Int = arg
}
// fun <T> T.toPrefixedString(prefix: String = "") = prefix + (this as java.lang.Object).toString()
fun box() : String {
val expected = (true, true, true, " ")
// if("mama".toPrefixedString("papa") != "papamama") return "fail"
// if("mama".toPrefixedString() != "mama") return "fail"
if(C().bar(10) != 5) return "fail"
if(C().bar() != 120) return "fail"
if(C().foo(10) != 10) return "fail"
if(C().foo() != 240) return "fail"
if(C().foo2() != 239) return "fail"
if(C().foo2(10) != 10) return "fail"
if(C().bar2() != 239) return "fail"
if(C().bar2(10) != 10) return "fail"
if(reformat("", true, true, true, " ") != expected) return "fail"
if(reformat("", true, true, true) != expected) return "fail"
if(reformat("", true, true) != expected) return "fail"
if(reformat("", true) != expected) return "fail"
if(reformat("") != expected) return "fail"
return "OK"
}
@@ -0,0 +1,19 @@
fun reformat(
str : String,
normalizeCase : Boolean = true,
uppercaseFirstLetter : Boolean = true,
divideByCamelHumps : Boolean = false,
wordSeparator : String = " "
) =
(normalizeCase, uppercaseFirstLetter, divideByCamelHumps, wordSeparator)
fun box() : String {
val expected = (true, true, false, " ")
if(reformat("", true, true, false, " ") != expected) return "fail"
// if(reformat(str="", wordSeparator=" ") != expected) return "fail"
// if(reformat("", true, true, false) != expected) return "fail"
// if(reformat("", true, true) != expected) return "fail"
// if(reformat("", true) != expected) return "fail"
// if(reformat("") != expected) return "fail"
return "OK"
}
@@ -0,0 +1,13 @@
fun loop(times : Int) {
while(times > 0) {
val u : fun(value : Int) : Unit = {
System.out?.println(it)
}
u(times--)
}
}
fun box() : String {
loop(5)
return "OK"
}