You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using `wireSet` you can obtain a set of multiple instances of the same type. This is done without constructing the set explicitly. All instances of the same type which are found by MacWire are used to construct the set.
730
732
731
-
Consider the below example. Let's suppose that you want to create a `RockBand(musicians: Set[Musician])` object. It's easy to do so using the `wireSet` functionality:
733
+
Using `wireList` you can obtain a list of multiple instances of the same type, preserving the order of definition. This works similarly to `wireSet`, but returns a `List` instead of a `Set`, maintaining the order in which the instances are discovered during macro expansion. This method is available only in Scala 3.
734
+
735
+
Consider the below example. Let's suppose that you want to create a `RockBand` object with musicians:
732
736
733
737
```scala
734
738
traitMusician
735
739
classRockBand(musicians: Set[Musician])
740
+
classOrchestra(musicians: List[Musician])
736
741
737
742
traitRockBandModule {
738
743
lazyvalsinger=newMusician {}
739
744
lazyvalguitarist=newMusician {}
740
745
lazyvaldrummer=newMusician {}
741
746
lazyvalbassist=newMusician {}
742
747
743
-
lazyvalmusicians= wireSet[Musician] // all above musicians will be wired together
744
-
// musicians has type Set[Musician]
748
+
lazyvalmusiciansSet= wireSet[Musician] // all above musicians will be wired together
749
+
// musiciansSet has type Set[Musician] (unordered)
750
+
751
+
lazyvalmusiciansList= wireList[Musician] // all above musicians will be wired together
752
+
// musiciansList has type List[Musician] (preserves order)
745
753
746
754
lazyvalrockBand= wire[RockBand]
755
+
lazyvalorchestra= wire[Orchestra]
747
756
}
748
757
```
749
758
759
+
Both `wireSet` and `wireList` look for instances in the same places:
760
+
- enclosing members (lazy vals, vals, defs without parameters)
761
+
- enclosing imports
762
+
- parent classes and traits
763
+
764
+
The key difference is that `wireSet` returns an unordered `Set[T]` while `wireList` returns an ordered `List[T]` that preserves the order of definition discovery.
0 commit comments