loco-rs / loco

🚂 🦀 The one-person framework for Rust for side-projects and startups
https://loco.rs
Apache License 2.0
5.11k stars 210 forks source link

consider removing / reducing the use of the prelude in generated code and examples #874

Open joshka opened 6 days ago

joshka commented 6 days ago

In Ratatui, we introduced a fairly extensive prelude with commonly used types. After seeking feedback from users across reddit, discord, users.rust-lang.org and our forums, we found that there were numerous reasons to avoid doing this for most things. It's worth reading the feedback on the links in https://github.com/ratatui/ratatui/issues/1150 for context.

The biggest things were that it makes it difficult to know where various types come from, which is harmful when reading source code. The one place where there was a small amount of consensus that preludes may be useful was for importing traits, but even that seemed to sometimes not be useful. In particular if your goal is a framework that targets rust beginners, I'd avoid this too.

The other problem is that preludes have a tendency to cause conflicts with other imported types in ways that are often annoying to fix.

jondot commented 5 days ago

Thanks, those are excellent insights. I appreciate learning from your already extensive experience here. I hope we can balance importing from prelude and codegen.

The thing with code gen, for example, models or migrations -- is that we're letting users pick data types, and they might pick anything on the CLI, so that would translate into actual Rust type, and the codegen framework is still text-based (has no reach to the Rust compiler or rust-analyzer to infer which types need to be imported), so using prelude gave use this umbrella catch-all for this case.

I guess we can create some islands of prelude groups, mostly to tackle codegen being comfortable enough (e.g. prelude types for models and migration, prelude for controller, etc.), and then to be more specific in all other areas

joshka commented 5 days ago

The thing with code gen, for example, models or migrations -- is that we're letting users pick data types, and they might pick anything on the CLI, so that would translate into actual Rust type, and the codegen framework is still text-based (has no reach to the Rust compiler or rust-analyzer to infer which types need to be imported), so using prelude gave use this umbrella catch-all for this case.

I'm not sure I entirely understand the problem here. The prelude usages I saw seemed like places where inserting real imports at the top of the file would have been ok. Can you expand on this a little.