The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

kurtg (4) [Avatar] Offline
#1
Mixin seems cool, but I was playing with the example for listing 8.21 on p. 226


class EvenSieve {
def getNo2() {
removeAll { it % 2 == 0}
return this
}
}
class MinusSieve {
def minus(int num) {
removeAll { it % num == 0}
return this
}
}

List.mixin EvenSieve, MinusSieve
def object = (0..10).toList().no2 - 3 - 5
assert object == [1,7]
println object
println object instanceof List
object.each { println it }


Maybe it's a good example because it made me think, but what gets returned isn't a list, but an instance of 'MinusSieve'. It's not clear how to convert this back to a list except perhaps to write a quick closure to build a new list.

In the debugger, I see that the 'object.metaclass.owner.referent' has what I'm looking for, but it's private/protected.

However in the dynamic world, it behaves like a list (for the most part) as long as I don't call any methods that require coercion to a list.

As a reader, it leaves me wondering how useful this feature really is. On first blush, it sounded great, but now, I'm cautious. I also wonder why Groovy obscures the underlying instance and/or provides better support for casting.
Mittie (397) [Avatar] Offline
#2
Re: Mixin Example
Hi Kurt,

thanks a lot for your feedback. You mention a valid point. We have to clarify this.

thanks for posting
Dierk