4 ms·
The first example is not emergent behavior. And it has nothing at all to do with the implicit nature of interfaces. It is in fact a pretty straightforward conse
by eridius 13y ago
The first example is not emergent behavior. And it has nothing at all to do with the implicit nature of interfaces. It is in fact a pretty straightforward consequence of the fact that functions are first-class types. Because they're types, you can newtype them, and you can implement interfaces on the newtype. This should work in any language that has interfaces and first-class functions.
For reference, here's the exact same thing in Rust:
// Define trait Foo, this is the equivalent of an interface
pub trait Foo {
fn foo(&self, x: int) -> int;
}
// Define a newtype called FooFunc
pub struct FooFunc(fn(x:int) -> int);
// Explicitly implement the trait on FooFunc
impl Foo for FooFunc {
fn foo(&self, x: int) -> int {
let &FooFunc(f) = self; // unwrap the newtype
f(x)
}
}
// This is a method that uses a Foo, analogous to http.Handle()
pub fn doFoo<T: Foo>(f: T) {
println!("foo(42) = {}", f.foo(42));
}
// Here's our function that we want to wrap
fn MyFoo(x: int) -> int {
x+1
}
fn main() {
// Call the function, just like http.Handle using http.HandlerFunc
doFoo(FooFunc(MyFoo));
}
And just like Go, this ends up being free at runtime.
- bslatkin 13y agoBut you had to explicitly define the relationship: "impl Foo for FooFunc"
- dignati 13y agoWhich many would prefer because it's implicit documentation. Never knowing what interfaces a go type implements sometimes bugged me. Functions, defined in interfaces often look different in style and paradigm so sometimes they stand out and made me wonder why they would implement such functionallity in this way, not knowing that the definition comes from a hidden interface. It also takes away the ability to quickly look up all die functionality because I would have to know the functions names and what interface they are defined in. Imagine a interface changes and you would have to find all the types you have to change.
- seabee 13y agoThis is exactly the problem you face in generic programming in C++ (and would have been fixed with concepts). You don't get to typecheck against the possible interfaces some object implements, and you have to be very good at parsing compiler diagnostics - assuming your compiler reports good enough diagnostics to let you figure out the problem. I would gladly trade for the verboseness of making these relationships explicit if it let me typecheck and refactor with ease.
- NateDad 13y agoIt is common to say "Foo implements the Fooer interface" in the doc on the function. Also, looking up what interfaces a type implements is 1.) impossible (depends on what other interfaces exist in the universe), 2.) pointless - you shouldn't care what interfaces it implements, you should look for functions that you want to use with your type. You're also missing one of the best parts of implicit interfaces - you can use other people's types in your own functions with interfaces that didn't even exist when they wrote their type. To do that in languages with explicit interfaces, you have to write ugly wrapper classes around the other person's type for zero benefit.
- gliptic 13y agoOnly if the type already defines the methods for that interface. In e.g. Rust you can define whatever interface (trait) you want for external types. No need for wrappers.
- NateDad 13y agoAhh, that's cool, I didn't realize Rust could do that. Though it does still require you to write the thing that tells the compiler their type implements your interface, right? Not a huge deal, but it's still essentially like writing a wrapper (though perhaps not as huge?) Also, it sounds like you can't implement a third party trait on a third party type, so you can't use bill's cool log writer in joe's cool log roller.
- deleted 13y ago
- md224 13y agoYeah, it looks like their solution is just creating a function object whose interface-required method calls the object itself (or at least that's my OOP-centric interpretation). You could do something like that in Javascript (minus the interface, obviously): var f = function(){ console.log('called'); }; f.callMe = function(){ this(); }; I wonder if this is a useful pattern in JS. Can't recall ever seeing it in practice.
- NateDad 13y agoIt's useful in go, because it means you can trivially turn a bare function into a type that fulfills an interface, which makes your function more flexible - it can easily take either a type with a method or just a method.