-
-
Save arnaudbreton/85f93d187d2b183e40bc to your computer and use it in GitHub Desktop.
dotGo 2014
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
*.class |
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
class ExplicitStringer implements Stringer { | |
public String String() { | |
return "I am a real Stringer!"; | |
} | |
} |
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
Main.java:11: error: method print in class Main cannot be applied to given types; | |
Main.print(notStringer); | |
^ | |
required: Stringer | |
found: NotExplicitStringer | |
reason: actual argument NotExplicitStringer cannot be converted to Stringer by method invocation conversion | |
1 error |
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
package main | |
import ( | |
"fmt" | |
) | |
type ImplicitStringer struct { | |
value string | |
} | |
func (s ImplicitStringer) String() string { | |
return s.value | |
} | |
func Print(stringer fmt.Stringer) { | |
fmt.Println(stringer) | |
} | |
func main() { | |
s := ImplicitStringer{ | |
value: "42", | |
} | |
Print(s) | |
} |
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
public class Main { | |
public static void main(String [] args) { | |
// Works because ExplicitStringer | |
// explictly implements Stringer interface | |
ExplicitStringer stringer = new ExplicitStringer(); | |
Main.print(stringer); | |
// Does not compile because NotExplicitStringer does not | |
// explictly implements Stringer interface | |
NotExplicitStringer notStringer = new NotExplicitStringer(); | |
Main.print(notStringer); | |
} | |
public static void print(Stringer stringer) { | |
System.out.println(stringer.String()); | |
} | |
} |
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
class NotExplicitStringer { | |
public String String() { | |
return "I am concrete!"; | |
} | |
} |
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
type Stringer interface { | |
String() string | |
} |
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
public interface Stringer{ | |
string String() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment