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.

tdeleeuw (9) [Avatar] Offline
#1
Hi

Page 104 of v 14, the example with remove contains strings, however I wonder what would be the result in this case:
def myList = [5, 4, 3, 2, 1, 0]
myList.remove(2)


Would it consider remove
* the element at position 2 (hence the result would be [5, 4, 2, 1, 0]) or
* element 2 from the list (hence it would be [5, 4, 3, 1, 0])

I think it would be useful to clarify (or tell me what I miss smilie )

Thanks

Thierry
paulk_asert (33) [Avatar] Offline
#2
Re: List.remove ambigious?
Groovy's method selection process will match the "remove" method with the most specific parameter. In this case it will be the remove(int) variant. If you want to match the Object variant you can add an explicit cast, e.g. "myList.remove((Object)2)" or use "myList.removeAll([2])" to achieve a similar result. There are is also the minus method for your particular example if you don't want to mutate the original, e.g. "myList -= 2" will now point to the desired list with the original left intact (in case there are other references to it).

The method selection process is discussed later in the book (e.g. chapter 8 and other places) but we'll look at clarifying briefly that particular aspect when describing that example. Thanks for spotting an area that is less clear in the book than it could be.