Re: [go-nuts] detecting cyclic references

2020-11-22 Thread arpad ryszka
Thanks a lot for the help! Seems like I could sort it out based on the reflect.DeepEqual code. Do you have any testing tips? Out of general curiosity, I am wondering why the type is part of the key for the visited map here: https://golang.org/src/reflect/deepequal.go#L15. If I remove it, the te

Re: [go-nuts] detecting cyclic references

2020-11-14 Thread 'Dan Kortschak' via golang-nuts
Or github.com/kortschak/utter . This package does an arguably better job at dealing with self- referencing structures. On Sat, 2020-11-14 at 03:52 -0800, twp...@gmail.com wrote: > > My use case is that I want to pretty print in-memory objects for > debug purposes during testing, and one of the fe

Re: [go-nuts] detecting cyclic references

2020-11-14 Thread twp...@gmail.com
> My use case is that I want to pretty print in-memory objects for debug purposes during testing, and one of the features would be to point out such cycles as this. Consider using an existing library for this, for example: https://github.com/sanity-io/litter https://github.com/davecgh/go-spe

Re: [go-nuts] detecting cyclic references

2020-11-11 Thread Ian Lance Taylor
On Wed, Nov 11, 2020 at 7:42 AM 'Axel Wagner' via golang-nuts wrote: > > https://play.golang.org/p/qTBiAdR9djt > > `a` is the address of the first element of `l`. > `b` is the address of the first element of `l[0]`, which is of type > `[]interface{}` after the assignment. > > Both `l` and `l[0]`

Re: [go-nuts] detecting cyclic references

2020-11-11 Thread 'Axel Wagner' via golang-nuts
BTW, the same idea doesn't work for maps, of course. For maps, you could exploit the fact that they are pointer-shaped in the current implementation and use unsafe to compare those pointers: https://play.golang.org/p/JCeXmoUctT2 You should keep in mind though, that this might break in the future, i

Re: [go-nuts] detecting cyclic references

2020-11-11 Thread 'Axel Wagner' via golang-nuts
https://play.golang.org/p/qTBiAdR9djt `a` is the address of the first element of `l`. `b` is the address of the first element of `l[0]`, which is of type `[]interface{}` after the assignment. Both `l` and `l[0]` refer to the same underlying array, so their first elements have the same address. I

Re: [go-nuts] detecting cyclic references

2020-11-11 Thread arpad ryszka
`l[0] = l` would be fine for me, indeed. Though I am not sure I understand the suggested solution. Notice that the type of the slice is `[]interface{}`. This... ll := l[0].([]interface{}) println(&l == &ll) ...would print `false`. I think the main challenge is that l and ll, or l[0] for that m

Re: [go-nuts] detecting cyclic references

2020-11-10 Thread 'Axel Wagner' via golang-nuts
The question was about detecting cyclic references. For that, the `l[0] = l` case is enough. On Wed, Nov 11, 2020 at 2:31 AM jake...@gmail.com wrote: > FYI, that does detect the simple case of l[0] = l, but not more > complicated circular cases like l[1] = l[1:] > > On Tuesday, November 10, 2020

Re: [go-nuts] detecting cyclic references

2020-11-10 Thread jake...@gmail.com
FYI, that does detect the simple case of l[0] = l, but not more complicated circular cases like l[1] = l[1:] On Tuesday, November 10, 2020 at 2:38:26 AM UTC-5 axel.wa...@googlemail.com wrote: > If the slice is empty, it doesn't reference anything. > If it is not empty, &x[0] can be used to iden

Re: [go-nuts] detecting cyclic references

2020-11-09 Thread 'Axel Wagner' via golang-nuts
If the slice is empty, it doesn't reference anything. If it is not empty, &x[0] can be used to identify the slice (potentially also using len/cap, if it's interesting). On Tue, Nov 10, 2020 at 4:11 AM arpad ryszka wrote: > Hi, > > is there a way to detect the cyclic reference in the following ex