duck-typing

Duck typing in Delphi 2007?

半腔热情 提交于 2019-11-26 20:27:05
问题 Question: Is there a way to do duck typing with Delphi 2007 (i.e. without generics and advanced Rtti features)? Duck typing Resources for Delphi 2010 onward: Duck Duck Delphi in google project by ARCANA. Duck Typing in Delphi by Daniele Teti. AOP and duck typing in Delphi by Stefan Glienke. Last Edit: I've delved more into the resouces listed above and studied every posted answers here. I end up refining my requirement a made a follow up post to this question. 回答1: With the help of the

Duck type testing with C# 4 for dynamic objects

主宰稳场 提交于 2019-11-26 18:11:46
问题 I'm wanting to have a simple duck typing example in C# using dynamic objects. It would seem to me, that a dynamic object should have HasValue/HasProperty/HasMethod methods with a single string parameter for the name of the value, property, or method you are looking for before trying to run against it. I'm trying to avoid try/catch blocks, and deeper reflection if possible. It just seems to be a common practice for duck typing in dynamic languages (JS, Ruby, Python etc.) that is to test for a

what is the right way to treat Python argparse.Namespace() as a dictionary?

牧云@^-^@ 提交于 2019-11-26 08:47:37
问题 If I want to use the results of argparse.ArgumentParser() , which is a Namespace object, with a method that expects a dictionary or mapping-like object (see collections.Mapping), what is the right way to do it? C:\\>python Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win 32 Type \"help\", \"copyright\", \"credits\" or \"license\" for more information. >>> import argparse >>> args = argparse.Namespace() >>> args.foo = 1 >>> args.bar = [1,2,3] >>> args.baz = \

What's an example of duck typing in Java?

梦想与她 提交于 2019-11-26 08:12:50
问题 I just recently heard of duck typing and I read the Wikipedia article about it, but I\'m having a hard time translating the examples into Java, which would really help my understanding. Would anyone be able to give a clear example of duck typing in Java and how I might possibly use it? 回答1: Java is by design not fit for duck typing. The way you might choose to do it is reflection: public void doSomething(Object obj) throws Exception { obj.getClass().getMethod("getName", new Class<?>[] {})

Duck typing in the C# compiler

廉价感情. 提交于 2019-11-26 07:40:08
问题 Note This is not a question about how to implement or emulate duck typing in C#... For several years I was under the impression that certain C# language features were depdendent on data structures defined in the language itself (which always seemed like an odd chicken & egg scenario to me). For example, I was under the impression that the foreach loop was only available to use with types that implemented IEnumerable . Since then I\'ve come to understand that the C# compiler uses duck typing

Why duck typing is allowed for classes in TypeScript

試著忘記壹切 提交于 2019-11-26 04:53:32
问题 Looks like in TypeScript it\'s absolutely fine (from the compiler perspective) to have such code: class Vehicle { public run(): void { console.log(\'Vehicle.run\'); } } class Task { public run(): void { console.log(\'Task.run\'); } } function runTask(t: Task) { t.run(); } runTask(new Task()); runTask(new Vehicle()); But at the same time I would expect a compilation error , because Vehicle and Task don\'t have anything in common. And sane usages can be implemented via explicit interface

What is duck typing?

℡╲_俬逩灬. 提交于 2019-11-25 23:38:45
问题 I came across the term duck typing while reading random topics on software online and did not completely understand it. What is “duck typing”? 回答1: It is a term used in dynamic languages that do not have strong typing. The idea is that you don't need a type in order to invoke an existing method on an object - if a method is defined on it, you can invoke it. The name comes from the phrase "If it looks like a duck and quacks like a duck, it's a duck". Wikipedia has much more information. 回答2: