bodiam
10/22/2019, 12:18 AMMemberTbl.select {
MemberTbl.member_id eq "200"
}.first().also {
assertThat(it[MemberTbl.id]).isEqualTo("200")
}
But, I'd like to refactor it to something like: MemberTbl.findByMemberId("200") with the help of an extension function, since this code is duplicated quite a lot.
What I have so far is this:
private fun MemberTbl.findByMemberId(s: String, row: ResultRow.() -> Unit) = this.select { member_id eq s }.first().also { row (it) }
So that my end result will look like this:
MemberTbl.findByMemberId("200") {
assertThat(it[MemberTbl.id]).isEqualTo("200")
}
But that won't, since the it in the assertEquals is no longer the closure passed in. How can I fix this?wasyl
10/22/2019, 12:21 AMassertEquals in the second snippet. I’m having trouble understanding the issue 🤔bodiam
10/22/2019, 12:23 AMwasyl
10/22/2019, 12:25 AM(ResultRow) -> Unit instead of ResultRow.() -> Unit?wasyl
10/22/2019, 12:27 AMit anymore, instead the value being passed is an implicit receiver, so you can access it with this. Perhaps the assertion could look like this.get(MemberTbl.id).isEqualTo("200" with current versionbodiam
10/22/2019, 12:29 AMbodiam
10/22/2019, 12:29 AMprivate fun MemberTbl.findByMemberId(s: String, row:( ResultRow) -> Unit) = this.select { member_id eq s }.first().also(row)bodiam
10/22/2019, 12:29 AMbodiam
10/22/2019, 12:30 AMwasyl
10/22/2019, 12:33 AM(X) -> Unit you get { it == X }, while X.() -> Unit means { this == X} (and there’s no it at all). So you still could use the second one if you wanted to, just with this instead of itbodiam
10/22/2019, 12:57 AM