6 ms·
anything you can ‘range’ over :)
by wwarner 2y ago
anything you can ‘range’ over :)
- dharmab 2y agoI have written my own types that can do this? EDIT: Had to check, yeah I had to implement the iter.Seq type to do it
- School-Cotton 2y agoGo added support for custom iterators a few versions ago.
- hnlmorg 2y agoThat’s been possible for a couple of releases now In fact I created a struct that you can range over just a couple of days ago. I can’t say I love the way Go implemented it though. But it does work.
- wwarner 2y agodid not know this thank you!
- fuzztester 2y agoYou can range over the fields of a struct?
- dharmab 2y agohttps://go.dev/play/p/Is1uwjRGASI https://go.dev/play/p/Is1uwjRGASI But I suspect they meant a struct which contains/encapsulates data which can be ranged over.
- fuzztester 2y agothanks
- fuzztester 2y ago>I can’t say I love the way Go implemented it though. Why not?
- hnlmorg 2y agoIt might just be user error on my part but it wasn’t entirely clear to me: 1. How it should be implemented “correctly” 2. The resulting code isn’t clear how it works at first glance (particularly with the yield command, it has “magical” properties that take a little effort to grok) 3. Requires calling a method Example code: https://github.com/lmorg/Ttyphoon/blob/321738f289e4791e9674d05c28e359b23fe96e5a/tmux/pane_map.go#L41 https://github.com/lmorg/Ttyphoon/blob/321738f289e4791e9674d... I did write this at something like 11pm so it’s entirely possible I’ve done this completely wrong though. Also please ignore the weird use of mutexes here too. I’m also aware that sync.Map could/should have been used here. This struct was more of an experiment than anything that will ultimately find its way into production code.
- dharmab 2y agoIn this specific case, one could do something like https://github.com/dharmab/skyeye/blob/main/pkg/radar/contacts.go#L114 https://github.com/dharmab/skyeye/blob/main/pkg/radar/contac.... But your critique is still relevant for cases where you are generating/querying items on demand.
- hnlmorg 2y agoThanks for sharing that. I’ll give that a play tonight.