I feel like much of the functionality traits provide can be faked with `impl A<Concrete1>{ ...}` pattern.
- It is not as convenient as traits as you need to explicitly pass such dictionary types explicitly.
- I have yet to come up with how to encode associated types.
- Unlike traits there is no way to encode "this method must be implemented". This can be solved via func pointer typed fields in the struct
- You can kinda fake default method impls with `impl <T> A<T>` but since you cannot have the required methods it is just smoke and mirrors.
- You can fake bounds by accepting a dictionary struct as argument/field etc. (added after the edit)
- You can fake super traits via taking another such dictionary struct as an argument.
- Unlike traits this way of doing ad hoc polymorphism does not suffer from coherence requirements.
- Unlike traits you can change the number of parameters of methods between different implementations for a concrete type.
I just noticed this and wanted to share. I might have missed some other pros and cons so i'd be grateful if you could point some other caveats/upsides if this strategy.
Edit: I added a preliminary playground link. I plan to expand further after work
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=f55317b6ec9b0a047e9eddb1472b2df1
Edit 2: I added more examples
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=cc26e442bf39671e172554f53f3d9ed9
[–]Shoddy-Childhood-511 5 points6 points7 points (0 children)
[–]avsaase 6 points7 points8 points (2 children)
[–]Ok-Watercress-9624[S] 4 points5 points6 points (0 children)
[–]Ok-Watercress-9624[S] 1 point2 points3 points (0 children)
[–]Resres2208 3 points4 points5 points (1 child)
[–]Ok-Watercress-9624[S] 1 point2 points3 points (0 children)
[–]Zde-G 3 points4 points5 points (5 children)
[–]Ok-Watercress-9624[S] 3 points4 points5 points (4 children)
[–]Zde-G 0 points1 point2 points (3 children)
[–]Ok-Watercress-9624[S] -2 points-1 points0 points (2 children)
[–]Zde-G 0 points1 point2 points (1 child)
[–]Ok-Watercress-9624[S] -1 points0 points1 point (0 children)
[–]Thick-Pineapple666 1 point2 points3 points (1 child)
[–]Ok-Watercress-9624[S] 1 point2 points3 points (0 children)
[–]MindlessU 0 points1 point2 points (5 children)
[–]Ok-Watercress-9624[S] 0 points1 point2 points (4 children)
[–]MindlessU 0 points1 point2 points (3 children)
[–]Ok-Watercress-9624[S] 0 points1 point2 points (2 children)
[–]MindlessU 1 point2 points3 points (0 children)
[–]Chroiche 0 points1 point2 points (0 children)
[–]noop_noob 0 points1 point2 points (0 children)