Skip to content

Import generic functions #1

Description

@Kolterdyx

Hello! I came across this project from your post on Reddit. I wanted to say so far it looks quite promising!

Is generic converter support a thing that could be done? It would be very useful for this case:

//go:build convgen

package main

import (
	"github.com/sublee/convgen"
)

func OptionalConverter[T any](v *T) Optional[T] {
	if v == nil {
		return Optional[T]{}
	}
	return Optional[T]{
		Value: *v,
		Set:   true,
		Valid: true,
	}
}


var mod = convgen.Module(
	convgen.ImportFunc(OptionalConverter), // this doesn't compile, just an example
)

var A2B = convgen.Struct[A, B](mod)

And then in the main file:

package main

type Optional[T any] struct {
	Value T
	Valid bool
	Set   bool
}

type A struct {
	Field *string
}

type B struct {
	Field Optional[string]
}

I understand this may be complex to implement, but since the generator is based on AST parsing and not actual execution of the configuration code, I think it may be doable, for example checking for a specific kind of generic instance with a dummy type:

var mod = convgen.Module(
	convgen.ImportFuncGeneric(OptionalConverter[convgen.G]), // this would compile, and the parser could check for it
)

The generated code would the use the proper generic instances. I'd be willing to come up with a PR if you think this is feasible

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions