polymorphism

Creating polymorphic recursive types in Haskell

Hi I'm trying to create a Tree type in Haskell. I've used this simple data constructor for storing a tree in which each node can either be empty, be a leaf containing an integer, or be a node containing an integer with branches to two other leaves/nodes. Here's what I've got: module Tree ( Tree(Empty, Leaf, Node) ) where data Tree = E...

sqlalchemy polymorphic many to many relation

I'm trying to allow users to 'favorite' different items in my web app. So, for example, a user can favorite a comment and favorite a news story. I then want to query all of the items a user has favorited, and load the associated objects (whether it be a news story or comment etc) polymorphically to display the list of objects to the user...

C++ Templates polymorphism

Hello. I have this structure of classes. class Interface{ ... } class Foo : public Interface{ ... } template <class T> class Container{ ... } And I have this constructor of some other class Bar. Bar(const Container<Interface> & bar){ ... } When I call the constructor this way I get "no matching function" error. Container<Foo> c...

What is a good method to have a common interface to code without incurring the cost of dynamic lookup?

I am writing some code for handling data. There are a number of groups of processing functions that can be chosen by the user that are then applied to the dataset. I would like to implement all these groups in separate places, but since they all take the same parameters and all do similar things I would like for them to have a common int...

C# polymorphism simple question

Hi there, I got a class X and a class Y, the latter which derives from X : class x {} class y : x {} Then somewhere I am using a list of X : List<X> lstX; ... Then I'd like to use a new list of Y, from the data in my other list...something along those lines : List<Y> lstY = lstX; I would believe that the items in the list of X ...

inheritance and class members

GIVEN: class A { String s = "A"; } class B extends A { String s = "B"; } public class C { public static void main(String[] args){ new C().go();} void go() { A a = new B(); System.out.println(a.s); } } Question: What are the mechanics behind JVM when this code is run? How come a.s prints back ...

Is there anything composition cannot accomplish that inheritance can?

Composition and inheritance. I am aware that they are both tools to be chosen when appropriate, and context is very important in choosing between composition and inheritance. However, the discussion about the appropriate context for each is usually a little fuzzy; this has me beginning to consider just how distinctly inheritance and po...

Does @AttributeOverride annotation breaks polymorphism?

Hi, I need help with inheritance in JPA I will illustrate my problem with an example: Supose we have an Animal class and a Cat class. @Entity @Table(name = "animal") @DiscriminatorColumn(name="animal_type",columnDefinition="Integer",length=11) public class Animal implements Serializable{ @Id @GeneratedValue (strategy = Generat...

c# instantiate class from string

I have an abstract class and I want to initalize it to a class that extends it. I have the child classes name as a string. Besides this... String childClassString; MyAbstractClass myObject; if (childClassString = "myExtenedObjectA") myObject = new ExtenedObjectA(); if (childClassString = "myExtenedObjectB") myObject = new Ext...

paperclip polymorphic association and saving

I changed my photo.rb model to be polymorphic and be usable to all sorts of other models needing to save images and it works fine except I can't figure out how to save new attachments properly through the parent model. Any ideas? Do I have to approach this differently somehow? As, its also not getting the imageable_type...which i'll h...

Handling subtypes in ASP.NET MVC

Hi, I can't think of a "best" way of handling the following situation - basically, I have a bunch of stored objects that inherit from a base type and would like to be able to retrieve one from storage, find its subtype (perhaps via "if(x is y)") and then act accordingly - some using a shared implementation, others with dedicated logic an...

problem using accept_nested_attributes with polymorphic relations and inheritance

Having this mix of polymorphism and inhertiance, I encounter some weird behaviour: Migration class CreateCommons < ActiveRecord::Migration def self.up create_table :commons do |t| t.string :name t.string :configurable_type t.integer :configurable_id end create_table :specifics do |t| t.integer :nu...

How to use attribute value as a discriminator for XML polymorphic type selection?

I am trying to write XML Schema by existing XML format description (i.e. document - free form description of elements multiplicity and types). My final idea is to feed such XSD to code generator and get binding classes. Here is an example I cannot cope with: packet1.xml: <?xml version="1.0" ?> <packet kind="type1"> <field1>value1<...

Newbie polymorphism question using generics

I have the following method that takes in a details object, validates it, converts it to a request and enqueues it. Everything is fine apart from the validate request which I am having trouble with. Basically, there is different validation logic for each different details object. I know from the generic constraint that the details object...

Code reuse through generics vs polymorphism

What is the better way to reuse implementation: inheritance or generics? The model is following: Script has Steps, Steps have Elements. Tree structure is double linked, i.e. Steps know their Script and Elements now their Step. Now, there are 2 types of Scripts: Templates and Runs, where a Run is created at first as a copy of the Templ...

How to call base.base.method() ??

// Cannot change source code class Base { public virtual void Say() { Console.WriteLine("Called from Base."); } } // Cannot change source code class Derived : Base { public override void Say() { Console.WriteLine("Called from Derived."); base.Say(); } } class SpecialDerived : Derived { ...

Adding virtual functions without modifying the original classes

Let's say we already have a hierarchy of classes, e.g. class Shape { virtual void get_area() = 0; }; class Square : Shape { ... }; class Circle : Shape { ... }; etc. Now let's say that I want to (effectively) add a virtual draw() = 0 method to Shape with appropriate definitions in each sub-class. However, let's say I want to do this w...

Casting classes

I vave 2 class: public class ClassA public class ClassB (from another namespace) : ClassA I use methdo at ClassA public static ClassA Deserialize(string path) { ClassA classA; //classA=code... return classA; } I invoke this method at classB public void DoSomething() { ClassB classB=(ClassB)ClassA.Deserialize("c:\directory\file.x...

Calling virtual functions inside member functions

I'm reading Thinking in C++ by Bruce Eckel. In Chapter 15 (Volume 1) under the heading "Behaviour of virtual functions inside constructor", he goes What happens if you’re inside a constructor and you call a virtual function? Inside an ordinary member function you can imagine what will happen – the virtual call is resolved a...

Is polymorphism via classes and methods bad? (2)

I have recently read that polymorphism via classes and methods is a bad way to go about polymorphism. Why is this a problem and what are the alternatives? I know this is a somewhat subjective question, but it is a legitimate one that spawns from the functional programming perspective such as: http://blog.thinkrelevance.com/2009/7/8/tria...