News for hackers. Stuff that matters.
Re:Dysfunctional Options Pattern in Go by abahlo on Saturday July 20, 2024 @02:26PM (#41018525)
Isn’t this just the builder pattern without Build()?
Re:Dysfunctional Options Pattern in Go by rednafi on Saturday July 20, 2024 @03:00PM (#41018802)
The conclusion mentions that.
Re:Dysfunctional Options Pattern in Go by ivanjermakov on Saturday July 20, 2024 @04:17PM (#41019444)
The problem with options in non-ML languages is that invalid state is unrepresentable[1]. E.g, in Java Option<Integer> can either be null, empty, some with null and some with a number.

To solve this problem, a language have to disallow null/nil as a bottom type and provide nice syntax to initialize types with optional types.

[1]: https://geeklaunch.io/blog/make-invalid-states-unrepresentab...

Re:Dysfunctional Options Pattern in Go by mmaniac on Thursday July 25, 2024 @09:58AM (#41068770)
Optional types don't fix this. The problem here is not having default (or named) function arguments. With optionals, you still have to pass every arg explicitly.

Languages with both of these features like Python don't end up using the builder pattern or reinventing it like this author.

Re:Dysfunctional Options Pattern in Go by evanmoran on Saturday July 20, 2024 @06:26PM (#41020594)
I like this write up. Nicely done! One think I have enjoyed doing is using named parameters as the options, but it does require a second struct only for the option parts:

    NewObject(
      required1, 
      required2, 
      &ObjectOptions{
        Option1: “foo”,
        // Option2 skipped
        Option3: “bar”,
    })
I can see the merits of both (two structs is tedious, for sure), but I wanted to share in case others had thoughts or alternate ideas!
Re:Dysfunctional Options Pattern in Go by iambvk on Sunday July 21, 2024 @09:48AM (#41025074)
What are the downsides of just adding new methods on the `Config` structure?

    func (c *Config) WithFizz(...) {...}

    func (c *Config) WithBazz(...) {...}
Re:Dysfunctional Options Pattern in Go by rednafi on Sunday July 21, 2024 @10:09AM (#41025227)
Isn't it essentially doing exactly that? The only difference is that it returns a pointer to the struct from each method. The advantange of doing that is it allows you to compose it as such `WithFizz(...).WithBazz(...)`.

If you don't need the composition, you can just add the methods as above and call it a day.

Re:Dysfunctional Options Pattern in Go by tail_exchange on Sunday July 21, 2024 @10:48AM (#41025504)
Looks cleaner and simpler. I like this pattern!