Thats an interesting way to do it rog. Although there are small penalties in performance and code complexity it does achieve the same effect as using a default argument in a struct without changing the language in anyway. As usual thank you rog for adding another pattern to my ever growing arsenal.
Putting that aside, I don't agree that the performance penalty for doing:
var f [200]Foo
is that much different between the current method and calling a constructor 200 times. The reason being that, if you are creating [200]Foo, it can be assumed that you'll have to go through each element and manually create and assign a Map to each of the Foo. Even if we use lazy initialization, the checks used in every method, can arguably negate any performance gains from not calling a constructor. So essentially the performance should be the same.
In addition, with the current mechanics, the amount of code you'll need to write increases quickly when you embed more slices that require initialization into a struct (non-slice/array fields don't have much of an effect though). For example:
type Foo struct {
data interface{}
m map[string]int
f [200]Buu
}
type Buu struct {
data interface{}
m map[string]int
}
to properly initialize:
var f [200]Foo
you'll first need an outer loop that creates a map for each of the Foo's and assign them to the Foo.m. Than you'll need another inner loop to loop through Foo.f. To create a map for each Buu.
Compare this to using default arguments in struct. Where we could do
type Foo struct {
data interface{}
m := make(map[string]int)
f [200]Buu
}
type Buu struct {
data interface{}
m := make(map[string]int)
}
we would not need any loops for initialization, which I argue makes things much simpler and less verbose. And be reminded that this is only a single slice embedded in a struct.
Having said that though, I do believe that the current implementation is much simpler implementation wise and that allows for maintainability, speedy execution, as well as maintains language simplicity. These are all great things.
Lastly, just out of curiosity rog, if you had to implement something like a default argument for structs how would you do it syntax wise, and implementation wise? (This is just for fun and the sake of discussion).