I wish to use a type parameter as a function. I first tried a struct but struct type parameters must be used as members and I don't wish to actually instantiate the parameter.
I've made a minimal example to show what I'm trying to do, but this doesn't compile:
Thanks for your quick response. I've tried what you're suggesting before, but that doesn't work.
13 | impl<T, Norm> BestThingFinder for T
| ^^^^ unconstrained type parameter
In general I would like to create a Type NormedBestThingFinder, that takes a Norm as an argument and implements BestThingFinder. This way I would be able to create multiple different NormedBestThingFinders with different norms.
Another thing that I've tried is to use a struct with a dummy member. That seems to compile but I'm not sure that's a proper solution. Since I only want Norm to be used statically, I don't want Norm to be a (member) object.
Can you give an example of calling the trait method with a fully concrete type? It would help to think about it and experiment. I find it strange that you have no self params.
This works, but as far as I understand this means NormedBestThingFinder holds an instance of a Norm trait object. That's what I originally wanted to avoid. I want my Type to take Norm as a type parameter and not as an object. The best workaround I found is using PhantomData, but I thought there might be a nicer solution using traits.
I think I haven't been quite clear about what I wanted to achieve. I've found a solution using PhantomData similar to @paramagnetic's suggestion, but preferably I would have a solution without any unused placeholder struct members.
Here's a more complete example of how the trait can be used:
Using a PhantomData field is quite common and has no significant drawbacks, so I suggest becoming accustomed to using it. It is zero-sized so it is only used by the type system -- it doesn't exist at runtime. You would need a self parameter anyway, since trait objects require one.
PhantomData is an unnecessary annoyance if you have a zero-sized marker type that you can just make up instances of. That's exactly why I didn't use a PhantomData. Just make the marker type Default or whatever, and instantiate it, it's trivial.
I unfortunately failed to understand what's wrong with storing an instance. If you are using the norm as a purely type-level marker, you shouldn't make it anything but a zero-sized type (likely a unit struct) that you can instantiate without any additional requirements.
That's a good point, in my effort to do away with a marker type I completely missed that it's zero-sized either way. So just using the trait object itself as suggested by @paramagnetic is the cleaner solution. Thanks for pointing that out!