pie icon indicating copy to clipboard operation
pie copied to clipboard

Functions depending on other Functions

Open Deleplace opened this issue 6 years ago • 2 comments

package main

import (
	"fmt"
)

func main() {
	a := myFloats{1.0, 2.0, 3.0}
	fmt.Println(a.First())
}

//go:generate pie myFloats.First
type myFloats []float64

yields

./myfloats_pie.go:5:11: ss.FirstOr undefined (type myFloats has no field or method FirstOr)

Currently, the developer hits this error and figures out they must explicitly ask for FirstOr:

//go:generate pie myFloats.First.FirstOr

It would be nice if Pie somehow knew it requires to generate FirstOras a dependency for First. I'm not saying this is trivial though, and also the inconvenience is minor.

Deleplace avatar May 12 '19 09:05 Deleplace

Generating all Pie functions with //go:generate pie myType.* seems to be the best choice in general.

My measurements confirm that the final compiled binary is not bigger when lots a unused functions are generated. Unused funcs are correctly discarded by the compiler.

Deleplace avatar May 12 '19 09:05 Deleplace

Ah, that's a good catch. I have thought before about how to handle this because there are several other cases where some functions would be easier to implement if they could use other functions that may not be included.

One solution I can think of right now is to add dependencies when registering the functions:

// functions/main.go

{"First", "first.go", ForAll, []string{"FirstOr"}},

The pie command has to build a list of functions anyway (from CLI args), this shouldn't be too hard to merge with the existing list.

elliotchance avatar May 12 '19 23:05 elliotchance