3 ms·
>In my experience though projects tend to start in a single crate, without great attention to their internal dependency graph, and once compilation time becomes
by _nalply 6y ago
>In my experience though projects tend to start in a single crate, without great attention to their internal dependency graph, and once compilation time becomes an issue, they have already created a spaghetti dependency graph that is difficult to refactor into smaller crates.
This happened to me too. One important problem to overcome is the orphan rule. This means: you can only implement a trait you own or for a type you own. If both the type and the trait are not in your crate, you are out of luck.
So my first attempt failed utterly. It was very disappointing. You see, by splitting the project in different crates I put one type and one trait in different crates.
After thinking about this problem for a few weeks I accepted that I needed to write wrappers, so called newtypes. But this hurt a lot. I managed to abstract away a lot of the boilerplate by writing macros but still am not very happy about the complexity.
Growing pains.
- tomsmeding 6y agoFunny how, I believe, the terms "orphan" and "newtype" originated in Haskell and were carried over to Rust. Evidence of the functional programming heritage that Rust has, I guess.
- ragnese 6y agoYes. For the unaware, the original creator of Rust wanted it to basically be OCaml with better concurrency. It used to have a more ML style syntax, green threads, etc.
- _nalply 6y agoAdding to my post: I understand the neccessity of the orphan rule (see other post about why). At least I am happy that there's a solution (newtypes) even if it is not nice.
- saiojd 6y agoI agree. Something must absolutely be done about the orphan rules, IMO they are Rust's biggest wart. They damage composition in a fundamental way, and gimp the otherwise extremely expressive trait system. I find it hard to believe that there exists no set of import declarations which remove all ambiguity, i.e., allowing orphan traits but requiring that they get imported explicitly.
- _nalply 6y agoI think this is a fundamentally difficult problem. Life is sometimes not nice and you have to choose. I chose to write newtype wrappers and to accept the complexity. > I find it hard to believe [...] I think you should read up stuff about the orphan rule, then you will understand better.
- saiojd 6y agoYes it does seem to be a very difficult problem. That being said, some hard problems deserve hard solutions!