Hacker News new | ask | show | jobs
by AnimalMuppet 3992 days ago
No. Concepts? It's a way of saying "for your type T to use this template, T must inherit from X, or support these operations, or some other restriction". It lets you have a template that takes a type T, but not just any type T will do. For example, if you have a templated sort function, the restriction might be that T must have a < operator.

Now, in Go, you don't have generics. But you can have a sort function that takes a type (interface) that means "something that has a less-than function", and because of the way Go does OO, anything that has that function works.

This doesn't get Go to the point of having templates that will take any type whatsoever (unless you use empty interfaces).

I feel like I'm still using the wrong word in one or two places. But I hope this is more clear than my previous comment.

1 comments

>No. Concepts? It's a way of saying "for your type T to use this template, T must inherit from X, or support these operations, or some other restriction". It lets you have a template that takes a type T, but not just any type T will do. For example, if you have a templated sort function, the restriction might be that T must have a < operator.

Yeah, that would be Concepts. IIRC, C++ doesn't have them landed yet.

>Now, in Go, you don't have generics. But you can have a sort function that takes a type (interface) that means "something that has a less-than function", and because of the way Go does OO, anything that has that function works.

Yeah, you can have that. But the benefit of generics is that the "things that fit that function" are auto-generated.

> Yeah, that would be Concepts. IIRC, C++ doesn't have them landed yet.

I also believe that C++ doesn't have them yet.

> But the benefit of generics is that the "things that fit that function" are auto-generated.

But because of the way that Go interfaces are in essence duck typed, "things that fit that function" are also auto-generated, other than having to specify the interface that must be satisfied. To me, this seems like no more work than specifying the C++ concept. (Or am I still missing something?)

>But because of the way that Go interfaces are in essence duck typed, "things that fit that function" are also auto-generated, other than having to specify the interface that must be satisfied. To me, this seems like no more work than specifying the C++ concept. (Or am I still missing something?)

No, you have to write their code (concrete implementation for a new type) manually.

The only thing that's automatic is that the new implementation is "registered" as compatible with the interface without you having to explicitly declare it (e.g. not like Java that needs you to write "extends IFoo").

Sorry, I'm not trying to be dense, but:

> No, you have to write their code (concrete implementation for a new type) manually.

Say we're talking about a sort function. Are you saying that I have to write both sort(Foo) and sort(Bar), rather than simply writing sort(SomeInterfaceSharedByFooAndBar)?

I presume you're not just saying that I have to write the code for Foo and Bar; I don't know of anything that will save me from that.