Last active
August 29, 2015 14:01
-
-
Save bmatsuo/1e30d002faf2bbe26dae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The trouble with specialized map types... I need a better name. | |
package main | |
import "fmt" | |
type I interface { | |
M() | |
} | |
type IFunc func() | |
func (fn IFunc) M() { | |
fn() | |
} | |
func printi(s string, impl I) { | |
fmt.Println(s) | |
impl.M() | |
} | |
func map1(m map[string]I) { | |
for s, impl := range m { | |
printi(s, impl) | |
} | |
} | |
func map2(m map[string]IFunc) { | |
for s, fn := range m { | |
printi(s, fn) | |
} | |
} | |
func map3(m Map) { | |
for s, fn := range m { | |
printi(s, fn) | |
} | |
} | |
type Map map[string]I | |
type MapFunc map[string]IFunc | |
type Map2 map[string]I | |
func main() { | |
map1(Map{"ok": IFunc(func() { fmt.Println("boom") })}) | |
map2(MapFunc{"ok": func() { fmt.Println("boom") }}) | |
map3(map[string]I{"ok": IFunc(func() { fmt.Println("boom") })}) | |
//map3(Map2{"ok": IFunc(func() { fmt.Println("boom") })}) // compilation error | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment