Last active
August 29, 2025 01:14
-
-
Save mmorton/8e92b4eb8c5cbb0d27079f3b24f44f03 to your computer and use it in GitHub Desktop.
Compose functional options.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package compose | |
| type Composer[T any] func(*T) | |
| func Create[T any, C ~func(*T)](opts ...C) *T { | |
| m := new(T) | |
| Using(m, opts...) | |
| return m | |
| } | |
| func Using[T any, C ~func(*T)](m *T, opts ...C) *T { | |
| for _, opt := range opts { | |
| opt(m) | |
| } | |
| return m | |
| } | |
| func With[T any](fn func(*T)) Composer[T] { | |
| return fn | |
| } | |
| func Combine[T any, C ~func(*T)](opts ...C) Composer[T] { | |
| return func(m *T) { | |
| Using(m, opts...) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment