Skip to content

Commit

Permalink
json/option: Add helper Optional[T] type
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondburned committed Aug 6, 2024
1 parent c40c6b8 commit af0a612
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/diamondburned/arikawa/v3

go 1.16
go 1.18

require (
github.com/gorilla/schema v1.4.1
Expand Down
14 changes: 14 additions & 0 deletions utils/json/option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,17 @@
// To generate pointerrized primitives, there are helper functions NewT() for
// each option type.
package option

// Optional wraps a type to make it omittable.
type Optional[T any] *T

// Some creates a new Optional with the value of the passed type.
func Some[T any](v T) Optional[T] {
return &v
}

// PtrTo creates a pointer to the passed value.
// It is like [Some], except it returns *T directly rather than Optional[T].
func PtrTo[T any](v T) *T {
return &v
}

0 comments on commit af0a612

Please sign in to comment.