6 ms·
> 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 c
by randomdata 2y ago
> 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.
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.
- Joker_vD 2y agoHow is that any different from doing 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?
- randomdata 2y ago> Do those languages now have duck typing 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?
- dilap 2y agoI think the closest equivalent in Java or C# would be to use reflection to pull out the Swim and Quack methods.
- mseepgood 2y agoIn C# you would simply use the 'dynamic' keyword.
- neonsunset 2y agoAnd get yelled at by your colleagues ;D In practice, the use of dynamic is highly discouraged as well as anything that could lead to type confusion i.e. currently discussed Go's duck typing it calls structural (runtime vs static is impl. detail). Instead, you are expected to simply do 'lessSpecific is IMoreSpecific ms`, works particularly well with pattern matching on switches for functional style.
- dilap 2y agoha totally forgot about that! what a crazy feature. i wonder if anyone uses it in the wild?
- Joker_vD 2y agoYes, the runtime type check does happen, see [0]. The output is pretty much the same as with the Go example, only the exact wording of the exception/panic is different. 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. [0] https://godbolt.org/z/vndns4619 https://godbolt.org/z/vndns4619
- randomdata 2y ago> It's about not having to write "implements IDuck" right in the class declaration for the down-cast to succeed. That doesn't make sense. Not having to write "implements IDuck" is true of both structural typing and duck typing. That logically cannot be what makes them different.
- dilap 2y agoAh, ok, thanks, now I see the point the article was trying to make. Yes -- supporting structural typing + also runtime casts means you support duck typing.
- jlarocco 2y ago> Not cast to just a concrete type, but to an interface. Literally 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.
- deleted 2y ago[deleted]
- randomdata 2y ago> If you're casting, then it's not real duck typing. '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.