Skip to content

Prevent duplicate initialisation #1196

Answered by JacobOaks
tumelowill asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @tumelowill - there currently isn't a satisfying way (that I'm aware of) to provide a type as both itself and as an interface in one line. However, you can avoid duplicate initialization by adding a "constructor" that converts the struct to an interface (see here as well).

// Prints "Hello World!" once
fx.New(
	fx.Provide(
		NewThing,
		func(t *Thing1) Thing2 { return t },
	),
	fx.Invoke(func(*Thing1, Thing2) {
	}),
)

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@tumelowill
Comment options

@tumelowill
Comment options

Answer selected by tumelowill
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants