hexops / valast

Convert Go values to their AST
Other
308 stars 16 forks source link

Function in structs breaks normal printing #23

Closed kaz-as closed 1 year ago

kaz-as commented 1 year ago

Valast cannot convert structs if they have functions:

package main

import (
    "fmt"

    "github.com/hexops/valast"
)

func SomeFunc() {}

type Struct struct {
    NormalField int
    FuncField   func()
}

func main() {
    fmt.Println(valast.String(&Struct{
        NormalField: 123,
        FuncField:   SomeFunc,
    }))
}

prints:

valast: cannot convert value of type func()

while I anticipate:

&Struct{NormalField: 123, FuncField: func()}

slimsag commented 1 year ago

Sorry, but functions and channels are generally out of scope. Valast is used to generate valid Go code, and is used by many to actually write Go code back into a Go source file. Your anticipated snippet of code would not be valid code.