FAQ
Yes, this is another usecase :)
On Thursday, October 9, 2014 2:22:08 PM UTC-7, milo.chr...@gmail.com wrote:

I dislike this little limitation, it would not be that hard to allow
methods on such a type would it?

For one thing it would remove the need for extra parentheses when doing
conversions.
eg: (*SomeType)(value)
On Thursday, October 9, 2014 5:06:42 PM UTC-4, xingtao zhao wrote:

I am trying to do the following but it des not compile (
http://play.golang.org/p/HTWEQ98MPZ):

package main

import "fmt"

type Stack *[]int
func (s Stack) Push(e int) { *s = append(*s, e) }
func (s Stack) Top() int { return (*s)[len(*s)-1] }
func (s Stack) Pop() { *s = (*s)[:len(*s)-1] }

func main() {
var s Stack = &[]int{}
s.Push(1)
s.Push(2)
s.Push(3)
fmt.Println(s.Pop())
fmt.Println(s.Pop())
fmt.Println(s.Pop())
}

Nor does this work (http://play.golang.org/p/11hX3oRnIU):

package main

import "fmt"

type ConcretStack []int
type Stack *ConcretStack
func (s *ConcretStack) Push(e int) { *s = append(*s, e) }
func (s *ConcretStack) Top() int { return (*s)[len(*s)-1] }
func (s *ConcretStack) Pop() { *s = (*s)[:len(*s)-1] }

func main() {
var s Stack = &ConcretStack{}
s.Push(1)
s.Push(2)
s.Push(3)
fmt.Println(s.Pop())
fmt.Println(s.Pop())
fmt.Println(s.Pop())
}

Does any one have some ideas on this?
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 5 of 17 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 9, '14 at 9:06p
activeOct 10, '14 at 1:09a
posts17
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase