Skip to content
This repository has been archived by the owner on Aug 3, 2018. It is now read-only.

Commit

Permalink
Add a new "New" method to the Interface
Browse files Browse the repository at this point in the history
This is needed mainly to create a new set from other methods where we do not have any knowledge about the underlying struct. Also this will be helpful for #13
  • Loading branch information
fatih committed Feb 3, 2014
1 parent fcc45fe commit a03200a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions set.go
Expand Up @@ -9,6 +9,7 @@ package set

// Interface describing a Set. Sets are an unordered, unique list of values.
type Interface interface {
New(items ...interface{}) Interface
Add(items ...interface{})
Remove(items ...interface{})
Pop() interface{}
Expand Down
11 changes: 9 additions & 2 deletions set_nots.go
@@ -1,8 +1,8 @@
package set

import (
"fmt"
"strings"
"fmt"
"strings"
)

// Provides a common set baseline for both threadsafe and non-ts Sets.
Expand All @@ -26,6 +26,13 @@ func NewNonTS(items ...interface{}) *SetNonTS {
return s
}

// New creates and initalizes a new Set interface. It accepts a variable
// number of arguments to populate the initial set. If nothing is passed a
// zero size Set based on the struct is created.
func (s *set) New(items ...interface{}) Interface {
return NewNonTS(items...)
}

// Add includes the specified items (one or more) to the set. The underlying
// Set s is modified. If passed nothing it silently returns.
func (s *set) Add(items ...interface{}) {
Expand Down
5 changes: 4 additions & 1 deletion set_ts.go
Expand Up @@ -21,6 +21,10 @@ func New(items ...interface{}) *Set {
return s
}

// func (s *set) New(items ...interface{}) Interface {
// return s.New(items...)
// }

// Add includes the specified items (one or more) to the set. The underlying
// Set s is modified. If passed nothing it silently returns.
func (s *Set) Add(items ...interface{}) {
Expand Down Expand Up @@ -185,4 +189,3 @@ func (s *Set) Merge(t Interface) {
return true
})
}

4 changes: 4 additions & 0 deletions set_ts_test.go
Expand Up @@ -13,6 +13,10 @@ func TestSet_New(t *testing.T) {
t.Error("New: calling without any parameters should create a set with zero size")
}

u := s.New()
if u.Size() != 0 {
t.Error("New: creating a new set via s.New() should create a set with zero size")
}
}

func TestSet_New_parameters(t *testing.T) {
Expand Down

0 comments on commit a03200a

Please sign in to comment.