Yep, in fact I have already added min and max functions that return the
lesser/greater of their two arguments. Adding builtin functions is much
easier than builtin methods, since other builtin functions are already
supported by the compiler. You just add a new op type to gc/syntax.go, then
add a switch case to typecheck1, and finally rewrite the ops to m = l; if r
cmp l { m = r } in gc/walk.go. (In this case, the operation is simple
enough that adding a runtime function would be overkill.) I was even able
to make the expressions evaluate to a constant if their arguments are
constant, which is cool.
So yes, I could add a reverse builtin function in the same way, and it
would definitely be simpler than adding a method. But I wouldn't learn
anything new about the compiler that way. Currently there are no builtin
methods whatsoever, which means that adding one is much more involved than
adding a new builtin function. I'm willing to put in the necessary effort,
but I could use a few pointers so that I avoid coding myself into a corner.
On Sunday, February 7, 2021 at 10:12:00 AM UTC-5 Brian Candler wrote:
> > If you were tasked with adding a builtin method to all slices, how would
> you do it?
>
> Did you consider making a global function, like copy() and append() ? Why
> does it have to be a method?
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/8d8bfed3-9e8b-469f-a542-b415d9e3ef23n%40googlegroups.com.