Overriding built-in functions

570 views
Skip to first unread message

koryl...@gmail.com

unread,
Jan 11, 2015, 11:55:20 AM1/11/15
to golan...@googlegroups.com
While watching Andrew and Brad's live coding session I noticed they used "make" as a variable name. Here's a basic example

package main

import (
   
"fmt"
)

func
Make() string {
   
return "Oops"
}

func main
() {
    make
:= Make
    fmt
.Println(make())
}

Playground (https://play.golang.org/p/p5wS1_INye)

This seems like a bad idea (I don't think they realized they did it), and I actually thought the compiler would not allow it.

I was really surprised when everything worked. Is there anytime when overriding make or its friends would be useful?

Kory

David Symonds

unread,
Jan 11, 2015, 6:50:01 PM1/11/15
to koryl...@gmail.com, golang-nuts
Builtins are not keywords, so the compiler permits it.

It's very useful in the sense of forward compatibility. Imagine that a
future version of Go introduces some new builtin function, "zero".
Everyone's code that currently does something like
zero := 0
will keep working in that future version just fine because of this.

Jesse McNelis

unread,
Jan 11, 2015, 7:02:17 PM1/11/15
to koryl...@gmail.com, golang-nuts
On Mon, Jan 12, 2015 at 3:55 AM, <koryl...@gmail.com> wrote:
> I was really surprised when everything worked. Is there anytime when
> overriding make or its friends would be useful?

To be clear, you're not 'overriding' the built-in make(), you're
creating a new variable called 'make' within the scope of main().
This makes the built-in make() inaccessible within main() but doesn't
effect it outside of main().

The variable called 'make' inside main() is said to 'shadow' the
built-in make().
This is one of the useful properties of block scoping.

koryl...@gmail.com

unread,
Jan 11, 2015, 11:07:43 PM1/11/15
to golan...@googlegroups.com
Makes sense. Thanks!

Gao

unread,
Jan 13, 2015, 10:30:19 AM1/13/15
to golan...@googlegroups.com, koryl...@gmail.com, jes...@jessta.id.au
I can not agree
```
package main

var make = 1 

func main() { 

}
```

what bout this code? It's just the reason that make is not keyword.
 

chris dollin

unread,
Jan 13, 2015, 10:40:50 AM1/13/15
to Gao, golang-nuts, koryl...@gmail.com, Jessta
Well, what about it?

> It's just the reason that make is not keyword.

make doesn't need to be a keyword. Which is good,
because it reduces the opportunity to trip over its spelling.

Chris

--
Chris "allusive" Dollin

Gao

unread,
Jan 13, 2015, 10:47:43 AM1/13/15
to golan...@googlegroups.com, gaop...@gmail.com, koryl...@gmail.com, jes...@jessta.id.au


On Tuesday, January 13, 2015 at 11:40:50 PM UTC+8, ehedgehog wrote:
On 13 January 2015 at 15:30, Gao <gaop...@gmail.com> wrote:

> On Monday, January 12, 2015 at 8:02:17 AM UTC+8, Jesse McNelis wrote:

>> The variable called 'make' inside main() is said to 'shadow' the
>> built-in make().
>> This is one of the useful properties of block scoping.
>
> I can not agree
> ```
> package main
>
> var make = 1
>
> func main() {
>
> }
> ```
>
> what bout this code?

Well, what about it?
I want illustrate that if make is predefined in the out most scope,it will arose a redefine error,right?

chris dollin

unread,
Jan 13, 2015, 10:53:37 AM1/13/15
to Gao, golang-nuts, koryl...@gmail.com, Jessta
On 13 January 2015 at 15:47, Gao <gaop...@gmail.com> wrote:
>
>
> On Tuesday, January 13, 2015 at 11:40:50 PM UTC+8, ehedgehog wrote:

>> > ```
>> > package main
>> >
>> > var make = 1
>> >
>> > func main() {
>> >
>> > }
>> > ```
>> >
>> > what bout this code?
>>
>> Well, what about it?
>
> I want illustrate that if make is predefined in the out most scope,it will
> arose a redefine error,right?

No.

As per

http://golang.org/ref/spec#Declarations_and_scope

the predefined identifiers are in the universe block, which
encloses the package block. Hence package-level declaration
may shadow the builtins.
Reply all
Reply to author
Forward
0 new messages