Last active
August 14, 2021 06:35
-
-
Save nobishino/ba74f22cb0954bc9471a98bad2f6011a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
func main() { | |
v3 := Vector3D[int]{1,2,3} | |
println(DProd[Vector3D[int], int](v3,v3)) | |
} | |
type Numeric interface { | |
~int | ~int8 | ~int16 | ~int32 | ~int64 | | |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | | |
~float32 | ~float64 | | |
~complex64 | ~complex128 | |
} | |
type Vector2D[T Numeric] [2]T | |
type Vector3D[T Numeric] [3]T | |
func (v Vector2D[T]) DProd(w Vector2D[T]) T { | |
var result T | |
for i:=range v { result += v[i]*w[i] } | |
return result | |
} | |
func (v Vector3D[T]) DProd(w Vector3D[T]) T { | |
var result T | |
for i:=range v { result += v[i]*w[i] } | |
return result | |
} | |
// 言語仕様に次のようにあるので、 TのスコープはDProdの直後から始まり、 V interface{DProd(V) T}のようにTを参照できる | |
// | |
// The scope of an identifier denoting a type parameter of a parameterized function | |
// or declared by a method Receiver begins after the function name | |
// and ends at the end of the function body. | |
func DProd[V interface{DProd(V) T}, T Numeric](v, w V) T { | |
return v.DProd(w) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment