https://github.com/superpaintman/typed
typed is a typed Go stdlib for generics
https://github.com/superpaintman/typed
generic generics go go-generics go118 golang
Last synced: 3 months ago
JSON representation
typed is a typed Go stdlib for generics
- Host: GitHub
- URL: https://github.com/superpaintman/typed
- Owner: SuperPaintman
- License: mit
- Created: 2022-01-27T17:32:42.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-01-28T01:23:28.000Z (over 3 years ago)
- Last Synced: 2025-01-15T00:40:52.623Z (4 months ago)
- Topics: generic, generics, go, go-generics, go118, golang
- Language: Go
- Homepage: https://pkg.go.dev/github.com/SuperPaintman/typed
- Size: 12.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# typed [🇹]
`typed` is a typed Go stdlib (almost) for generics.
> It is a set of experimental packages that heavily use features of
> [Go 1.18](https://tip.golang.org/doc/go1.18) generics.---
## Examples
```go
package mainimport (
"fmt"
"github.com/SuperPaintman/typed/mathx"
)func main() {
v := mathx.Max(1, 20)fmt.Printf("v: %d\n", v)
// Output: v: 20
}
``````go
package mainimport (
"fmt"
"github.com/SuperPaintman/typed/containerx/stackx"
)func main() {
var stack stackx.Stack[int]stack = append(stack, 7331)
stack = append(stack, 1337)v1, ok := stack.Pop()
fmt.Printf("v1: %d, ok: %v\n", v1, ok)
// Output: v1: 1337, ok: truev2, ok := stack.Pop()
fmt.Printf("v2: %d, ok: %v\n", v2, ok)
// Output: v2: 7331, ok: truev3, ok := stack.Pop()
fmt.Printf("v3: %d, ok: %v\n", v3, ok)
// Output: v3: 0, ok: false
}
``````go
package mainimport (
"encoding/json"
"fmt"
"github.com/SuperPaintman/typed/nullx"
)func main() {
var post struct {
Description nullx.Nullable[string]
Likes nullx.Nullable[int]
Tags nullx.NonNullableSlice[string]
}if err := json.Unmarshal([]byte(`{ "likes": 0 }`), &post); err != nil {
panic(err)
}fmt.Printf("%#v\n", post)
// Output: struct { Description nullx.Nullable[string]; Likes nullx.Nullable[int]; Tags nullx.NonNullableSlice[string] }{Description:nullx.Nullable[string]{Value:"", Valid:false}, Likes:nullx.Nullable[int]{Value:0, Valid:true}, Tags:nullx.NonNullableSlice[string](nil)}b, err := json.Marshal(post)
if err != nil {
panic(err)
}fmt.Printf("%s\n", b)
// Output: {"Description":null,"Likes":0,"Tags":[]}
}
```---
## FAQ
> **Question**:
>
> Should I use this package in production?
>
> **Answer**:
>
> At the moment, I think not. As long as
> [Go 1.18](https://tip.golang.org/doc/go1.18) is in beta, it is better to
> taste Go generics, but it is questionable to use them in real projects.
>
> Later, probably yes.> **Question**:
>
> Why every package has `x` suffix?
>
> **Answer**:
>
> To avoid collisions with the std Go packages and save idiomatic
> [package naming](https://go.dev/blog/package-names) simultaneously.
>
> Also, `t` (or any other letters) looks awkward and unreadable in package names
> (like `matht`).---
#### License
[MIT](./LICENSE)
---
With 🫀 by [Aleksandr Krivoshchekov (@SuperPaintman)](https://github.com/SuperPaintman)