Replies: 5 comments 3 replies
|
I believe it's due to the functional roots from the Elm Architecture where immutability is preferred. Personally I will have functions that use pointer receivers in a |
|
but it also works with pointers, thus i'm curious about the thoughts behind this |
|
I wondered about this as well, until I learned more about both Go and Bubbletea and realized two things:
In all but the most trivial examples, I find it clearer to satisfy
More on all that at https://google.github.io/styleguide/go/decisions#receiver-type I think of the the |
|
I'm a long time Go programmer but have only been using bubbletea for a little while now, it's pretty cool to write nice UI's with Go! Who needs Flutter when you can write TUIs!? 😀 However, this is one of the things in bubbletea that frustrates me a little bit. The design feels inconsistent as there are two ways of writing and using Take for example: func (m myModel) Update(msg tea.Msg) (myModel, tea.Cmd) {
switch msg := msg.(type) {
case someThing:
m.answer = 42 // seems ineffective, linters will warn about this. (1)
m.table.SetWidth(10) // fine to do
}
var cmd tea.Cmd
m.table, cmd = m.table.Update(msg) // not idiomatic Go? (2)
return m, cmd
}
I think it would be best if the tea.Model return value would be dropped from Update(), and pointer receivers used for all models. I understand that changing this will be breaking, but the migration is fairly straightforward. It changes the framework to a single way of writing and using Resulting example: func (m *myModel) Update(msg tea.Msg) (tea.Cmd) {
switch msg := msg.(type) {
case someThing:
m.answer = 42
m.table.SetWidth(10)
}
cmd := m.table.Update(msg)
return cmd
} |
|
this has been a point of confusion for me as well. The moment you introduce a map into your model state, you have to do a lot more work to ensure that your model is immutable (as maps are pointers by default), is it even worth the effort? |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
i was wondering why all
tea.Model's have theirInit,UpdateandViewmethods as value receivers instead of pointer receivers (which leads to returning thetea.Modelafter each call toUpdate). any particular reason behind this design choice?PS: sorry if i've not spotted any obvious reasons.
All reactions