Constructors #747
Replies: 3 comments 3 replies
-
On the cognitive load part - your LSP should handle telling you what parameters are required. On the faster part - if you want that, I'd suggest that this use case should be using If constructors are always empty, then there's no point in having a I think where I differ from your perspective is that I see constructor methods as a convenience idiom in rust, not the same as a constructor which is the only way to crate items in other languages. So it allows for the idea that a rust constructor is supposed to be used for most common use case, and not just to provide the required fields. In ratatui, we've inherited types that act both as builders and the runtime value in the same object, which makes things a bit painful to play with, as such we sort of run into these sorts of slightly ambiguous idiomatic things which would not be there if we had actual builder types. So we make do with that and try to come up with fairly reasonable set of APIs that walk that line between builders and constructed types. In the end, my argument for making title and borders parameters to |
Beta Was this translation helpful? Give feedback.
-
Going even further, if If
I actually expect the opposite, if both exist I expect them to do different things. More generally I always feel like
Which doesn't mean anything. Or even worse, means different things to different people. TLDR; According to me:
|
Beta Was this translation helpful? Give feedback.
-
This seems like a different side of #978. #978 is about having only useful structs while As outlined in the linked issue I think the only useful struct approach is better in most cases. Only a few structs have multiple different ways like Style or Block so they can exist on their own and have no minimal required content. Example: |
Beta Was this translation helpful? Give feedback.
-
Originally posted by @kdheepak in #736 (comment)
In languages like Python, all of these can build a
Block
:In Rust, we have to use this pattern for the same benefits (not breaking future features, configuration of defaults, changing order in which you set fields, etc):
which my understanding of is why it the convention for building
struct
s.Also, you can build a
Block
without a title usingBlock::new().borders(Borders::ALL)
. Along the same lines of theParagraph
point you pointed out,Block::new(title, borders)
implies these are required and doesn't hint at other options (e.g. padding). It also is easier to end up breaking in the future if we decide to add a new field that is of equal importance to title and borders.fwiw, I like that
Paragraph::new
takestext
because as you said it is useless without content. But I also don't remember everynew
function in ratatui and their signatures. I don't want to changeParagraph::new
or for example the constructor forTable::new
, I was trying to give an example where what I type on autopilot doesn't work and the cognitive overhead of different ways of constructing something.Also, it is generally way faster for me to type
SomeWidget::new().<TAB>
and wait for autocomplete to give me options than it is to typeSomeWidget::new(<TAB>
, then complete the required arguments, and then continue withSomeWidget::new(...).<TAB>
to look for more options. I find it ergonomic when I get.
autocomplete for all available optional configuration.Beta Was this translation helpful? Give feedback.
All reactions