if a > b {
c = a
else {
c = b
}
can easily be written as
c = a > b ? a : b
I think there should be consideration for this in future releases.
Or what do you guys think?
func iffS(c bool, a string, b string) string {
if c { return a } else { return b }
That being said I have the feeling it's unlikely to ever see this in
go. It certainly wasn't neglected due to forgetfulness.
For my personal style, I like not having a ternary operator. I feel
that making code too compact can make it unreadable (perl).
- John
The opposite is also true - making it too long makes it unreadable.
Java, for example, makes your code longer - each line is very simple,
but there are a lot of them. I mean, getters and setters etc... it's
hard to find something from Java class, sometimes.
If you want to get fancy you can always do something like this:
c := func(x, y int) int {if x>y {return x};return y}(a, b);
If Issue 65 (http://code.google.com/p/go/issues/detail?id=65) gets
resolved you could do
c := func(x, y int) int {if x>y {return x} else {return y}}(a, b);
Which looks a bit nicer.
I would say that if and when we get generics you could define
templated min/max functions, but then this would be hard to implement
without operator overloading. If you keep going down this route you
end up with C++ or D.
I think the Go way is to keep things simple - just use the code you
posted.
map[bool]int{true: a, false: b}[a > b]
--
Peter H. Froehlich <http://www.cs.jhu.edu/~phf/>
Senior Lecturer | Director, Johns Hopkins Gaming Lab
This is beautiful abuse. Simultaneously I feel respect and disgust.
Humans can figure the intent here. Can an optimizing compiler?
Probably not this one yet.
On Wed, Dec 23, 2009 at 1:25 PM, Qtvali <qtv...@gmail.com> wrote:map[bool]int{true: a, false: b}[a > b]
> func iffI(c bool, a int, b int) int {
> if c { return a } else { return b }
> }
Once again: http://www.cs.utexas.edu/users/EWD/transcriptions/EWD06xx/EWD667.html
Intent might be to call both and return one. That's why "||" now
leaves second one uncalculated if first one is false, by default -
without excplicitly stating this, code would be broken. Too much magic
breaks things.
map[bool]int{true: func()(int){return a}, false: func()(int){return b}}
[a > b]()
Horrendous :)
you might as well just do
x := func()(int){ if a > b { return a }; return b }()
since funcs are full closures.
Go is not ready for such a Max yet. Returning just Comparable when
both arguments are ints loses static type information and a cast
(implying an unnecessary runtime check) would be needed to recover it;
it should better have the same result type as the type of arguments.
Also, plain ints won't support Comparable.
And this doesn't cover other uses of if-expression.
--
Marcin Kowalczyk
Prior to this recent post, this thread had been dormant for eight years. I think the results speak for themselves and this topic does not need to be revisited again.
--
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.