I think the proposal goes beyond saving few keystrokes.. as demonstrated in 
several of my replies above. The issue is readability and avoiding creating 
new scope with the current shortcut approach if x, err:=fn();  err!=nil.

On Wednesday, August 2, 2023 at 2:58:42 PM UTC-6 Wojciech S. Czarnecki 
wrote:

> Dnia 2023-07-29, o godz. 22:57:15
> DrGo <salah....@gmail.com> napisał(a):
>
> > This involves introducing a new keyword "orelse" that is a syntactic 
> sugar for an "if err!=nil" block.
>
> You can implement it as a snippet under any editor in use today.
>
> If your goal is to personally have it in just one line, you can turn off 
> gofmt and write `; if err !=nil {; return err }`
>
> I'd like to point out that considering 
>
> > It is an error to not return an error from an orelse block.
>
> The `return err` in examples still is a boilerplate, so the overall 
> savings are just four utf-8 bytes, ie. {\n}\n.
>
> I understand sentiment, 'cos there is a huge camp of devs claiming that 
> any error handling proper is one that is done by something/someone up the 
> callstack, even future-me — for this camp idiomatic Go can be annoying.
>
> But note that for the most pleasant coder experience the `orelse return 
> err` is way too much of text to write. 
>
> I would better stay with ^^ digraph instead. Lets name it a 'bat operator'.
>
> func CopyFile(src, dst string) error { 
> r, err := os.Open(src) ^^ 
> defer r.Close()
> w, err := os.Create(dst) ^^
> defer w.Close()
> err = io.Copy(w, r) ^^
> err = w.Close() ^^
> }
>
> > It also works well with named returns. e.g., 
>
> func returnsObjorErro() (obj Obj, err error) {
> obj, err := createObj() ^^ //returns nil and err
> } 
>
> > otherwise ^^ is like "else" so e.g., it can be followed by a block if 
> > additional cleanup or error formatting etc is needed before returning, eg
>
> w, err := os.Create(dst) ^^ {
> ....
> return err 
> }
>
> P.S — bat operator is easily implementable in an IDE with 
> onFileOpen/onFileWrite hooks.
> Its the piping of content to tools (VSCode and GoLand) that bars me from 
> personally using it.
>
> Hope this helps,
>
> -- 
> Wojciech S. Czarnecki
> << ^oo^ >> OHIR-RIPE
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/dfa8041e-7e16-4ef5-a97b-a7282ba6b5c4n%40googlegroups.com.

Reply via email to