Go语言支持操作符类型的变量吗?

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

Does Go support operator type variables?

问题

这可能不是一个很好的问题,因为我不知道任何支持这个特性的编译语言,但由于Go语言经常让我感到惊讶,我还是问一下:

为了练习,我正在用Go语言编写一个小型计算器程序。我想知道是否有一种方法可以声明和赋值一个类型为"Operator"的变量,这样我就可以像这样写:

var o Operator

o = +

var o1 Operator

o1 = /

并且编写如下的函数:

func DoOperation(a, b int, o Operator) int {
    return a o b
}

(不,我不是在问运算符重载的问题。)

我不知道有哪些编译语言支持这样的功能(我对此不是很了解)。我查看了Go语言的文档中的运算符部分,但没有找到相关内容。Go语言会再次让我感到惊喜吗?

编辑: 接受的答案指出Haskell支持这一点,

英文:

This is might not be such a good question, since I don't know of any compiled language that supports this feature, but since Go is constantly surprising me, I'll ask it anyway:

For my own practice, I am writing a little calculator program in Go. I'm wondering if there is a way I can declare and assign a variable of type "Operator", such that I could, for example, write:

var o Operator

o = +

var o1 Operator

o1 = /

and write function like this

func DoOperation(a,b int,o Operator) int{

    return a o b
    
}

(No, I am not asking about operator overloading.)

Offhand, I don't know of any compiled language that supports such a thing (I'm not an expert in this). I did look at the docs under operators and found nothing. Can Go surprise me again?

Edit: The accepted answer states that Haskell supports this,

答案1

得分: 7

不,Go运算符不是函数,因此没有有效的右侧表达式。它们以一种通用的方式工作,例如加法运算符适用于所有数值类型,而类似Haskell的中缀表示法也不受支持。

您将需要使用反射编写自己的“软”通用加法函数。

一个编译语言可以满足您所有要求的是Haskell。

英文:

No, Go operators are not functions and hence no valid right-hand expressions. They work in a generic way e.g. the plus-operator works on all numeric types and infix-notation a la haskell is not supported either.

You would have to write your own "soft"-generic addition function using reflection.

One compiled language that covers all of your requirements is Haskell.

答案2

得分: 5

你不能完全按照你说的那样做,但是你可以使用函数来代替。你需要为每个运算符编写函数,但这只需要很少的代码。

type BinaryOperator func(a, b int) int

func OpAdd(a, b int) int { return a + b }
func OpSub(a, b int) int { return a - b }

func ApplyBinaryOperator(a, b int, op BinaryOperator) int {
    return op(a, b)
}

你可以定义一个BinaryOperator类型的函数,它接受两个整数参数并返回一个整数。然后,你可以编写OpAddOpSub函数来执行加法和减法操作。最后,你可以使用ApplyBinaryOperator函数来应用任何二元运算符。

英文:

You can't do exactly what you say, but you can use functions instead. You have to write functions for each operator, but that's relatively little code.

type BinaryOperator func(a, b int) int

func OpAdd(a, b int) int { return a + b }
func OpSub(a, b int) int { return a - b }

func ApplyBinaryOperator(a, b int, op BinaryOperator) int {
    return op(a, b)
}

答案3

得分: 0

从面向对象编程(OOP)的背景出发,我开始编写以下代码:

package main

import "fmt"

type MyInt int64

func (i *MyInt) Add(n MyInt) *MyInt {
    *i += n
    return i
}

func (i MyInt) String() string {
    v := int64(i)
    return fmt.Sprintf("0x%x (%d)", v, v)
}

func main() {
    x := MyInt(10)
    x.Add(10).Add(20).Add(30)
    fmt.Println("x =", x)
}

请注意,这是一个使用Go语言编写的程序,它定义了一个名为MyInt的自定义类型,并为其定义了两个方法:AddStringAdd方法用于将给定的整数与MyInt类型的变量相加,并返回结果。String方法用于将MyInt类型的变量转换为字符串表示形式。

main函数中,我们创建了一个名为xMyInt类型的变量,并连续调用了Add方法四次,每次传入不同的整数。最后,我们使用fmt.Println函数打印出x的值。

希望这对你有帮助!

英文:

Coming from an oop background I started doing this :

package main

import "fmt"
type MyInt int64

func (i * MyInt) Add(n MyInt) * MyInt {
	*i += n
	return i
}

func (i MyInt) String() string {
	v := int64(i)
	return fmt.Sprintf("0x%x (%d)", v, v)
}

func main() {
	x := MyInt(10)
	x.Add(10).Add(20).Add(30)
	fmt.Println("x = ", x)
}

huangapple
  • 本文由 发表于 2014年3月25日 14:39:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/22627113.html
匿名

发表评论

匿名网友

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

确定