by dilap · 2y ago · 0 comments
It's more an illustration that with Go you can, if you choose, take "any" as your argument type and then later cast to the actual type you need, risking, of course, run-time exceptions if the type is not fulfilled.
Basically, you can opt-in to dynamic typing (the same as e.g. C#).
If the method quack were
quack(ducks ...Duck)
then you'd get a compile-time error.Not cast to just a concrete type, but to an interface. Literally duck typing.
> then you'd get a compile-time error.
Which is emblematic of structural typing, and the only real difference between structural typing and duck typing is that one is dealt with at compile time and the other at runtime. The article tells that Go supports both.
Yes -- supporting structural typing + also runtime casts means you support duck typing.
That's not duck typing, though. It's similar but not the same.
With duck typing there isn't an interface to cast to, but the runtime object has a method matching the one being called. If you're casting, then it's not real duck typing.
People often imagine a "metaclass" or "meta-interface" for each method to help understand how duck typing works, but it's just a mental model to understand the concept. In reality there is no interface tying the methods together, and the whole thing is done at runtime, along with a runtime error if the method doesn't exist.
'If' being the operative word. You're not actually casting. That was merely to not try and confuse the commenter further. In fact, Go doesn't even support casting! It does support type conversion, which is oft confused with casting, but that uses the syntax `type(variable)`, which is not what is going on here either.
> In reality there is no interface tying the methods together, and the whole thing is done at runtime, along with a runtime error if the method doesn't exist.
Right, which is how it works.
public interface ISwim { void Swim(); }
public interface IQuack { void Quack(); }
public interface IDuck extends ISwim, IQuack { }
public class Mallard implements IDuck {
public void Swim() { System.out.println("mallard swimming"); }
public void Quack() { System.out.println("mallard quacking"); }
}
public class Dog implements ISwim {
public void Swim() { System.out.println("dog swimming"); }
public void Bark() { System.out.println("dog barking"); }
}
public static void quack(Object... ducks) {
for (Object duck: ducks) {
((ISwim)duck).Swim();
((IQuack)duck).Quack();
}
}
public static void main() {
quack(new Mallard(), new Dog());
}
in Java? You can do this in Java, you know, casting down to an interface; the same goes for C#. Do those languages now have duck typing, just because in them you can up-cast anything to Object, and then try to down-cast to any other type, be it an interface or a class?Maybe. I don't have a language reference handy. Does runtime type checking take place with that code?
My impression, based on the way you said it (and my vague memories of using Java once upon a time), is no. On that assumption, where would you find the duck type? The significance of both structural typing and duck typing is that type checking occurs. As before, the difference is when the check takes place – be it at compile time or at runtime.
Perhaps you could update that code to demonstrate how you would bypass calling objects which, when type checked, show to not quack like a duck like as is shown in the Go examples?
And no, the structural/duck typing is not about when the type checking happens. It's about not having to write "implements IDuck" right in the class declaration for the down-cast to succeed.
I think this is an important detail, because it’s way safer than in C, where you can just re-interpret memory accidentally and the compiler won’t stop you. Go will panic the thread. Ok, but we already knew that.
The subtle bit is that it’s even a bit safer than most dynamic languages like Python. Because the programmer has to explicitly handle the “unexpected type” case. And because of how verbose Go’s error handling is, every caller up the stack has to consider the error, enforced by the type system. This is different than in Python, where every variable is type any at compiler time, so every line can throw a time error and the callers probably don’t handle it. Even when using “any”, Go’s type system enforces that both the caller and callee consider the “unexpected variable type case”. Overall, it’s still way more of a controlled failure compared to dynamic languages.
It’s great that Go has generics now, to do away with some any hacks. And in general you should avoid any in Go. But when you need it, I think Go actually has a really great reflection API (especially with how it composes with other features like error handling). It’s a bit looked over I think because the difference is subtle at first glance.