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

Questions about type-safety #108

Open
jakob-lilliemarck opened this issue Jan 21, 2025 · 0 comments
Open

Questions about type-safety #108

jakob-lilliemarck opened this issue Jan 21, 2025 · 0 comments

Comments

@jakob-lilliemarck
Copy link

Really a question more than an actual issue. I've been scanning through the docs but from what I've seen the information on this matter is quite thin. I apologize if this is the wrong place for questions, in which case, I ask you to please point me in the direction of where you wish me to re-post it 🙏

This library presents itself as beeing "type-safe":

It may replace the uber/dig fantastic package in simple Go projects. samber/do uses Go 1.18+ generics and therefore offers a typesafe API.
I am struggling a bit to understand what is meant by that in this context really, and how type-safety is enforced. It seems to me that types are enforced at runtime, which really doesn't provide much "safety" - but perhaps I'm doing something wrong?

I pulled down the repository and took the examples/simple/example.go for a spin. I tried to comment all of the methods that provide Wheel, and was hoping to get a static type-error in my editor or a compile error as nothing of type Wheel is provided, but it turns out that the code compiles and that "Simple example" is indeed printed to the terminal upon invoking it 🤔

package main

import (
// preceeding code unmodified...

func main() {
	fmt.Printf("Simple example\n")
	injector := do.New()

	// provide wheels
	// do.ProvideNamedValue(injector, "wheel-1", &Wheel{}) 
	// do.ProvideNamedValue(injector, "wheel-2", &Wheel{})
	// do.ProvideNamedValue(injector, "wheel-3", &Wheel{})
	// do.ProvideNamedValue(injector, "wheel-4", &Wheel{})

   // ...rest of the code unmodified
}

We're currently using uber-fx for dependency injection, and I am not a big fan of the magic it provides. I am trying to understand if samber/do somehow does away with some of that magic, and how it compares in terms of type-safety.

Grateful for any insight into the above, or any pointers to pieces of documentation on type-safety which I may have missed

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

1 participant