duanpen9294 2014-08-22 12:28
浏览 46
已采纳

函数参数中的多态性

i found several questions with similar titles, but cannot find the answer to my question in them:

I have the following simple scenario:

types:

    type intMappedSortable interface {
       getIntMapping() int
    }

    type Rectangle struct {
       length, width int
    }

    func (r Rectangle) getIntMapping() int {
        return r.Area();
    }

    func (Rectangle r) getIntMapping() int {
        return r.length * r.width;
    }

main:

func main() {
    r := rand.New(rand.NewSource(time.Now().UnixNano()))

    var values []int
    values = make([]int, 0)

    for i := 0; i < 10; i++ {
        values = append(values, r.Intn(20))
    }

    var rects []Rectangle;
    rects = make([]intMappedSortable, len(values));

    for i,v:= range values {
        r := Rectangle{v,v};
        rects[i] = r;
    }
    for i,v:= range rects {
        fmt.Println(v.Area());
    }


    rectsRet := make(chan intMappedSortable, len(rects));
    sort(rects, rectsRet);
}

doWork:

func sort(values []intMappedSortable, out chan intMappedSortable) {...}

How do i manage to pass the Rectangles to the sorting function and then work with the sorted rectangles in main after it?

I tried:

var rects []*Rectangle;
rects = make([]*Rectangle, len(values));

as a habit from my C days, i don't want to copy the rectangles, just the addresses, so i can sort directly in the original slice, preventing 2 copy procedures for the whole data.

After this failed i tried:

var rects []intMappedSortable;
rects = make([]*Rectangle, len(values));

i learned that Go handles "polymorphism" by holding a pointer to the original data which is not exposed, so i changed *Rectangle to Rectangle, both gave me the compilererror that Rectangle is not []intMappedSortable

What obviously works is:

var rects []intMappedSortable;
rects = make([]intMappedSortable, len(values));

for i,v:= range values {
    r := Rectangle{v,v};
    rects[i] = r;
}

But are are these rectangles now copied or is just the memoryrepresentation of the interface with their reference copied? Additionally there now is no way to access length and width of the rectangles as the slice is not explicitly of type rectangle anymore.

So, how would i implement this scenario? I want to create a slice of ANY structure, that implements the mapToInt(), sort the slice and then keep working with the concrete type after it

EDIT/FOLLOWUP:

I know its not good style, but i'm, experimenting:

can i somehow use type assertion with a dynamic type like:

func maptest(values []intMappedSortable, out interface{}) {
    oType := reflect.TypeOf(out);
    fmt.Println(oType); // --> chan main.intMappedSortable  
    test := values[0].(oType) //i know this is not working AND wrong even in thought because oType holds "chan intMappedSortable", but just for theory's sake
}

how could i do this, or is this not possible. I do not mean wether it is "meant to be done", i know it is not. But is it possible?^^

  • 写回答

1条回答 默认 最新

  • douchui1488 2014-08-22 12:32
    关注

    But are are these rectangles now copied or is just the memory representation of the interface with their reference copied?

    The latter, see "what is the meaning of interface{} in golang?"

    An interface value is constructed of two words of data:

    • one word is used to point to a method table for the value’s underlying type,
    • and the other word is used to point to the actual data being held by that value.

    I want to create a slice of ANY structure, that implements the mapToInt(), sort the slice and then keep working with the concrete type after it

    That isn't possible, as there is no genericity in Go.
    See "What would generics in Go be?"

    That is why you have projects like "gen":

    generates code for your types, at development time, using the command line.
    gen is not an import; the generated source becomes part of your project and takes no external dependencies.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改