goatlang / goat

Extended flavor of the Go programming language, aiming for increased value safety and maintainability
MIT License
63 stars 1 forks source link

Struct Initial Values #1

Open avivcarmis opened 2 years ago

avivcarmis commented 2 years ago

What's the best approach for us to provide struct initial values:

require accepting values is problematic because it will break backward compatibility of no argument initialization in zero value of structs, reflect.New and other use cases. can we work around it somehow?

I had something like this in mind:

private type Foo struct {
  private fieldA string // zero value
  private fieldB string = 1 // hard coded value
  private fieldC string = self.calculateFieldC() // calculated value
}

func (f *Foo) calculateFieldC() string {
  return f.fieldB * 3
}

More context in this blog post section.