jcburley / joker

Small Clojure interpreter and linter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Too Much Code Generated

jcburley opened this issue · comments

From @jcburley on November 8, 2018 6:13

When converting named types such as structs, a single function should be emitted to perform any potentially length conversion, and the wrapper functions changed to call that single function.

Consider this code:

func interfaceByIndex(index int) Object {
	res1, res2 := net.InterfaceByIndex(index)
	res := EmptyVector
	map1 := EmptyArrayMap()
	map1.Add(MakeKeyword("Index"), MakeInt((*res1).Index))
	map1.Add(MakeKeyword("MTU"), MakeInt((*res1).MTU))
	map1.Add(MakeKeyword("Name"), MakeString((*res1).Name))
	vec2 := EmptyVector
	for _, el2 := range (*res1).HardwareAddr {
		vec2 = vec2.Conjoin(MakeInt(el2))
	}
	map1.Add(MakeKeyword("HardwareAddr"), vec2)
	map1.Add(MakeKeyword("Flags"), MakeInt((*res1).Flags))
	res.Conjoin(map1)
	res.Conjoin(string(res2))
	return res
}

The block beginning with map1 appears several times in different wrapper functions. It should be implemented in a single function, e.g. func interface(res Interface) (map Object) { ... }, and called by the above code, e.g. map1 := interface(el1). That would probably (unless Go optimizes this away itself) reduce code size noticeably, as more APIs are implemented.

Copied from original issue: jcburley/gostd2joker#5

No longer an issue, now that Go objects have been introduced and are what Go API calls consume and produce. (Conversion routines are, or will be, generated per type, in one place for each type.)