Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuple type #67972

Closed
sabouaram opened this issue Jun 13, 2024 · 2 comments
Closed

Tuple type #67972

sabouaram opened this issue Jun 13, 2024 · 2 comments
Labels

Comments

@sabouaram
Copy link

sabouaram commented Jun 13, 2024

Proposal Details

Hello Gophers,
We propose the implementation of a tuple type in Golang, which allows for the creation of a collection of elements of different types. The tuple type provides several advantages, including the ability to store and retrieve heterogeneous data, iterate over its elements, and obtain the type information of each element.

An example of the tuple type:

package main

import (
	"fmt"
	"reflect"
)

type tuple struct {
	elements []any
}

type tupleElement struct {
	elemindex int
	elemvalue any
	elemtype  reflect.Type
}

func Tuple(elements ...any) *tuple {
	return &tuple{elements: elements}
}

func (t *tuple) len() int {
	return len(t.elements)
}

func (t *tuple) get(index int) (any, reflect.Type) {
	if index < 0 || index >= len(t.elements) {
		panic("index out of range")
	}
	return t.elements[index], reflect.TypeOf(t.elements[index])
}

func (t *tuple) valueAt(index int) any {
	value, _ := t.get(index)
	return value
}

func (t *tuple) typeAt(index int) reflect.Type {
	_, typ := t.get(index)
	return typ
}

func (t *tuple) Iterate() <-chan tupleElement {
	ch := make(chan tupleElement)
	go func() {
		for i, elem := range t.elements {
			ch <- tupleElement{i, elem, reflect.TypeOf(elem)}
		}
		close(ch)
	}()
	return ch
}

func (t *tuple) String() string {
	result := "tuple{"
	for i, elem := range t.elements {
		result += fmt.Sprintf("%v (type: %s)", elem, reflect.TypeOf(elem))
		if i < len(t.elements)-1 {
			result += ", "
		}
	}
	result += "}"
	return result
}

// //////////////////////////////////////////////Example////////////////////////////////////////////////////////////////
func main() {

	t := Tuple("Alice", 30, "Engineer", true, 42.0, struct { a int ; b string }{ 4 , "hello" } )

	fmt.Printf("t[0] = %v (type: %s)\n", t.valueAt(0), t.typeAt(0))
	fmt.Printf("t[1] = %v (type: %s)\n", t.valueAt(1), t.typeAt(1))

	for elem := range t.Iterate() {
		fmt.Printf("t[%d] = %v (type: %s)\n", elem.elemindex, elem.elemvalue, elem.elemtype)
	}

	fmt.Println(t)

}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@gabyhelp
Copy link

Similar Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@seankhliao
Copy link
Member

I'm going to close this as a dupe of the above.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants