Then make a dummy interface, with a method with a unique name that you 
never call.

On Thursday, 17 August 2023 at 16:48:28 UTC+1 Mark wrote:

> Hi Brian,
> Sorry for not being clearer. Ideally I don't want an interface: each Shape 
> is essentially a store of different kinds of values so at some point I'm 
> going to iterate over them and within that iterating use a type switch. 
> This is because the shapes _don't_ know how to draw themselves but an 
> external function that reads them will know.
>
> On Thursday, August 17, 2023 at 4:35:10 PM UTC+1 Brian Candler wrote:
>
>> I'm not sure without knowing *how* you want to use []*Shape{}.  Perhaps 
>> you want Shape to be an interface?
>>
>> type Shape interface {
>>     DrawMe()
>> }
>>
>> type BoxShape struct ...
>> func (*BoxShape) DrawMe() { }
>>
>> type LineShape struct ...
>> func (*LineShape) DrawMe() { }
>>
>> shapes := []Shape{}
>>
>> (Note that interfaces internally carry pointers, so you don't generally 
>> want to have pointers to interface types)
>>
>> On Thursday, 17 August 2023 at 13:44:10 UTC+1 Mark wrote:
>>
>>> Here's something that works but uses `any` which is too broad in scope:
>>> ```go
>>> type Shape any // Want to constrain to BoxShape and LineShape only
>>>
>>> type BaseShape struct {
>>> X         int
>>> Y         int
>>> PenColor  color
>>> PenWidth  int
>>> FillColor color
>>> }
>>>
>>> type BoxShape struct {
>>> BaseShape
>>> Width  int
>>> Height int
>>> }
>>>
>>> type LineShape struct {
>>> BaseShape
>>> X2 int
>>> Y2 int
>>> }
>>>
>>> shapes := []*Shape{}
>>> ```
>>> Is there a nice way to create a `Shape` type which will allow me to do 
>>> `shapes := []*Shape{}` and that _also_ constrains the shapes to `BoxShape` 
>>> and `LineShape`?
>>>
>>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/09798318-1a13-4570-a8a2-af6ccb3f0155n%40googlegroups.com.

Reply via email to