Giter Site home page Giter Site logo

support extending generic type about genny HOT 1 OPEN

cheekybits avatar cheekybits commented on September 13, 2024 3
support extending generic type

from genny.

Comments (1)

ThinCats avatar ThinCats commented on September 13, 2024

I think it's a bit difficult to implement the same syntax as Java :), but we can use the golang style to nest the interface.

In your example, you can use existed interface defined in other packages

(The following code will work on genny df3d48a)

package genny
import (
  "bytes"
  "fmt"
)

type Something interface {
  fmt.Stringer
}

type SomethingQueue struct {
  items []Something
}

...

func (q *SomethingQueue) String() string {
  var buf bytes.Buffer
	for _, item := range q.items {
		buf.WriteString(item.String())
	}
	return buf.String()
}

It's also easy to extend other interfaces, like json.Marshaler. We can also nest with generic.Type to indicate the interface is generic

type Something interface {
  generic.Type

  json.Marshaler
  fmt.Stringer
}

We can do this because genney will replace the string identifier of the file, regardless of its actual type. In this way, we can extend more complex types while being type-safe.

I have summarized several common usages used in my toy project.

  1. Type Object extends to struct (has fields)
type Object struct {
  generic.Type

  name string
  age int
}

// Now you can use it in function without compile errors
func doSomeThing(o *Object) (string, int) {
  ...
  return o.name, o.age
}
  1. Type Object extends to interface (has method)
type Object interface {
  generic.Type // can be removed

  run() Object
  stop() Object
}

// use it as normal interface
func operate(o Object) {
  o.run().stop()
}
  1. Mock package
// file1: demo.go
type mockPackage struct {
  // package function
  Log(...interface{}) error
  // package exported variable
  ColorRed int
  ColorGreen int
  // Not find a way to mock exported Type
}

var PackageName mockPackage

// file2: demo_template.go
// Use in another file
func normalFunc() {
  PackageName.Log("Hello", "Good")
  color := PackageName.ColorGreen
  ...
}

// use command
// geeny -in=demo_template.go -out=gen_demo.go gen "PackageName=mypkg"

Hope to be helpful :)

from genny.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.