[wishlist] 'Safe Navigation Operator'.

246 views
Skip to first unread message

Constantin Kulikov

unread,
Dec 10, 2013, 4:41:24 PM12/10/13
to golan...@googlegroups.com
http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28?.%29

and here(7. Monadic null checking): http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated

before:
var num int = -1
if nil != point {
   num = points.Get_number_of_points()
   points.Void_method()
}

after:
num := points?.Get_number_of_points() ?? -1
points?.Void_method()

Jonathan Amsterdam

unread,
Dec 10, 2013, 4:53:25 PM12/10/13
to golan...@googlegroups.com
Go methods on pointers can have null receivers.

type Point struct { x, y int }

func (p *Point) Translate(dx, dy int) {
if p != nil {
p.x += dx
p.y += dy
}
}

func main() {
var p *Point
p.Translate(1, 2) // does not panic
        // ...

Constantin Kulikov

unread,
Dec 10, 2013, 5:21:49 PM12/10/13
to golan...@googlegroups.com
Okay.jpg

Then it may be helpful for null interfaces.

example:

type IPoint interface {
    Translate(dx,dy int)
}

var something IPoint = nil // 'untyped' nil

something?.Translate(1,2) // don't panic

something.Translate(1,2) // panic!

OR:

func megafunc_on_point(point IPoint) {
  point?.Translate(1,1)
  return
}


среда, 11 декабря 2013 г., 1:53:25 UTC+4 пользователь Jonathan Amsterdam написал:

Volker Dobler

unread,
Dec 10, 2013, 5:27:23 PM12/10/13
to golan...@googlegroups.com
Am Dienstag, 10. Dezember 2013 23:21:49 UTC+1 schrieb Constantin Kulikov:
...

something?.Translate(1,2) // don't panic
...


Fine, no panic. But what *does* happen? Just a no-op? Does that help?

V. 

Constantin Kulikov

unread,
Dec 10, 2013, 5:38:43 PM12/10/13
to golan...@googlegroups.com
For this case no panic, no-op.
When method intended to return value:
ret := something?.M_with_return_
value() ?? default_value

Dave Cheney

unread,
Dec 10, 2013, 5:42:32 PM12/10/13
to Volker Dobler, golan...@googlegroups.com
Dear OP,

Please take the time to learn Go throughly before proposing a language change. 

I am confident that you will find that your proposal is unwarranted because the use of nil as a *VALID* return value is dramatically less that in other languages such as Java and C#.

The remaining cases are programming error and should be fixed by the author, not worked around with a new syntax. 

Cheers

Dave
--
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/groups/opt_out.

luz...@gmail.com

unread,
Dec 10, 2013, 5:56:03 PM12/10/13
to golan...@googlegroups.com
I know this "safe navigation operator" from Groovy. It can get a bit annoying: people tend to use it everywhere instead of ".", "just to be on the safe side". It's only really useful if you have long "train wreck" chains like "A.B.C.D.E.F", where each component can be nil. The better design is to avoid "train wrecks" (Law of Demeter) and excessive use of nil pointers. Initialize pointers before usage, use more value types. Also, strings in Go can't be nil and nil slices are valid empty slices.

Go doesn't even support the ternary "?:" operator, because it hides control flow branches inside a single line, and each "?." is a hidden "if" as well.
Reply all
Reply to author
Forward
0 new messages