--
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.
// ternary returns 12345 if x is positive (x > 0).
// It returns -1 otherwise.
func ternary(x int) int {
return map[bool]int{true:12345,false:-1}[x>0]
}color := map[bool]string{true:"red",false:"green"}[temperature>80]Though to the ops point, not sure why Go doesn’t have the ternary operator - which is pretty ubiquitous.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.
--
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 golan...@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 golan...@googlegroups.com.
--
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.
--
IMHO I've wanted a switch expression, rather than a switch statement for a while.
value := switch test {
case true => "red"
case false => "blue"
}
or
value := switch item.(type) {
case int => item
case string => strconv.Atoi(item)
case time.Time => {
... something more involved ... returning an int
}
}
Sure, not as compact as ternary -- but far more powerful and
useful.
IMHO I've wanted a switch expression, rather than a switch statement for a while.
value := switch test { case true => "red" case false => "blue" }
value := switch item.(type) { case int => item case string => strconv.Atoi(item) case time.Time => { ... something more involved ... returning an int } }
But don't deny others the ability to choose the first alternative
--
> Is this so bad?
Yes, it's horrible as you'll loose any type information you had.
Meaning the next thing you naturally had to do was type cast it, which isn't the nicest syntax to begin with.
By then it's probably more work than just using if / else
On Wed, Apr 24, 2019, at 14:08, Mark Volkmann wrote:
> Are there really developers that find this unreadable?
>
> color := temperature > 80 ? “red” : “green”
Yes.
What is "?"? If I've never seen that before I have no easy way to search
for that, and a random symbol me nothing about what it does. Go
specifically tries to stick to keywords because even if you've never
seen them before it's generally easier to figure out what they do (or to
search for them if you're not sure).
Not to mention that even if you do know what they do, that specific
statement isn't the problem. If you allow people to do that, they'll end
up trying to nest it 5 levels deep. Go tries not to give people the
tools to shoot themselves in the foot for some tiny perceived advantage.
—Sam
> On Apr 24, 2019, at 6:22 AM, Robert Engels <ren...@ix.netcom.com> wrote:
>
> Though to the ops point, not sure why Go doesn’t have the ternary operator - which is pretty ubiquitous.
--
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.
There are many counter-examples. What is the likelihood that someone who is not familiar with the "?" operator will be familiar with the operators for getting (*) and dereferencing (&) a pointer. And what is "<-"? Certainly people not familiar with Go will initially be confused by operators related to channels.
If you allow people to use pointers, will they use pointers to pointers to pointers to pointers?
--
It sure would be nice if Go syntax allowed programmers to replaceif ( test) {...do sonething} else {..do something else}with? (test) {//...do something}{//..do something else}
The ? operator can be anything the Go language team considers appropriate
datalen := removedKeyken // removedKeyken must have been int32 in your example.if value != nil {datalen = len(value)}
Is this so bad?func ternary(cond bool, pos, neg interface{}) interface{} {if cond {return pos} else {return neg}}color := ternary( temp < 80, "blue", "red" )
On Wed, Apr 24, 2019, at 14:08, Mark Volkmann wrote:
> Are there really developers that find this unreadable?
>
> color := temperature > 80 ? “red” : “green”
Yes.
What is "?"? If I've never seen that before I have no easy way to search
for that, and a random symbol me nothing about what it does. Go
specifically tries to stick to keywords because even if you've never
seen them before it's generally easier to figure out what they do (or to
search for them if you're not sure).
color := func() string {
if temperature > 80 {
return "red"
}
return "green"
}()
David Riley <frave...@gmail.com> wrote:
same potential for abuse (no one is gonna stop you from nesting expressions).
Yes, but only assuming it were implemented as an expression.However, if it were instead implemented as an “if-assignment" statement?result := if temperature > 80then "red"else "green"Having it be a statement instead would not address what everyone wants, but it would solve the excessive verbosity of referencing “result” three times and having to explicitly declare its type, or having to assign to it only to have its value immediately overwritten:
var result stringif temperature > 80 {result = "red"} else {result = "green"}And it would do those things without the downsides mentioned thus far in this thread.
On Wed, Apr 24, 2019, at 14:08, Mark Volkmann wrote:
> Are there really developers that find this unreadable?
>
> color := temperature > 80 ? “red” : “green”
Yes.
What is "?"? If I've never seen that before I have no easy way to search
for that, and a random symbol me nothing about what it does. Go
specifically tries to stick to keywords because even if you've never
seen them before it's generally easier to figure out what they do (or to
search for them if you're not sure).
--
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.
On Wed, Apr 24, 2019 at 1:14 PM andrey mirtchovski <mirtc...@gmail.com> wrote:Here's the lore associated with the subject: Ken wanted ternary, Rob
and Robert did not. They overruled Ken (remember, early on all three
had to agree for a feature to go in). The end.
The number of frivolous and egregious abuse of ternary that I've seen
in _modern_ C code is too high.jpg+100 to that sentiment. While the brevity of a ternary expression is useful for trivial cases it tends to be abused. For your amusement/horror here are just a few, of a couple hundred, examples of ternary being abused in the AT&T AST (which includes ksh) source:For those who don't want to follow those links this is the code from the first URL above:lc_unicodeliterals = quote=='u' ? 1 : quote=='U' ? 0 : !!(ast.locale.set & AST_LC_unicodeliterals);
--Kurtis RaderCaretaker of the exceptional canines Junior and Hank
--
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.
As language choice becomes more arbitrary, and transpilers more common, personal preferred syntax / language features may end up being handled like code formatting rules. So In Peter++ I could then use all the syntax I like, transpile that to WASM or LLVM, and someone else working on that code transpiles it back into Gopher++ to make a change.
That appears to be more of a bug than a feature to me, and a nightmare of incompatible source code. Imagine everyone on a team having their source code in their own flavor of a language? And I have yet to see a transpiler maintain 100% compatibility in both directions.Sounds like pure hell to me.IMO transpilers are a hack that come with lots of downsides. Also IMO developers use transpilers because those controlling the evolution of a language have not been responsive to the needs of their developers (I am mainly looking at CSS here, not Go.)I for one pine for a day that will likely never come when transpilers are no longer needed for development (I am not referring to go generate here, but tools to input one language and output another.)JMTCW.
Are there really developers that find this unreadable?
color := temperature > 80 ? “red” : “green”
I know what you are going to say. People will nest them. But even nested usage can be readable when formatted nicely with one condition per line. Another alternative is to allow only unnested ternaries.
R. Mark Volkmann
Object Computing, Inc.
> On Apr 24, 2019, at 8:58 AM, Jan Mercl <0xj...@gmail.com> wrote:
>
>> On Wed, Apr 24, 2019 at 3:48 PM L Godioleskky <lgo...@gmail.com> wrote:
>>
>> The lack of a Go ternary operator is at odds with Go's major theme of clean and easy to read syntax. Those who choose not to use the ternary operator can always resort back to Go's current 'if -else' or 'case' syntax. So Go syntax suffers no negative impact by adding the ternary op to its syntax list. Those opposed to the ternary op should not be allowed to deny it use other Go programmers, that consider it useful.
>
> That's backwards. Those who has to read the code can no more chose not
> to decrypt the unreadable 4-level nested ternary operations instead of
> 5 if statements.
>
> And to follow on your "logic". If you add to Go even just 10% of what
> people consider useful, it will become a new C++, only much worse. And
> again by your very logic. Why we, that haven't chosen to code in C++
> in the first place, would be denied by others to use Go, when those
> others have C++ already at hand?
>
> Let everyone use the language he/she likes. Why ruin it for others
> instead of that by forcing Go to become the same as his/her other
> favorite language?
--
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 don't think there are developers who find it unreadable once they know the logic of that operator. However, if you don't know the rules, then it looks like black magic.In large software developments, consistency beats convenience almost all the time. You can somewhat easily add another developer, if the notation rules are consistent. Whereas clever notational convenience costs for every developer you add to the project. Giving people a choice means you have yet another section in your style guide to understand and maintain.The deeper observation is that in a language with two syntactic classes, statements and expressions, you can opt to have an if-like evaluation in the expression class, or you can omit it. Go does the latter, where C does the former. Of course, you can also define your language with a single syntactic class. In that case, the above example can be writtenlet color = if temperature > 80 then "red" else "green" in ...This style is used in a large selection of languages, which people often call "functional languages". Though the lines of when something fits into the moniker is somewhat blurry.
Given the extent of responses to the original question by the Go community (pro and con), hopefully those responsible for Go syntax will re-visit the issue and decide whether Go statement "if test then {.. } else { .. }" prevents any possible abuse or confusion that would occur if it were replaced with ?test { ..} : { .. }