Last active
January 27, 2022 19:09
-
-
Save percybolmer/7f1f73bb9d1b60019a063bfdbf9979ff to your computer and use it in GitHub Desktop.
This code is not working because type constraint
This file contains hidden or 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
// Moveable is a interface that is used to handle many objects that are moveable | |
type Moveable interface { | |
Move(Subtractable) | |
} | |
// Person is a person, implements Moveable | |
type Person struct { | |
Name string | |
} | |
func (p Person) Move(meters Subtractable) { | |
fmt.Printf("%s moved %d meters\n", p.Name, meters) | |
} | |
// Car is a test struct for cars, implements Moveable | |
type Car struct { | |
Name string | |
} | |
func (c Car) Move(meters Subtractable) { | |
fmt.Printf("%s moved %d meters\n", c.Name, meters) | |
} | |
// Move is a generic function that takes in a Moveable and moves it | |
func Move[V Moveable, S Subtractable](v V, distance S, meters S) S { | |
v.Move(meters) | |
return Subtract(distance, meters) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment