在编程语言中使用任何类型和实现通用列表 [英] Any type and implementing generic list in go programming language

查看:152
本文介绍了在编程语言中使用任何类型和实现通用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一些去编程语言



我对Go的简单性感到兴奋,但在玩完它之后,我遇到了一些麻烦。

1。我知道 Go 不支持泛型和继承。 有没有什么办法可以实现泛型列表?



我在考虑使用:

 类型任何接口{} 

但是如何检查if该值为NULL。

我正在寻找一些等价的实现C

  struct List {
List * tail;
void * head;或者使用代数数据类型:


$ b b

 数据List a = Nil |缺点(列表a)





2。更高级的要求是为为某个特定类型的字段的对象创建一个容器

例如,在 Scala编程语言中,我可以键入:

  val列表[Animal {type SuitableFood = Grass}] 

获得 List 动物,它的成员类型为 SuitableFood ,它是 Grass

解决方案

听起来就像你试图做的是一个链表。



这是一个包含在golang中的例子:

http://golang.org/pkg/container/list/



以下是源代码:

http://golang.org/src/pkg/container/list/list.go


I'm trying a little of go programming language.

I'm a excited about the simplicity of the Go, but after playing with it I met some troubles.

1 . I know that Go doesn't support generics and inheritance. Is there any way to implement generic list?

I thinking about using:

type Any interface { }

but how can I check if the value is NULL.
I'm looking for some equivalent implementation to C

struct List {
  List* tail;
  void* head;
}

Or using algebraic datatype:

data List a = Nil | Cons a (List a)


2 . More advanced requirement would be to make some container for objects with a field of a particular type?
For example in Scala programming language I can type:

val List[Animal { type SuitableFood = Grass} ]

to get a List of Animals, which have a member type SuitableFood which is Grass

解决方案

It sound's like what you're trying to make is a linked list.

Here's an example of one included with golang:
http://golang.org/pkg/container/list/

And here's the source code:
http://golang.org/src/pkg/container/list/list.go

这篇关于在编程语言中使用任何类型和实现通用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆