Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wire generate: Support generics #354

Open
pauky opened this issue Mar 31, 2022 · 2 comments
Open

Wire generate: Support generics #354

pauky opened this issue Mar 31, 2022 · 2 comments

Comments

@pauky
Copy link

pauky commented Mar 31, 2022

Is your feature request related to a problem? Please describe.

When I used wire to build the code with go generics in golang:1.18, but It didn't work and throw some error.
There are some code, command and log in Additional context.

Describe the solution you'd like

I think that wire should support code with go generics as soon as possible for golang:1.18. It must a great power to help developers to build the apps.

Describe alternatives you've considered

Nothing.

Additional context

Code

main.go

package main

import "fmt"

type Message struct {
	Msg string
	V   int
}

func NewMessage(v int) Message {
	return Message{Msg: "Hi there!", V: v + 1}
}

type Greeter struct {
	Message Message // <- adding a Message field
}

func NewGreeter(m Message) Greeter {
	return Greeter{Message: m}
}

func (g Greeter) Greet() Message {
	return g.Message
}

type V interface {
	int | float32
}

func NewV[T V]() T {
	return 2
}

type Event struct {
	Greeter Greeter // <- adding a Greeter field
}

func NewEvent(g Greeter) Event {
	return Event{Greeter: g}
}

func (e Event) Start() {
	m := e.Greeter.Greet()
	fmt.Println(m.Msg, m.V)
}

func main() {
	e := InitializeEvent()

	e.Start()
}

// func main() {
// 	v := NewV[int]()
// 	message := NewMessage(v)
// 	greeter := NewGreeter(message)
// 	event := NewEvent(greeter)
// 	event.Start()
// }

wire.go

//go:build wireinject

package main

// The build tag makes sure the stub is not built in the final build.

import "github.com/google/wire"

func InitializeEvent() Event {
	wire.Build(NewEvent, NewGreeter, NewMessage, NewV)
	return Event{}
}
Command
wire
Error log
wire: ~/test/wire/main.go:27:6: expected ';', found '|'
wire: ~/test/wire/main.go:31:2: expected '}', found 'return'
wire: ~/test/wire/main.go:31:9: expected ';', found 2
wire: ~/test/wire/main.go:27:2: int is not an interface
wire: ~/test/wire/wire.go:10:47: undeclared name: NewV
wire: generate failed
@stytchiz
Copy link
Collaborator

#360 provides a workaround. See comment for details. Note that this may not work for many use cases of generics. Full support is not planned.

@dcormier
Copy link

dcormier commented Feb 9, 2024

Full support is not planned.

Wow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants