Re: [go-nuts] Introspecting a type

2017-11-23 Thread Hein Meling
That's nice work-around! Thank you Jakob! 

Best regards,
:) Hein

On Wednesday, November 22, 2017 at 11:27:34 PM UTC-8, Jakob Borg wrote:
>
> A variable of type [][]Operation has that type and only that type. Your 
> Concurrent and Sequential types are convertible to []Operation, and this is 
> what happens in the assignment. 
>
> You can probably use interfaces to accomplish what you want.
>
> type Operator interface {
> Operations() []Operation
> }
>
> Make Concurrent and Sequential distinct types that both implement 
> Operations() []Operation. This can be as simple as
>
> type Concurrent []Operation
>
> func (c Concurrent) Operations() []Operation {
> return c
> }
>
> Make your top level thing an []Operator. Use type switching or further 
> interface methods as appropriate.
>
> //jb
>
> On 23 Nov 2017, at 00:39, Hein Meling > 
> wrote:
>
> I have code like this:
> type TestCase struct {
> Namestring
> Description string
> OrderOp [][]Operation
> }
>
> type Concurrent []Operation
> type Sequential []Operation
>
> With the intent to do things like this:
> OrderOp: [][]Operation{
> Concurrent{
> {ID: "A", Name: "Read", ArgumentType: "ReadRequest", Value: ""},
> {ID: "B", Name: "Write", ArgumentType: "Value", Value: "7"},
> },
> Sequential{
> {ID: "C", Name: "Read", ArgumentType: "ReadRequest", Value: ""},
> {ID: "D", Name: "Write", ArgumentType: "Value", Value: "8"},
> },
> }
>
> However, it seems that the OrderOp array do not preserve the type 
> information, and I cannot use type assertion or reflect.TypeOf() to recover 
> the types Concurrent and Sequential, that I intended to. For a full 
> example, see link below.
>
> My question is: How can I best work around this problem??
>
> (Preferably without adding a separate field in a struct to discriminate 
> between concurrent and sequential.)
>
> https://github.com/selabhvl/cpnmbt/blob/master/rwregister/table_test.go
>
> Thanks,
> :) Hein
>
> -- 
> 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...@googlegroups.com .
> For more options, visit https://groups.google.com/d/optout.
>
>
>

-- 
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.


Re: [go-nuts] Introspecting a type

2017-11-22 Thread Jakob Borg
A variable of type [][]Operation has that type and only that type. Your 
Concurrent and Sequential types are convertible to []Operation, and this is 
what happens in the assignment.

You can probably use interfaces to accomplish what you want.

type Operator interface {
Operations() []Operation
}

Make Concurrent and Sequential distinct types that both implement Operations() 
[]Operation. This can be as simple as

type Concurrent []Operation

func (c Concurrent) Operations() []Operation {
return c
}

Make your top level thing an []Operator. Use type switching or further 
interface methods as appropriate.

//jb

On 23 Nov 2017, at 00:39, Hein Meling 
mailto:hein.mel...@gmail.com>> wrote:

I have code like this:
type TestCase struct {
Namestring
Description string
OrderOp [][]Operation
}

type Concurrent []Operation
type Sequential []Operation

With the intent to do things like this:
OrderOp: [][]Operation{
Concurrent{
{ID: "A", Name: "Read", ArgumentType: "ReadRequest", Value: ""},
{ID: "B", Name: "Write", ArgumentType: "Value", Value: "7"},
},
Sequential{
{ID: "C", Name: "Read", ArgumentType: "ReadRequest", Value: ""},
{ID: "D", Name: "Write", ArgumentType: "Value", Value: "8"},
},
}

However, it seems that the OrderOp array do not preserve the type information, 
and I cannot use type assertion or reflect.TypeOf() to recover the types 
Concurrent and Sequential, that I intended to. For a full example, see link 
below.

My question is: How can I best work around this problem??

(Preferably without adding a separate field in a struct to discriminate between 
concurrent and sequential.)

https://github.com/selabhvl/cpnmbt/blob/master/rwregister/table_test.go

Thanks,
:) Hein

--
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.

-- 
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] Introspecting a type

2017-11-22 Thread Hein Meling
I have code like this:
type TestCase struct {
Namestring
Description string
OrderOp [][]Operation
}

type Concurrent []Operation
type Sequential []Operation

With the intent to do things like this:
OrderOp: [][]Operation{
Concurrent{
{ID: "A", Name: "Read", ArgumentType: "ReadRequest", Value: ""},
{ID: "B", Name: "Write", ArgumentType: "Value", Value: "7"},
},
Sequential{
{ID: "C", Name: "Read", ArgumentType: "ReadRequest", Value: ""},
{ID: "D", Name: "Write", ArgumentType: "Value", Value: "8"},
},
}

However, it seems that the OrderOp array do not preserve the type 
information, and I cannot use type assertion or reflect.TypeOf() to recover 
the types Concurrent and Sequential, that I intended to. For a full 
example, see link below.

My question is: How can I best work around this problem??

(Preferably without adding a separate field in a struct to discriminate 
between concurrent and sequential.)

https://github.com/selabhvl/cpnmbt/blob/master/rwregister/table_test.go

Thanks,
:) Hein

-- 
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.