On Tue, Jun 4, 2019 at 10:22 PM Nadim Kobeissi <nadim@nadim.computer> wrote: > > Two more examples to show off some more cool ways match could be useful. > Here, I just demonstrate how we can have logic inside matches thanks to > anonymous functions: > > result = match getDayAndDate() { > "tuesday", _: "wow looks like it's tuesday", > _, 5: (func() string { > if isItWednesday() == true { > return "well, at least it's wednesday!" > } else { > return "not wednesday either!" > } > })(), > } > > Here's another interesting example which showcases destructuring as well as > guards: > > type date struct { > day int > month string > year int > } > > func getDate() date { /* Use your imagination */ } > > var today string > > today = match getDate() { > date{day: d, month: m, year: y} if (m == 2) && (d > 29) : > fmt.Sprintf("Today is impossible.", m, d, y), > date{day: d, month: m, year: y}: fmt.Sprintf("Today is %s %d, %d.", m, d, > y), > }
There are a lot of complex ideas being mixed together here. Let me try to write these examples in the existing language. result = func() string { day, date := getDayAndDate() switch { case day == "tuesday": return "it's Tuesday" case date == 5: if isItWednesday() == true { return "well, at least it's wednesday!" } else { return "not wednesday either!" } } return "" }() today = func() string { d := getDate() switch { case d.month == 2 && d.day == 29: return fmt.Sprint("Today is impossible.", d.month, d.day, d.year) default: return fmt.Sprintf("Today is %s %d, %d.", d.month, d.day, d.year) } }() The code with match is a little shorter, but it doesn't seem to provide any capability that we don't already have. Any new language feature of this complexity should be orthogonal to other features. To my eyes match seems to have a significant overlap with switch. Ian -- 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/CAOyqgcVjdFZOjiV02e-b6Aa8LMhV7dHvoJrx7ZvsF%2BDiNg7pDA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.