Programming custom type conversion

3,601 views
Skip to first unread message

ThePiachu

unread,
Sep 19, 2011, 6:00:56 PM9/19/11
to golang-nuts
I`d like to implement a custom class
type A struct{
val []byte
}
And I`d like to implement a custom type conversion that would convert
that class into for example a string. How should I go about that,
since I can't see it anywhere on the website?

Steven Blenkinsop

unread,
Sep 19, 2011, 8:09:43 PM9/19/11
to ThePiachu, golang-nuts
Don't think of them as classes. They aren't. They're types with methods.

type A struct {
    val []byte
}

func (a *A) String() string { return string(a.val) }

Note that, unless you have a particular reason for using a struct, you could just do:

type A []byte
func (a A) String() string { return string(a) }

Have you read:

And:

?

ThePiachu

unread,
Sep 21, 2011, 6:29:36 PM9/21/11
to golang-nuts
Okay, what you presented works when I`m calling
fmt.Printf("%s", a.String())

I was thinking of something that would look like:
fmt.Printf("%s", string(a))

That doesn`t work with
func (a *A) String() string { return string(a.val) }

I supposed it would look more like
func string(a A) string{return string(a.Val)}

but I can't use string as a function name. I was wondering whether it
is possible at all, or are there just some fixed conversion functions
like string(a) that can't be expanded (like it is possible in C++).

Steven Blenkinsop

unread,
Sep 21, 2011, 6:48:12 PM9/21/11
to ThePiachu, golang-nuts
You can't override built-in behaviour in Go. This property is considered desirable because it allows you to have an idea of the cost of a given operation. Here, why would you want to? Just call the method, don't try to hide it behind a language construct. There isn't any benefit in doing so.

Kyle Lemons

unread,
Sep 21, 2011, 6:51:11 PM9/21/11
to ThePiachu, golang-nuts
On Wed, Sep 21, 2011 at 3:29 PM, ThePiachu <thep...@gmail.com> wrote:
Okay, what you presented works when I`m calling
fmt.Printf("%s", a.String())

This is better written:
fmt.Printf("%s", a)

which will automatically call a.String() for you because fmt looks for objects that implement Stringer.

I was thinking of something that would look like:
fmt.Printf("%s", string(a))

That doesn`t work with
func (a *A) String() string { return string(a.val) }

I supposed it would look more like
func string(a A) string{return string(a.Val)}

This sounds suspiciously like operator overloading.  To me, when I am debugging a function, if I see "a + b" I am relieved to know that a and b must both be identical numeric types.  This saves a lot of cognitive load.  The same is true of string(x).  I know that x is a string, a type whose underlying type is string, or a []byte.  Again, it simplifies my debugging, because I don't have to worry about side effects.  If, however, I see a.String() I need to go see what that function does, or risk missing the source of whatever I'm debugging.
 
but I can't use string as a function name. I was wondering whether it
Sure you can.  Almost nothing is sacred in Go.

package main

import "fmt"

func string(i int) {
fmt.Printf("%d", i)
}

func main() {
string(1)
}

I just wouldn't recommend it.
Reply all
Reply to author
Forward
0 new messages