Skip to content

Commit 42e8297

Browse files
jeffwidmandeckarep
authored andcommitted
Delete New*SetFromSlice() and NewSetWith() APIs
These are no longer needed now that both `NewSet()` and `NewThreadUnsafeSet()` accept optional vals. Removing them simplifies the API surface, so that developers aren't left scratching their heads wondering why there's both a `NewSet(vals ...T)` and a `NewSetFromSlice()` that do the same thing. Previously they couldn't be removed due to backwards compatibility concerns, but with the `go 1.18` drop of generics, this library will be cutting a new `v2` release, so a perfect time to cleanup this confusing API as well.
1 parent 8f063a4 commit 42e8297

File tree

4 files changed

+20
-42
lines changed

4 files changed

+20
-42
lines changed

iterator_example_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ type yourType struct {
3535

3636
func Test_ExampleIterator(t *testing.T) {
3737

38-
s := NewSetFromSlice[*yourType]([]*yourType{
39-
&yourType{name: "Alise"},
40-
&yourType{name: "Bob"},
41-
&yourType{name: "John"},
42-
&yourType{name: "Nick"},
43-
})
38+
s := NewSet[*yourType](
39+
[]*yourType{
40+
&yourType{name: "Alise"},
41+
&yourType{name: "Bob"},
42+
&yourType{name: "John"},
43+
&yourType{name: "Nick"},
44+
}...,
45+
)
4446

4547
var found *yourType
4648
it := s.Iterator()

set.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,6 @@ func NewSet[T comparable](vals ...T) Set[T] {
185185
return &s
186186
}
187187

188-
// NewSetWith creates and returns a new set with the given elements.
189-
// Operations on the resulting set are thread-safe.
190-
func NewSetWith[T comparable](vals ...T) Set[T] {
191-
return NewSetFromSlice(vals)
192-
}
193-
194-
// NewSetFromSlice creates and returns a reference to a set from an
195-
// existing slice. Operations on the resulting set are thread-safe.
196-
func NewSetFromSlice[T comparable](v []T) Set[T] {
197-
s := NewSet(v...)
198-
return s
199-
}
200-
201188
// NewThreadUnsafeSet creates and returns a new set with the given elements.
202189
// Operations on the resulting set are not thread-safe.
203190
func NewThreadUnsafeSet[T comparable](vals ...T) Set[T] {
@@ -207,14 +194,3 @@ func NewThreadUnsafeSet[T comparable](vals ...T) Set[T] {
207194
}
208195
return &s
209196
}
210-
211-
// NewThreadUnsafeSetFromSlice creates and returns a reference to a
212-
// set from an existing slice. Operations on the resulting set are
213-
// not thread-safe.
214-
func NewThreadUnsafeSetFromSlice[T comparable](v []T) Set[T] {
215-
s := NewThreadUnsafeSet[T]()
216-
for _, item := range v {
217-
s.Add(item)
218-
}
219-
return s
220-
}

set_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ func Test_NewSet(t *testing.T) {
5555
t.Error("NewSet should start out as an empty set")
5656
}
5757

58-
assertEqual(NewSetFromSlice[int]([]int{}), NewSet[int](), t)
59-
assertEqual(NewSetFromSlice[int]([]int{1}), NewSet[int](1), t)
60-
assertEqual(NewSetFromSlice[int]([]int{1, 2}), NewSet[int](1, 2), t)
61-
assertEqual(NewSetFromSlice[string]([]string{"a"}), NewSet[string]("a"), t)
62-
assertEqual(NewSetFromSlice[string]([]string{"a", "b"}), NewSet[string]("a", "b"), t)
58+
assertEqual(NewSet([]int{}...), NewSet[int](), t)
59+
assertEqual(NewSet([]int{1}...), NewSet(1), t)
60+
assertEqual(NewSet([]int{1, 2}...), NewSet(1, 2), t)
61+
assertEqual(NewSet([]string{"a"}...), NewSet("a"), t)
62+
assertEqual(NewSet([]string{"a", "b"}...), NewSet("a", "b"), t)
6363
}
6464

6565
func Test_NewUnsafeSet(t *testing.T) {
@@ -1105,7 +1105,7 @@ func Test_Example(t *testing.T) {
11051105
requiredClasses.Add("Biology")
11061106
11071107
scienceSlice := []interface{}{"Biology", "Chemistry"}
1108-
scienceClasses := NewSetFromSlice(scienceSlice)
1108+
scienceClasses := NewSet(scienceSlice)
11091109
11101110
electiveClasses := NewSet()
11111111
electiveClasses.Add("Welding")

threadsafe_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,13 @@ func Test_ToSliceDeadlock(t *testing.T) {
473473

474474
func Test_UnmarshalJSON(t *testing.T) {
475475
s := []byte(`["test", "1", "2", "3"]`) //,["4,5,6"]]`)
476-
expected := NewSetFromSlice(
476+
expected := NewSet(
477477
[]string{
478478
string(json.Number("1")),
479479
string(json.Number("2")),
480480
string(json.Number("3")),
481481
"test",
482-
},
482+
}...,
483483
)
484484

485485
actual := NewSet[string]()
@@ -494,19 +494,19 @@ func Test_UnmarshalJSON(t *testing.T) {
494494
}
495495

496496
func Test_MarshalJSON(t *testing.T) {
497-
expected := NewSetFromSlice(
497+
expected := NewSet(
498498
[]string{
499499
string(json.Number("1")),
500500
"test",
501-
},
501+
}...,
502502
)
503503

504504
b, err := json.Marshal(
505-
NewSetFromSlice(
505+
NewSet(
506506
[]string{
507507
"1",
508508
"test",
509-
},
509+
}...,
510510
),
511511
)
512512
if err != nil {

0 commit comments

Comments
 (0)