// GetByID read the Tomate of given ID func (t Controller) GetByID(getID string) (jsonResBody *Tomate, err error) { t.backend.Transact(func(backend *Tomates) { jsonResBody = backend. Filter(FilterTomates.ByID(getID)). First() }) if jsonResBody == nil { err = &NotFoundError{errors.New("Tomate not found")} } return jsonResBody, err }
// Create a new Tomate
func (t Controller) Create(postColor *string) (jsonResBody *Tomate, err error) {
mustNot(postColor, nil) or
return ... &UserInputError{errors.New("Missing color parameter")}
color, _ := mustNot(strings.TrimSpace(*postColor), "") or
return ... &UserInputError{errors.New("color must not be empty")}
t.backend.Transact(func(backend *Tomates) {
must(backend.Filter(FilterTomates.ByColor(color)).Empty(), true) or {
err = &UserInputError{errors.New("color must be unique")}
return
}
jsonResBody = &Tomate{ID: fmt.Sprintf("%v", backend.Len()), Color: color}
backend.Push(jsonResBody)
})
return jsonResBody, err
}
From that go compatible code,
// Create a new Tomate
func (t Controller) Create(postColor *string) (jsonResBody *Tomate, err error) {
if postColor == nil {
return nil, &UserInputError{errors.New("Missing color parameter")}
}
color := strings.TrimSpace(*postColor)
if color == "" {
return nil, &UserInputError{errors.New("color must not be empty")}
}
t.backend.Transact(func(backend *Tomates) {
exist := backend.Filter(FilterTomates.ByColor(color)).Len()
if exist > 0 {
err = &UserInputError{errors.New("color must be unique")}
return
}
jsonResBody = &Tomate{ID: fmt.Sprintf("%v", backend.Len()), Color: color}
backend.Push(jsonResBody)
})
return jsonResBody, err
}Sorry for this crime of thought, i was thinking it was a chance to talk about it and explore different paths.
That, this, is the only answer, well, life is hard.
Generics enable more than just replacing loops. For example, they can enable libraries of generic algorithms to be used with any type of array. Here's an example:foo := GetArray()result := foo.Take(10).map(...).Sort(...).Reduce(...)
--
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.
Right, now show me the contents of the average wage by country function! It's not going to be one line or as readable is it?!?!
Hi,
yes generics / not generics.
That is, in what i proposed, only the consumer is enhanced, thus generics are not required anymore on the declarer, except for this conv method, indeed.
also as the declarer can rely on more powerful tool on the consumer,
it needs to declare less to able more.
That being said,
Can you take a look to that, genericity less, it works
https://gist.github.com/mh-cbon/2062efef48e246592bdb0665f0ab8547
IMHO, its simple, it does what i need, it helps me to avoid obvious gotchas, after many attempt i feel satisfy,
please critic it.
I think you are missing the point of my comment.I'm all for generics, but I also can survive without them without much of a problem. (I'm even maintaining https://docs.google.com/document/d/1vrAy9gMpMoS3uaVphB32uVXX4pi-HnNjkMEgyAHX4N4 to understand the problem better)
On Sunday, June 4, 2017 at 12:25:17 PM UTC-4, Egon wrote:I think you are missing the point of my comment.I'm all for generics, but I also can survive without them without much of a problem. (I'm even maintaining https://docs.google.com/document/d/1vrAy9gMpMoS3uaVphB32uVXX4pi-HnNjkMEgyAHX4N4 to understand the problem better)
create a custom container type that I can use with any type out-of-the-box.
for example: iset := make(Set[int],9); sset := make(Set[string],9); pset := make(Set[Person]);
right now, my solution is to rewrite the source with go generate calling a tool i wrote which grew out from a great talk from robert griesemer. The video: https://www.youtube.com/watch?v=vLxX3yZmw5Q
This solution has nearly twice the performance increase over maps with almost every type used so far. Now I could have just written the 500 lines of code over and over again for every type needed until the end of time(or copy and paste and edit until the end of time), or I could use empty interfaces which throws type safety out the window, but I chose to write a tool, and when go generate came to be I chose to use that tool like a simple script executed within a '//go:generate go run..' tag.
Over time golang(I do this now to annoy the zealots) provided the tools(reflection, /go packages, go generate) that made creating custom generic type safe containers easier. These things weren't always this easy, and these things could be easier.
And I am serious about create a custom container type that I can use with any type out-of-the-box. Please indulge me, how would you solve this problem. Provide a solution for a simple type-safe container type Set that takes any type, with a simple api of insert,remove,contain,union,difference,intersection, complement,superset,subset.
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/Oo5s6ryBCYI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
I think you are missing the point of my comment.I'm all for generics, but I also can survive without them without much of a problem. (I'm even maintaining https://docs.google.com/document/d/1vrAy9gMpMoS3uaVphB32uVXX4pi-HnNjkMEgyAHX4N4 to understand the problem better)
The issue I was trying to highlight was that you are proposing solutions to a problem, but other people don't know what your exact problem is. Hence my 3 lines version are equivalent in their result and value, because the problem is missing.
Similarly, when we don't have a concrete problem we could over-engineer the generics or alternatively solve the wrong problems.For example, when the solution you propose only solves 5% of the problems that generics needs to solve... or vice-versa... a generics solution 100x simpler would solve 99% of the cases... then what is the value of the proposed solution?So, which of the 147 different generics approaches/implementations works better for Go and why? (I'm also counting the ones that aren't known yet :D)When you would use a real-world example and problem, it would be possible to argue that one approach is better than the other... without one, this discussion doesn't lead to anywhere.+ Egon
just to add,
https://www.youtube.com/watch?v=gHCtEjzZ-rY
I could not agree more on the end,
i could argue about some arguments ;)
btw, is there a channel that gathers all talks about go ?
I gave you a simple problem, don't over think it. A Set data structure with a simple api that can work out-of-the-box - just as slices and maps with the builtins. I kept the requirements minimal. I'd like to see your solution.