From 394334bdb86303b1f7c232585b97623620aa6b95 Mon Sep 17 00:00:00 2001 From: Viktor Nikolaiev Date: Sun, 2 Feb 2025 23:41:44 +0200 Subject: [PATCH] Add Stream[T] type alias with Go 1.24 generic alias support (#51) --- .github/workflows/check.yaml | 2 +- README.md | 9 +++++++++ wrap124.go | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 wrap124.go diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index f75369b..fc5f2e9 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -11,7 +11,7 @@ jobs: gotest: strategy: matrix: - go-version: [1.21.x, 1.23.x] + go-version: [1.21.x, 1.23.x, 1.24.0-rc.2] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 diff --git a/README.md b/README.md index 421a8d8..c4e2298 100644 --- a/README.md +++ b/README.md @@ -428,6 +428,15 @@ func StreamUsers(ctx context.Context, query *mockapi.UserQuery) <-chan rill.Try[ } ``` +**Note:** Starting from Go 1.24, thanks to generic type aliases, the return type of the `StreamUsers` function +can optionally be simplified to `rill.Stream[*mockapi.User]` + +```go +func StreamUsers(ctx context.Context, query *mockapi.UserQuery) rill.Stream[*mockapi.User] { + ... +} +``` + ## Go 1.23 Iterators Starting from Go 1.23, the language added *range-over-function* feature, allowing users to define custom iterators diff --git a/wrap124.go b/wrap124.go new file mode 100644 index 0000000..7f642f9 --- /dev/null +++ b/wrap124.go @@ -0,0 +1,19 @@ +//go:build go1.24 + +package rill + +// Stream is a type alias for a channel of [Try] containers. +// This alias is optional, but it can make the code more readable. +// +// Before: +// +// func StreamUsers() <-chan rill.Try[*User] { +// ... +// } +// +// After: +// +// func StreamUsers() rill.Stream[*User] { +// ... +// } +type Stream[T any] = <-chan Try[T]