How to write a func that returns a func that returns a Generic type

huangapple go评论75阅读模式
英文:

How to write a func that returns a func that returns a Generic type

问题

我有以下的Go代码,无法编译

我想要的是如何编写一个返回一个返回泛型类型的函数的函数。

package main

import (
	"log"

	"golang.org/x/exp/constraints"
)

type logAndReturnDefault[T constraints.Ordered] func(input T) T

func iWillRewturnAFunc(name string) logAndReturnDefault{
	return func(input string){
		log.Println("我在将作为返回值返回的函数中")
	}
}

func Add[T constraints.Ordered](a T, b T) T {
	i := iWillRewturnAFunc("ABC")
	return a + b + i
}

func main() {
	result := Add(1, 2)
	result2 := Add(1.2, 3.4)
	log.Printf("", result)
	log.Printf("", result2)
}

我的意图是使用iWillRewturnAFunc来返回一个可以返回泛型类型的func

但是,当然我失败了,因为我无法确定iWillRewturnAFunc函数的声明语法。

英文:

I have the following go code which DOES NOT COMPILE

What I am looking for is how to write a func that returns a func that returns a generic type

package main

import (
	"log"

	"golang.org/x/exp/constraints"
)

type logAndReturnDefault[T constraints.Ordered] func(input T) T

func iWillRewturnAFunc(name string) logAndReturnDefault{
	return func(input string){
		log.Println("I am in the func that will be returned as a returned value")
	}
}

func Add[T constraints.Ordered](a T, b T) T {
	i := iWillRewturnAFunc("ABC")
	return a + b + i
}

func main() {
	result := Add(1, 2)
	result2 := Add(1.2, 3.4)
	log.Printf("", result)
	log.Printf("", result2)
}

My intention is to use iWillRewturnAFunc to return a func that can return a generic type.

But of-course I am failing, as I am not able to identify the syntax for the declaration of iWillRewturnAFunc func.

答案1

得分: 1

你想要实现的目标与你的代码之间存在一些不匹配。

通用函数实际上就是一个通用类型,所以返回通用类型的函数必须是通用的:

func iWillRewturnAFunc[T constraints.Ordered](name string) logAndReturnDefault[T] {
	return logAndReturnDefault[T](func(input T) T {
        var t T
        ...
		return t
	})
}

但是你试图做的是返回一个特定的字符串实例,这是可以实现的:

func iWillRewturnAStrFunc(name string) logAndReturnDefault[string] {
	return func(input string) string {
		log.Println("我在将作为返回值返回的函数中")
		return ""
	}
}
英文:

There is some mismatch between what you want to achieve and your code.

A generic function is nothing but a generic type, so a function returning a generic type must be generic:

func iWillRewturnAFunc[T constraints.Ordered](name string) logAndReturnDefault[T] {
	return logAndReturnDefault[T](func(input T) T {
        var t T
        ...
		return t
	})
}

But what you are trying to do is to return a specific instance for string, which can be done:

func iWillRewturnAStrFunc(name string) logAndReturnDefault[string] {
	return func(input string) string {
		log.Println("I am in the func that will be returned as a returned value")
		return ""
	}
}

答案2

得分: 0

回答我自己的问题。下面的代码现在可以编译和运行

泛型的返回类型由Add函数决定,并作为嵌套函数的参数提供,因为它们都使用相同的泛型接口T constraints.Ordered

就像这样,也有一种方法可以处理两种不同的泛型类型,通过定义U constraints.Ordered(或任何其他接口)

package main

import (
	"log"
	"golang.org/x/exp/constraints"
)

type LogAndReturnDefaultFunc[T constraints.Ordered] func(T) T

func iWillReturnAFunc[T constraints.Ordered](name T) LogAndReturnDefaultFunc[T] {
	log.Println(name)
	// or do something with the *name"
    return LogAndReturnDefaultFunc[T] (func(input T) T {
        t := name
		log.Println(input)
		// Do something with input
        return t
    })
}

func Add[T constraints.Ordered](a T, b T) T {
	// Call `iWillReturnAFunc``, that will return a func
    returnedFunc := iWillReturnAFunc(a)
	// Run the returned func to get its o/p
	outputOfReturnedFunc := returnedFunc(b)
	//Do something with the Output of the func
	log.Println(outputOfReturnedFunc)
    return a + b + outputOfReturnedFunc
}

func main() {
	result := Add("abc", "DEF")
	result2 := Add(1.2, 3.4)
	log.Printf("%v\n", result)
	log.Printf("%v\n", result2)
}
英文:

Answering my own question. In the below code NOW COMPILES AND RUNS.

The return Type of generic is decided by the Add function. will be provided to the nested function as both are using the same generic interface T constraints.Ordered .

Just like this, There is a way to work with 2 different Generic type as well. by defining U constraints.Ordered (or any other interface) .

package main

import (
	"log"
	"golang.org/x/exp/constraints"
)

type LogAndReturnDefaultFunc[T constraints.Ordered] func(T) T

func iWillReturnAFunc[T constraints.Ordered](name T) LogAndReturnDefaultFunc[T] {
	log.Println(name)
	// or do something with the *name"
    return LogAndReturnDefaultFunc[T] (func(input T) T {
        t := name
		log.Println(input)
		// Do something with input
        return t
    })
}

func Add[T constraints.Ordered](a T, b T) T {
	// Call `iWillReturnAFunc``, that will return a func
    returnedFunc := iWillReturnAFunc(a)
	// Run the returned func to get its o/p
	outputOfReturnedFunc := returnedFunc(b)
	//Do something with the Output of the func
	log.Println(outputOfReturnedFunc)
    return a + b + outputOfReturnedFunc
}

func main() {
	result := Add("abc", "DEF")
	result2 := Add(1.2, 3.4)
	log.Printf("%v\n", result)
	log.Printf("%v\n", result2)
}

huangapple
  • 本文由 发表于 2023年6月26日 01:59:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定