Implement constructors

This commit is contained in:
2024-09-12 00:45:41 +02:00
parent 440a6e33e6
commit ece38cda72
2 changed files with 18 additions and 2 deletions

View File

@@ -1,7 +1,11 @@
---@diagnostic disable: missing-return
{{range .Fields -}}
---@field {{.Name}} {{.Type}} {{.Comment}}
{{end}}
{{end -}}
{{range .Constructors -}}
---@overload fun({{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}: {{$param.Type}}{{end}}): {{$.ClassName}}
{{end -}}
---@class {{.ClassName}}
{{.ClassName}} = {
{{$n := len .Methods -}}
{{$methods := len .Methods -}}
@@ -19,5 +23,6 @@
{{end}}{{end}}
}
---@type ({{$.ClassName}} | fun(): {{$.ClassName}})
---@type {{$.ClassName}}
-- Define class globally so it's "available" to other files
{{$.ClassName}} = nil

11
main.go
View File

@@ -77,6 +77,17 @@ func main() {
Comment: "test method",
},
},
Constructors: []Constructor{
{
Params: []Param{},
},
{
Params: []Param{
{Name: "a", Type: "int", Comment: "test param 1"},
{Name: "b", Type: "string", Comment: "test param 2"},
},
},
},
}
ltemplate, err := template.New("class").Funcs(fns).Parse(templatestr)