problem about duck-typing-Collection of common programming errors


  • Rune FS
    reflection f# duck-typing dci
    using let inline and member constraints I’ll be able to make duck typing for known members but what if I would like to define a generic function like so:let duckwrapper<‘a> duck = …with the signature ‘b -> ‘a and where the returned value would be an object that implemented ‘a (which would be an interface) and forwarded the calls to duck.I’ve done this in C# using Reflection.Emit but I’m wondering if F# reflection, quotations or other constructs would make it easier.Any suggestions on how to

  • ehsanul
    ruby nil duck-typing
    Ruby’s duck-typing is great, but this is the one way that it bites me in the ass. I’ll have some long running text-processing script or something running, and after several hours, some unexpected set of circumstances ends up causing the script to exit with at NoMethodError due to a variable becoming nil. Now, once it happens, it’s usually an easy fix, but it would be nicer if I could predict these better, or at least handle these types of errors more gracefully. Sorry for the vagueness of the qu

  • warwaruk
    python validation assertions duck-typing
    About duck typing:Duck typing is aided by habitually not testing for the type of arguments in method and function bodies, relying on documentation, clear code and testing to ensure correct use.About argument validation (EAFP: Easier to ask for forgiveness than permission). An adapted example from here:…it is considered more pythonic to do :def my_method(self, key):try:value = self.a_dict[member]except TypeError:# do something elseThis means that anyone else using your code doesn’t have to use

  • Wim Coenen
    c# adapter duck-typing
    After looking at how Go handles interfaces and liking it, I started thinking about how you could achieve similar duck-typing in C# like this:var mallard = new Mallard(); // doesn’t implement IDuck but has the right methods IDuck duck = DuckTyper.Adapt<Mallard,IDuck>(mallard);The DuckTyper.Adapt method would use System.Reflection.Emit to build an adapter on the fly. Maybe somebody has already written something like this. I guess it’s not too different from what mocking frameworks already do

  • Mare Infinitus
    c# java inheritance interface duck-typing
    Today we had a strange happening with a close() method.Here is the code in doubt:interface ICloseable {void Close(); }public class Closer {public void Close(){Console.WriteLine(“closed”);} }public class ConcreteCloser : Closer, ICloseable { }class Program {static void Main(string[] args){var concrete = new ConcreteCloser();concrete.Close();Console.ReadKey();} }So the question is:The base class does not implement the interface.Why does the compiler accept the Closer.close() method as implementat

  • menjaraz
    delphi delphi-2007 duck-typing
    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.

  • Dinosaur Jones
    java ruby dynamic-languages duck-typing flexible
    I’ve been using Java almost since it first came out but have over the last five years gotten burnt out with how complex it’s become to get even the simplest things done. I’m starting to learn Ruby at the recommendation of my psychiatrist, uh, I mean my coworkers (younger, cooler coworkers – they use Macs!). Anyway, one of the things they keep repeating is that Ruby is a “flexible” language compared to older, more beaten-up languages like Java but I really have no idea what that means. Could some

  • Thomson
    c++ templates duck-typing
    To me, C++ template used the idea of duck typing, is this right? Does it mean all generic types referenced in template class or method are duck type?

  • Roman A. Taycher
    duck-typing dynamic-typing
    I’m used to dynamic typing meaning checking for type info of object/non object oriented structure at runtime and throwing some sort of type error, ie if it quacks like a duck its a duck. Is there a different type of dynamic typing (please go into details).

  • sawa
    ruby oop duck-typing
    Since there is no type in ruby, how do Ruby programmers make sure a function receives correct arguments? Right now, I am repeating if object.kind_of/instance_of statements to check and raise runtime errors everywhere, which is ugly. There must be a better way of doing this.

  • leonbloy
    java interface static-typing duck-typing structural-typing
    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(Acti

  • James Crowley
    c# castle duck-typing dynamic-proxy linfu
    I have an object handed into our library and passed through various processes. I need to attach some additional information to these objects as they pass through various stages and out the other end – a kind of dynamic decorator pattern, I guess, except adding additional properties rather than changing existing behaviour.I was hoping to use LinFu or Castle to create a dynamic proxy and implement an additional interface on the object to store this. Components that know about the extended interfac

  • Viclib

  • GenericTypeTea
    c# linq lambda duck-typing
    I asked a very similar question yesterday, but it wasn’t until today I realised the answer I accepted doesn’t solve all my problems. I have the following code:public Expression<Func<TItem, object>> SelectExpression<TItem>(string fieldName) {var param = Expression.Parameter(typeof(TItem), “item”);var field = Expression.Property(param, fieldName);return Expression.Lambda<Func<TItem, object>>(field, new ParameterExpression[] { param }); }Which is used as follows:string

  • GenericTypeTea
    c# linq reflection duck-typing
    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, wh

  • Chris Morris
    templates interfaces polymorphism duck-typing explicit
    I think I understand the actual limitations of compile-time polymorphism and run-time polymorphism. But what are the conceptual differences between explicit interfaces (run-time polymorphism. ie virtual functions and pointers/references) and implicit interfaces (compile-time polymorphism. ie. templates).My thoughts are that two objects that offer the same explicit interface must be the same type of object (or have a common ancestor), while two objects that offer the same implicit interface need

  • Raynos
    object-oriented duck-typing polymorphism
    From Polymorphism on WIkipediaIn computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface.From duck typing on WikipediaIn computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object’s current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interf

  • julien
    objective-c runtime protocols duck-typing
    @interface Dog : NSObject @end@implementation Dog – (id)valueForUndefinedKey:(NSString *)key {if ([key isEqualToString:@”quacks”])return YES; } @endThe above allows to leverage KVC and write something like :[[Dog new] valueForKey:@”quacks”]; // YESHowever, can the objc runtime be used to leverage the same KVC mechanism, AND conform to the Duck protocol at runtime ?@protocol Duck <NSObject> @optional@property (readonly) BOOL quacks; @endid<Duck> dug = (id<Duck>)[Dog new]; dug.qu

  • Gordon Gustafson
    php interface dynamic-languages duck-typing
    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 descr

  • DGM
    ruby duck-typing
    Coming from a Java background, I am a little perturbed by Ruby’s completely blasé attitude toward its method parameters. Whereas in Java I could guarantee that parameter x was the type necessary for the method to work properly, in Ruby I have no way of guaranteeing that x is an integer, a string, or anything else for that matter.Example: if I wanted to write an absolute_value method in Java, the header would be something likepublic static int absoluteValue(int x)In Ruby it would be something li

  • meagar
    webforms duck-typing
    I use visual studio and c# win forms (web forms). I try open connection to MS 2005 Server and reader query.here – is sql = “SELECT Files.ID, Files.FileName, Files.File_Name, Files.CreatingDate, aspnet_Users.UserName, aspnet_Membership.Email ” + “FROM aspnet_Membership ” + “INNER JOIN aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId ” + “INNER JOIN Files ON aspnet_Membership.UserId = Files.UserId”; And those tables exists in databes. Invalid object name ‘aspnet_Membership’. Descrip

  • Mike
    perl language-agnostic duck-typing static-typing
    In my current job I’m building a suite of Perl scripts that depend heavily on objects. (using Perl’s bless() on a Hash to get as close to OO as possible) Now, for lack of a better way of putting this, most programmers at my company aren’t very smart. Worse, they don’t like reading documentation and seem to have a problem understanding other people’s code. Cowboy coding is the game here. Whenever they encounter a problem and try to fix it, they come up with a horrendous solution that actually sol

  • Prof. Falken
    php object type-conversion duck-typing
    class wat {public $a = 3.14;public $x = 9;public $y = 2; }$a = new wat();var_dump(1000 + $a); var_dump($a + 1000);The output is:int(1001) int(1001)Well, adding the wat* object to an integer is obviously not the right thing to do, since PHP complains about it with “Object of class wat could not be converted to int”, but still, what does it do? (I also have a practical reason for asking this, I want to refactor a function to get rid of the “PHP Notice”, while still keeping behaviour completely unc

Web site is in building