Skip to content

proposal: math: use generics. #58148

@gucio321

Description

@gucio321

While going through math code, I see many functions that's usage could be simplified by using generics.
this will simplify code like this:

var a, b int = 5, 10
var max int
max = int(math.Max(float64(a), float64(b)))

to be just

var a, b int = 5, 10
var max int
max = math.Max(a, b)

because Max declaration could become

func Max[T number](x, y T) T {...}

what the number should be needs to be discussed. My proposal is to make it

type number interface {
   ~int | ~uint | ~float32 | ~float64
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions