Inverted syntax: Difference between revisions

Content added Content deleted
(Removed {{omit from|Go}} template as 'inverted syntax' can in fact be simulated in the language.)
(Added Go)
Line 365: Line 365:
needUmbrella = true
needUmbrella = true
b is 3
b is 3
</pre>

=={{header|Go}}==
The closest Go can get to inverted syntax for conditionals is to define a new type ('ibool' say) based on the built-in 'bool' type and then define a method ('iif' say) on the new type which takes the place of the traditional 'if'.
<lang go>package main

import "fmt"

type ibool bool

const itrue ibool = true

func (ib ibool) iif(cond bool) bool {
if cond {
return bool(ib)
}
return bool(!ib)
}

func main() {
var needUmbrella bool
raining := true

// normal syntax
if raining {
needUmbrella = true
}
fmt.Printf("Is it raining? %t. Do I need an umbrella? %t\n", raining, needUmbrella)

// inverted syntax
raining = false
needUmbrella = itrue.iif(raining)
fmt.Printf("Is it raining? %t. Do I need an umbrella? %t\n", raining, needUmbrella)
}</lang>

{{out}}
<pre>
Is it raining? true. Do I need an umbrella? true
Is it raining? false. Do I need an umbrella? false
</pre>
</pre>