Skip to content

Commit

Permalink
Add error handling to All
Browse files Browse the repository at this point in the history
  • Loading branch information
itsubaki committed Jun 21, 2023
1 parent cee567f commit b4fbe68
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions evaluator/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,18 @@ func (qb *Qubit) Get(a ast.Expr) ([]q.Qubit, bool) {
}

// All returns all values of qubit.
func (qb *Qubit) All() []q.Qubit {
func (qb *Qubit) All() ([]q.Qubit, error) {
out := make([]q.Qubit, 0)
for _, n := range qb.Name {
qb, _ := qb.Get(&ast.IdentExpr{Name: n}) // no error
qb, ok := qb.Get(&ast.IdentExpr{Name: n})
if !ok {
return nil, fmt.Errorf("qubit %v not found", n)
}

out = append(out, qb...)
}

return out
return out, nil
}

func (qb *Qubit) String() string {
Expand Down
2 changes: 1 addition & 1 deletion evaluator/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func ExampleQubit_All() {
fmt.Println(qb.All())

// Output:
// [1 2 3 4 5 6 7 8 9]
// [1 2 3 4 5 6 7 8 9] <nil>
}

0 comments on commit b4fbe68

Please sign in to comment.