Hello everyone,

This is my first post to this list so please excuse me if I'm not following 
all of the proper traditions for this community (posting this in the wrong 
place, etc.)

I like Golang a lot and I think that it's a gift to people who love 
computer programming. I have written three softwares in Golang and I'm 
really having a great time with this language.

I was wondering if people would be interested in discussing the inclusion 
of pattern matching syntax in Golang. By "pattern matching" I do not mean 
regular expressions, but rather a feature that is seen in other languages 
such as OCaml 
<https://caml.inria.fr/pub/docs/oreilly-book/html/book-ora016.html>.

Rust also has this feature, which it simply calls "Patterns" 
<https://doc.rust-lang.org/1.5.0/book/patterns.html>. In particular Rust's 
implementation of this feature seems quite excellent actually.

I think adding patterns/pattern matching to Golang would allow it to be 
used much more seriously in situations like building a parser, transpiler, 
compiler etc.. I'm sure there are other uses as well.

Here is an example of what I have in mind:

// Just some code to later demonstrate my proposition
func getDayAndDate() (string, int) {
    date := getDate()
    if todayIsTuesday() == true {
        return "tuesday", date
    }
    return "not tuesday!", date
}

var result string

// What I am proposing starts here (the below is currently not valid Golang 
code)
result = match getDayAndDate() {
    "tuesday", _: "wow looks like it's tuesday",
    _, 5: "i don't know if it's tuesday but it's the fifth! nice!",
    "not tuesday!", _: "not a tuesday my dude",
}

Wouldn't that be cool? I don't think that existing Golang syntax allows us 
to accomplish stuff like this so elegantly. We'd have to do something 
really convoluted. The following doesn't work since switch can't handle 
functions with multiple return values:

switch getDayAndDate() {
    case "tuesday", 5:
        println("cool!")
}

switch also can't handle the _ wildcard as a case value. So really there is 
no serious alternative to achieve pattern matching in Golang right now, as 
far as I can tell.

I tried searching the Internet, including this mailing list, for previous 
discussions of this topic, and the only thing I could find was a post 
dating to eight years ago with little follow-up.

I genuinely believe this is a great feature for Golang to pursue and I 
wonder if we can get the developers of this fantastic programming language 
and toolkit to take this feature more seriously. I'd be happy to help 
implement it myself if I can!

Thank you,

Nadim
Sent from my computer

-- 
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/2699b3f4-f56d-4e04-bfdb-b00fe10390e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to