On Thu, Aug 13, 2020 at 8:37 PM Beka Westberg <bekawestb...@gmail.com> wrote:
>
> Hello! I just ran into a problem that is solved really nicely with the new 
> generics proposal and I thought someone might find it interesting :D
>
> I was trying to implement a library for doing ascii art programmatically. The 
> main struct is basically an image.Image except it works with bytes instead of 
> colors. As such you /should/ be able to create functions (like a flood fill 
> algorithm) that are abstracted over both of these types, but without 
> "generics" this cannot be done (with compile time type safety).
>
> I sketched a little project in the go2go playground that implements generic 
> functions for doing Porter-Duff compositions on any types that fulfill the 
> following constraint:
>
> ```
> type Rect(type T comparable) interface {
> At(x, y int) T
> Set(x, y int, t T)
> }
> ```
>
> Type defs on lines [8, 13]. Porter-Duff implementation [15, 99] Main [101, 
> 108] ByteImg implementation [110, 185]
>
> I liked this example because it requires passing a generic function (the 
> Porter-Duff func) to another generic function (Merge) :D I hope someone else 
> finds it interesting as well!

Thanks for the example.

Ian

-- 
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/CAOyqgcUWF7c3kvJa-c%3DTTw9Cdccbcz%3D9A%2BKDxpdSBaWgjrtymw%40mail.gmail.com.

Reply via email to