如何简化kotlin函数的默认实现

下面的代码,我总是觉得有点啰嗦。

//judging employee's salary level, and set a default implemention of the function:whether it > amount fun paidMore(amount: Int, employee: Employee, predicate: (Int, Employee) -> Boolean = { amount, employee -> employee.salary > amount } ): Boolean = predicate(amount, employee) 

我想我可以简化这样的代码:

 fun paidMore(amount: Int, employee: Employee, predicate: (Int, Employee) -> Boolean = employee.salary > amount ): Boolean = predicate(amount, employee) 

当然,我是自以为是的。 它不能运行。

 //This code is treated as a Boolean expression, not a function. employee.salary > amount 

为什么? 我应该怎么做来简化代码?

哦,是的,我简化了一些代码。

 fun paidMore(amount: Int, employee: Employee, predicate: (Int, Employee) -> Boolean = { _, _ -> employee.salary > amount } ): Boolean = predicate(amount, employee) 

现在,我再次简化了代码

 import org.jetbrains.spek.api.Spek import org.jetbrains.spek.api.dsl.describe import org.jetbrains.spek.api.dsl.it import kotlin.test.assertFalse import kotlin.test.assertTrue object ClosureSpec : Spek({ describe("Test, add a default implementation to the function") { data class Employee(val name: String, val salary: Int) //the model used to test fun paidMore(amount: Int, employee: Employee, predicate: (Int) -> Boolean = { _ -> employee.salary > amount } ): Boolean = predicate(amount) it("judging employee's salary level") { fun isHighPaid(employee: Employee): Boolean = paidMore(100, employee) fun isHighPaid2(employee: Employee): Boolean = paidMore(100, employee) {amount -> employee.salary > amount * 2 } val employee = Employee("yuri", 101) assertTrue(isHighPaid(employee)) assertFalse(isHighPaid2(employee)) assertTrue(isHighPaid2(Employee("peter", 201))) assertFalse(isHighPaid2(Employee("jerry", 200))) } } }) 

要不然

 import org.jetbrains.spek.api.Spek import org.jetbrains.spek.api.dsl.describe import org.jetbrains.spek.api.dsl.it import kotlin.test.assertFalse import kotlin.test.assertTrue object ClosureSpec : Spek({ describe("Test, add a default implementation to the function") { data class Employee(val name: String, val salary: Int) //the model used to test //judging employee's salary level fun Employee.paidMore(amount: Int, predicate: (Int) -> Boolean = { this.salary > it }): Boolean = predicate(amount) on("use the default implementation") { val amount = 100 fun Employee.isHighPaid() = paidMore(amount) it("should all be passed") { assertTrue(Employee("Alice", amount + 1).isHighPaid()) assertFalse(Employee("Alice", amount - 1).isHighPaid()) } } on("override the default implementation") { val amount = 100 fun Employee.isHighPaid() = paidMore(amount) { this.salary > it * 2 } it("should all be passed") { assertTrue(Employee("Bob", amount * 2 + 1).isHighPaid()) assertFalse(Employee("Bob", amount * 2 - 1).isHighPaid()) } } } }) 

哪一个更好? 别的东西? 期待你的好主意