--
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.
--
I understand that many requests for syntax additions are rejected on two grounds.
The first is ternaries. What if only simple, non-nested ternaries were supported? For example, color := temperature > 100 ? “red” : “blue”.
This seems so much more clear than the map[bool]:string trick that some have proposed.
Writing this with an if statement takes either 4 or 6 lines.
This comes up often enough in code to make a significant difference. I think it’s hard to argue that this has the potential to make code harder to read if they can’t be nested.
[...]
The first is ternaries. What if only simple, non-nested ternaries were supported? For example, color := temperature > 100 ? “red” : “blue”.
[...]
var color = temperature > 100 ? “red” : “blue”
var color = if temperature > 100 { "red" } else { "blue" }
color := "blue"
--
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.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@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.
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.
The first is ternaries. What if only simple, non-nested ternaries were supported? For example, color := temperature > 100 ? “red” : “blue”. This seems so much more clear than the map[bool]:string trick that some have proposed. Writing this with an if statement takes either 4 or 6 lines. This comes up often enough in code to make a significant difference. I think it’s hard to argue that this has the potential to make code harder to read if they can’t be nested.
color := <conditions> ? <positive action> : <negative action>
color := <condition1> ? (<condition2> ? <first action> : <second value>) : <last value>
To me the biggest reason to want a ternary operator is to make it easier on the reader of the code. If there's anything that go and python have in common, it's that both languages are designed to be easy to read (though with notable differences in emphasis about what makes it easy), and also relatively easy to tell whether code is correct.
With a ternary, it's very clear that after the statement, the assignment has occurred.result = test ? "a" : "b"With an `if' statement, you have to read a bit more:var result stringif test {result = "a"} else {result = "b"}In most cases the analysis is very simple to tell that both code branches assign to `result'. But there's a problem with this. This is only easy to see if someone doesn't then come along and insert extra code into those nice welcoming code blocks:var result stringif test {result = "a"// insert code herefuncCall()} else {// insert other code herefuncCall2()result = "b"}
As a result, as the code is modified, the condition that 'result has a value' is not held and you end up with the zero value instead of something you expected.
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...@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...@googlegroups.com.
I’m new to Go and I imagine the idea of adding a ternary operator to Go has been discussed many times. Rather than repeat that, can someone point me to a discussion about why Go doesn’t add this? I’m struggling to understand why it is desirable to write code like this:
var color
if temperature > 100 {
color = “red”
} else {
color = “blue”
}
Instead of this:
var color = temperature > 100 ? “red” : “blue”
Remember the Vasa! http://www.stroustrup.com/P0977-remember-the-vasa.pdf
func either(c bool, a, b func() string) string {if c {return a()}return b()}func thunk(s string) func() string {return func() string { return s }}fmt.Printf("color is %s", either(temperature > 100, thunk("red"), thunk("blue"))</troll> ;)
var color
if temperature > 100 {
color = “red”
} else {
color = “blue”
}
I find that one-concept-per-line, yielding more compact functions, is easier to focus on, as it reduces vertical eye scanning and scrolling.A single-line if stmnt and my switch example demonstrate one-concept-per-line.
go fmt aspires to produce an odd sort of poetry, which I find inefficient. It's especially bad in GitHub commits.
People have a wide range of perceptive and cognitive abilities. People also have difficulty imagining the very different abilities that others possess.
I find constant vertical scanning and scrolling to be distracting and thus inefficient. So I won't use a tool that adds line breaks to my code, and I'd benefit from a one-line switch.
This is a good compromise, less cryptic than ?:v = if t a; else b // else can be on next linev = if (t) a; else b // parens isolate the test if required
v = b ; if (t) { v = a }
v = b ; if (t) { a }
v = b ; if t { a }
v = b ; if t { v = a }
// Oneline
v := "blue"; if data == nil { v = "red" }
v := data == nil ? "red" : "blue"
v := (data == nil) ? "red" : "blue"
// multiple lines
status := "blue"; if data == nil { status = "red" }
latitude := device.gps("lat"); if data == nil { latitude := "000.00000" }
longitude := device.gps("long"); if data == nil { latitude := "000.00000" }
status := data == nil ? "blue" : "red"
latitude := data == nil ? device.gps("lat") : "000.00000"
longitude := data == nil ? device.gps("long") : "000.00000"
// nested conditions oneline
v := "blue"; if (data == nil && (device == nil || wifi == nil)) { v = "red" }
v := data == (data == nil && (device == nil || wifi == nil)) ? "red" : "blue"
// nested conditions multiple lines
status := "blue"; if (data == nil && (bluetooth == nil || wifi == nil)) { status = "red" }
latitude := device.gps("lat"); if (data == nil || accuracy < 30) { latitude := "000.00000" }
longitude := device.gps("long"); if (data == nil || accuracy < 30) { latitude := "000.00000" }
status := (data == nil && (bluetooth == nil || wifi == nil) ? "blue" : "red"
latitude := (data == nil || device.gps.accuracy() < 30) ? device.gps("lat") : "000.00000"
longitude := (data == nil || device.gps.accuracy() < 30) ? device.gps("long") : "000.00000"
Note that go fmt allows this clumsy constructif err = pkg.ActionItem(); err != nil { // which op is tested?return err}While exploding this clear oneerr = pkg.ActionItem()if err != nil { return err }
err := pkg.ActionItem()
if err != nil {
return err
}
if err = pkg.ActionItem(); err != nil {
return err
}
I've lost count of the times I've had to change:return FooBar{Field: blah,}To:var foo FooBarif someCond {foo.Field = blah} else {foo.Field = bar}return foo
if someCond {return FooBar{Field: blah}}return FooBar{Field: bar}
--
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/MrpkS4epn_E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.