duck-typing

Why interfaces must be declared in Java?

亡梦爱人 提交于 2019-11-30 13:49:16
Sometimes we have several classes that have some methods with the same signature, but that don't correspond to a declared Java interface. For example, both JTextField and JButton (among several others in javax.swing.* ) have a method public void addActionListener(ActionListener l) Now, suppose I wish to do something with objects that have that method; then, I'd like to have an interface (or perhaps to define it myself), e.g. public interface CanAddActionListener { public void addActionListener(ActionListener l); } so that I could write: public void myMethod(CanAddActionListener aaa,

F# and duck-typing

妖精的绣舞 提交于 2019-11-30 01:46:09
问题 Let's say I defined in F# the following two types: type Dog = { DogName:string; Age:int } type Cat = { CatName:string; Age:int } I was expecting the following method to work for both cats and dogs: let isOld x = x.Age >= 65 Actually, what seems to happen is that isOld will only accept cats: let dog = { DogName = "Jackie"; Age = 4 } let cat = { CatName = "Micky"; Age = 80 } let isDogOld = isOld dog //error My hopes were that F# would be smart enough to define some kind of "virtual" interface X

Is there any point for interfaces in dynamic languages?

五迷三道 提交于 2019-11-30 01:37:16
In static languages like Java you need interfaces because otherwise the type system just won't let you do certain things. But in dynamic languages like PHP and Python you just take advantage of duck-typing . PHP supports interfaces. Ruby and Python don't have them. So you can clearly live happily without them. I've been mostly doing my work in PHP and have never really made use of the ability to define interfaces. When I need a set of classes to implement certain common interface, then I just describe it in documentation. So, what do you think? Aren't you better off without using interfaces in

How to identify numpy types in python?

送分小仙女□ 提交于 2019-11-29 22:00:14
How can one reliably determine if an object has a numpy type? I realize that this question goes against the philosophy of duck typing, but idea is to make sure a function (which uses scipy and numpy) never returns a numpy type unless it is called with a numpy type. This comes up in the solution to another question, but I think the general problem of determining if an object has a numpy type is far enough away from that original question that they should be separated. Use the builtin type function to get the type, then you can use the __module__ property to find out where it was defined: >>>

issubclass of abstract base class Sequence

对着背影说爱祢 提交于 2019-11-29 15:21:45
This list shows what methods you need to implement for your class to be "regarded" as Sequence: __getitem__ , __len__ , __contains__ , __iter__ , __reversed__ , index , and count . So why does this minimal implementation does not work, i.e. why issubclass(S, Sequence) is False ? from collections import * class S(object): def __getitem__(self, item): raise IndexError def __len__(self): return 0 def __contains__(self, item): return False def __iter__(self): return iter(()) def __reversed__(self): return self def index(self, item): raise IndexError def count(self, item): return 0 issubclass(S,

Does C# have an equivalent to Scala's structural typing?

痞子三分冷 提交于 2019-11-29 13:24:17
In Scala, I can define structural types as follows: type Pressable = { def press(): Unit } This means that I can define a function or method which takes as an argument something that is Pressable, like this: def foo(i: Pressable) { // etc. The object which I pass to this function must have defined for it a method called press() that matches the type signature defined in the type - takes no arguments, returns Unit (Scala's version of void). I can even use the structural type inline: def foo(i: { def press(): Unit }) { // etc. It basically allows the programmer to have all the benefits of duck

Simulating duck typing in Java

[亡魂溺海] 提交于 2019-11-29 12:48:12
问题 The problem: I'd like to be able to generically access in Java any property/field on a Java ojbect similarly to how a dynamic language (think Groovy, JavaScript) would. I won't know at the time I'm writing this plumbing code what type of object it is or what the property/field name will be. But I will know the property/field name when I go to use it. My current solution: So far I've written a simple wrapper class that uses java.beans.Introspector to grab the properties of a Bean/POJO and

Is there any point for interfaces in dynamic languages?

梦想的初衷 提交于 2019-11-28 22:26:45
问题 In static languages like Java you need interfaces because otherwise the type system just won't let you do certain things. But in dynamic languages like PHP and Python you just take advantage of duck-typing . PHP supports interfaces. Ruby and Python don't have them. So you can clearly live happily without them. I've been mostly doing my work in PHP and have never really made use of the ability to define interfaces. When I need a set of classes to implement certain common interface, then I just

How can I create a dynamic Select on an IEnumerable<T> at runtime?

陌路散爱 提交于 2019-11-28 20:54:48
Given that I have an IEnumerable<T> , where T is any object, how can I select a specific property from it, given that I know the name of the one of the property names at run time as a string? For example: var externalIEnumerable = DataPassedFromConsumingCode(); // `IEnumerable<T>` string knownPropertyName = "Foo"; var fooSelect = externalIEnumerable.Select(...); In essence, I'm obviously just doing externalIEnumerable.Select(x=> x.Foo); , but I need to perform this Select at runtime, when I don't have control over when it's initially created. -- ANSWER: Based on AlanT's answer, here's what I

Are there any static duck-typed languages?

不羁的心 提交于 2019-11-28 20:33:11
Can I specify interfaces when I declare a member? After thinking about this question for a while, it occurred to me that a static-duck-typed language might actually work. Why can't predefined classes be bound to an interface at compile time? Example: public interface IMyInterface { public void MyMethod(); } public class MyClass //Does not explicitly implement IMyInterface { public void MyMethod() //But contains a compatible method definition { Console.WriteLine("Hello, world!"); } } ... public void CallMyMethod(IMyInterface m) { m.MyMethod(); } ... MyClass obj = new MyClass(); CallMyMethod(obj