NULL
; otherwise it returns its second operand.
<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// Coalesces can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
?>
I think this feature is needed in Golang.
I disagree, nil is not as common in Go as it is in other lanagues because we use error values to indicate an operating failed; returning nil as a sentinel value is strongly discouraged.
Thanks
Dave
--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Your example uses strings, but in Go strings aren't nullable. So at least as presented, it's not directly applicable to Go: it doesn't make sense to have an expression that evaluates to either a string or nil (unless it's of type interface{}).A variation on the idea would be to recognize "" instead of nil, but then no need to stop there. You could extend || and && to any comparable type, and "x || y" (respectively "x && y") could evaluate to x without evaluating y if it's non-zero (respectively zero), and otherwise evaluate to y. That would generalize nicely from the current definitions of || and &&, and the zero-ness of false.That would let cmd/dist/build.go for example be written like:goroot_final = os.Getenv("GOROOT_FINAL") || gorootgobin = os.Getenv("GOBIN") || goroot + slash + "bin"goos = os.Getenv("GOOS") || gohostosgoarm = os.Getenv("GOARM") || xgetgoarm()
Consider this:var x interface{}y := x || 1What does it mean?
What type should y be?
Should y be 1 if x is nil interface?
Or a typed nil?
Note that what the user really want for "y := x || 1" is probably this:var y intif v, ok := x.(int); ok {y = v} else {y = 1}
So supposing this concrete proposal:- Generalize the binary logical operators to apply to any comparable value instead of only boolean values.- Generalize "p && q" to mean "if p equals its type's zero value then p else q".- Generalize "p || q" to mean "if p equals its type's zero value then q else p".