[go-nuts] Re: Struct overloading method issue

2017-09-01 Thread BeaT Adrian
I solved it for now using composition and Constructors

func NewA() {
  r := A{}
  r.private = privateForA
} 

func NewB(){
  r := B{}
  r.private = privateForB
}

I saw using constructors in a few builtin packages so I guess is ok.



On Friday, September 1, 2017 at 5:50:52 PM UTC+3, BeaT Adrian wrote:
>
> Hello, I ran into a strange scenario and I wanted to know if there is a 
> better solution for it
>
> type A struct{}
>
> func (a *A) private() {}
> func (a *A) Public() {
>a.private()
> }
>
> type B struct {A}
>
> func (b *B) private() {}
>
> bi := B{}
> b.Public() //calls the A.Private
>
>
>
> I just want to rewrite a small part of the algorithm.
> As a workaround I used composition, but in a weird way
> type A struct {
>  private
> }
>
> func (a *A) Public() {
>if a.private == nil {
>  //set private for A
>}
>a.private
> }
> type B struct {A}
> func (b *B) Public() {
>  if b.private == nil {
>   //set private for B
>  }
> }
>
>  
>
> Other solutions I tried were not effective (how to detect in inner A 
> function that it's a B now, and should use other algorithm).
>
> Thanks!
>
>
>
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Struct overloading method issue

2017-09-01 Thread BeaT Adrian
Hello, I ran into a strange scenario and I wanted to know if there is a 
better solution for it

type A struct{}

func (a *A) private() {}
func (a *A) Public() {
   a.private()
}

type B struct {A}

func (b *B) private() {}

bi := B{}
b.Public() //calls the A.Private



I just want to rewrite a small part of the algorithm.
As a workaround I used composition, but in a weird way
type A struct {
 private
}

func (a *A) Public() {
   if a.private == nil {
 //set private for A
   }
   a.private
}
type B struct {A}
func (b *B) Public() {
 if b.private == nil {
  //set private for B
 }
}

 

Other solutions I tried were not effective (how to detect in inner A 
function that it's a B now, and should use other algorithm).

Thanks!



-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: can list.List.Push* or Get return nil?

2017-08-30 Thread BeaT Adrian
I want to make some data structures that can handle millions of entries. I 
will dome some benchmarks, but in theory slices should be slower (all that 
allocating/reallocating arrays behind the scenes). I don't know the size of 
the structures and I only need the first/last element.

On Tuesday, August 29, 2017 at 8:24:58 PM UTC+3, Egon wrote:
>
> Is there a reason you are using `container/list`, in most cases it's the 
> wrong solution. Slices in most cases are faster and use less resources and 
> easier to work with.
>
> + Egon
>
> On Tuesday, 29 August 2017 01:50:10 UTC+3, BeaT Adrian wrote:
>>
>> Hello, I just started to learn golang and I have a small dillema.
>>
>> My programming is too defensive OR how can I replicate this scenario (for 
>> my test coverage sake)
>>
>> list = list.New()
>> element := list.PushBack(item)
>>  if element == nil {
>>  //don't know how this can happen, just being defensive
>>  return false
>>  }
>>
>>
>> or 
>> element:= list.Back()
>> //can element be nil ? 
>>
>> I browsed the list.List code but still haven't found a solution how to 
>> replicate this case.
>>
>> Thanks!!
>>
>>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: can list.List.Push* or Get return nil?

2017-08-29 Thread BeaT Adrian
Thanks, my bad, for Back() and Front() is obvious, returns nil when there 
are no elements left.

But I don't see how a Push*() can return nil, it always return an Element. 
If I push a nil it will return an Element with value=nil.


On Tuesday, August 29, 2017 at 7:27:31 AM UTC+3, snmed wrote:
>
> Hi 
>
> As far as the docs are right:
>
> For Back: Back returns the last element of list l or nil.
>
> For PushBack:  PushBack inserts a new element e with value v at the back 
> of list l and returns e.
>
> So if you push nil, you get nil otherwise you get always your pushed 
> element.
>
> Cheers
>
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] can list.List.Push* or Get return nil?

2017-08-28 Thread BeaT Adrian
Hello, I just started to learn golang and I have a small dillema.

My programming is too defensive OR how can I replicate this scenario (for 
my test coverage sake)

list = list.New()
element := list.PushBack(item)
 if element == nil {
 //don't know how this can happen, just being defensive
 return false
 }


or 
element:= list.Back()
//can element be nil ? 

I browsed the list.List code but still haven't found a solution how to 
replicate this case.

Thanks!!

-- 
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.
For more options, visit https://groups.google.com/d/optout.