What you missing is that generics are parametric polymorphism, see http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29
A function that takes a variable of some type and returns that type, not an interface variable.
Sent from my mobile.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
A function that takes a variable of some type and returns that type, not an interface variable.
func Foo(s someType) someType {
// ...
}On 14 Dec 2014 at 17:11:48, Peter Kleiweg (pkle...@xs4all.nl) wrote
On 14 Dec 2014 at 17:11:48, Peter Kleiweg (pkle...@xs4all.nl) wrote
type TypeVar1;type TypeVar2;
It’s supposed to state that
a and b in
func copy(a TypeVar) b TypeVar{
b:=a
return b
}
must have any, but the same type instead of untyped
func (a interface{}) b interface{}
--Kostarev Ilya
1 problem I am having is that I want to use a struct to read data into its fields - say lastName, firstName, middleInitial and define them as string.I can read the data into them from a SQL DBMS from CHAR / VARCHAR fields as long they are NOT NULL.If there are NULL values - I have to define a field, say middleInitial as sql.NullString.I find this totally counter-intuitive and counter-productive. Because now I cannot use the struct to read data from a CSV file or a JSON impl. such as MongoDB.
I am not understanding why my struct needs to be connected to a SQL implementation.
It is more like 20% of the non-comment lines need to be edited. A simple method set(insert, delete,contain, difference, intersect, super, sub) for a Set struct wrapping a slice is about 100 lines of gofmt formatted code - 22 lines need editing every time I change the type.
A simple method set(insert, delete,contain, difference, intersect, super, sub) for a Set struct wrapping a slice is about 100 lines of gofmt formatted code - 22 lines need editing every time I change the type. I've gave my two cents why generics should be added at a syntactic level, so I won't get into again - other than stating code reuse, and that alone should be a big enough reason to consider it. I find it ironic that the authors put so much effort into keeping the language syntactically light to reduce source sizes but left out a feature such as type parameterization.
You can write code in generic manner "var x $Type” and then generate needed instances for $Type with “go generate” or thirdparty tools. Unfortunately such generic code can’t be typechecked by compiler. Fullblown type checker implementing Hindley–Milner would be quite sophisticated system. Say Haskell type checker is Turing complete, which means it can consume unpredictable time to process, even never stop. But Go compiler must be fast, it’s a design goal. So I can understand tradeoff. Personally I fill comfortable without generic.
--
Kostarev Ilya
I think the Sort function in the sort package is a good example of a generic function. Then why keep people asking for generics? What am I missing?
I think the Sort function in the sort package is a good example of a generic function. Then why keep people asking for generics? What am I missing?
[...]
Personally, I believe Go should have generics (because I hate rewriting the same function over and over and over, as I just had to do) and would like to see a comprehensive discussion about the topic.
Mostly because I think such a conversation has a good chance of leading to Go getting Generics! :-)
They lack of overloading/operators is a major pain for some types of applications such a vector/matrix math. If you consider the equivalent C++ code that you can produce there is little doubt that while lack of overloading/operators may be simpler for the compiler writer it is definitely not simpler for the user of the langauge.Consider:glm::vec3 v1 = ...;glm::vec3 v2 = ...;glm::vec3 v3 = ...;glm::vec3 v = ((v1 - v2) * v2) / 2.0vsv1.Sub(v2).Mul(v2).DivN(2.0)It doesn't take much imagination to see which is simpler to read and understand. And then it gets much worse with no overloading so you end up with Mul, MulV3, MulMat3, MulN and so forth.
No one is forcing you to read the discussions, Andrew.
And so far I've not seen *anything* that contradicts a claim I made in previous thread that Go doesn't have generics because the team doesn't like 'em! So there's zero chance Go will ever have generics. But I'm an optimist. :-)
Oh, nearly forgot - thanks for the condescension.
On Sunday, 14 December 2014 16:11:44 UTC+2, Peter Kleiweg wrote:Op zondag 14 december 2014 13:02:42 UTC+1 schreef branimir....@gmail.com:What you missing is that generics are parametric polymorphism, see http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29It don't understand that, so I think it's a good thing Go doesn't have that. I'm a programmer, not a programming language theorist.Show me something practical that you can do with generics and not with Go.1. Rx -> for reactive programming,2. LINQ -> i.e. concise data manipulation (map/reduce/fold etc.)3. Generic Data Structures -> Sets/Trees/Matrices e.g. https://github.com/egonelbre/spexs2/blob/master/set/set.goCurrently it implements only for a concrete case of ints.4. Generic Algorithmse.g. https://gist.github.com/egonelbre/10578266, you have type conversion scattered around.5. Type-Safe interface implementations -> e.g. https://github.com/egonelbre/event/blob/master/example/guestlist/counter.go#L14 see how the Apply takes a interface instead of a concrete type.They have their benefits, mainly type-safety/speed, but I won't go over those...My opinion regarding those with Generics:1, 2 = I'm not sure whether trying to retrofit a different paradigm into Go is a such good idea. A DSL instead of Generics might be better, although it will be more work.2 = Such data-processing is probably better suited for the DB side, because it has more information how to do the processing efficiently.3 = There aren't that many data-structures that can be generic and fast/useful at the same time. A collection for different concrete use cases might be better than a generic data-structure. Also for the rare cases go generate with map/slices suffices.4 = The same as before it might be more beneficial to have concrete use-case implementation than a generic package.5 = Type conversion isn't that bad. Also, there might be a way to write a tool that does vetting/linting for such particular cases.+ Egon
Did you look at how C# does generics? It doesn't require boxing and unboxing as Java does and also doesn't appear to require the linker to strip out all sorts of unused code like C++ templates.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
I think the Sort function in the sort package is a good example of a generic function. Then why keep people asking for generics? What am I missing?
If one wants optimal speed why not just add macros and be done with it?
I think the Sort function in the sort package is a good example of a generic function. Then why keep people asking for generics? What am I missing?
numbers := []int{1, 2, 3}
newSlice := make([]int, 0, 3)
for _, num := range numbers {
newSlice = append(newSlice, num+2)
}type mapper func(int) int
func collect(s []int, f mapper) []int {
return []int{3, 4, 5}
}And call it like this.
numbers := []int{1, 2, 3}
newSlice := collect(numbers, func(x int) int {
return (x + 2)
})For me, Go's type system is a bit weak. You can't write fully elegant code like in a dynamic language, and you can't be safe/flexible with a truly powerful type system like Haskell's. Heck, even C++'s type system allows for some nice reusable, safe, performant algorithms that you just can't do with Go.Interfaces don't cut it.
In my personal opinion interfaces are not about generics or polymorphism at all. They are more about design by contract.
--
On Sunday, 14 December 2014 06:19:07 UTC-5, Peter Kleiweg wrote:I think the Sort function in the sort package is a good example of a generic function. Then why keep people asking for generics? What am I missing?
Whether or not one thinks that generics would be a good addition to Go (and the apparent consensus around here is that it would not), it is a fact of life that without generics, there are many little routines that one must implement over and over again, such as the max() function over numbers:func max(x, y int) int {if y > x {return y}return x}The definition above is good for int, but not for int64, or float32, or string. I'm sure you've encountered other examples in your own work.Generics is one way to let you express this algorithm once so that it works for all appropriate data types.Macro expansion (such as Matt Sherman's gen tool) is another.Interface (subtype) polymorphism doesn't help in this case, though it does in some others.