invalid recursive type in a struct in go

后端 未结 2 1464
Happy的楠姐
Happy的楠姐 2021-02-04 23:54

I am new to the Go programming language and I have an assignment to create and interpreter but I am running into the following problem:

I want to define an Environment a

相关标签:
2条回答
  • 2021-02-05 00:14

    You need to define Environment as:

    type Environment struct {
        parent *Environment // note that this is now a pointer
        symbol string
        value  RCFAEValue
    }
    

    Otherwise the compiler has no way to figure out what the size of the Environment structure is. A pointer's size is known, but how big is something that contains itself? (And the inner struct contains itself as well, as does the inner inner struct, and so on.)

    Creating the Environment will then look like:

    Environment{&fun_Val.ds, fun_Val.param, exp.arg_exp.interp(env)}
    
    0 讨论(0)
  • 2021-02-05 00:19

    I hope this should fix the problem:

    Environment{&fun_Val.ds,fun_Val.param,exp.arg_exp.interp(env)}
    

    (The & is the 'address of' operator in Go.)

    0 讨论(0)
提交回复
热议问题