Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8866593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:49:36+00:00 2026-06-14T16:49:36+00:00

How to use polymorphism in functional programming (with dynamic type system)? Let’s consider following

  • 0

How to use polymorphism in functional programming (with dynamic type system)?

Let’s consider following example (first in OOP second in FP). The program is very simple – there are list of figures and we need to draw all of them, different figures use different drawing algorithms.

In OOP it can be done trivially, but how to do it in FP? Especially in languages with dynamic type system, like Scheme, Clojure (without static type resolving at compile time)?

I created simple code ( live version http://tinkerbin.com/0C3y8D9Z , press ‘Run’ button ). I used if/else switch in FP sample, but it’s a very bad approach. How such problem can be solved better?

Samples are in JavaScript, but it’s only for purpose of simplicity, it would be interesting to see a solution in any functional language with dynamic typing system.

OOP

var print = function(message){document.write(message + "\n<br/>")}

// Object Oriented Approach.
var circle = {
  draw: function(){print("drawing circle ...")}
}
var rectangle = {
  draw: function(){print("drawing rectangle ...")}
}

var objects = [circle, rectangle]
objects.forEach(function(o){
  o.draw()
})

FP

var print = function(message){document.write(message + "\n<br/>")}

// Functional Approach.
var circle = {type: 'Circle'}
var drawCircle = function(){print("drawing circle ...")}

var rectangle = {type: 'Rectangle'}
var drawRectangle = function(){print("drawing rectangle ...")}

var objects = [circle, rectangle]
objects.forEach(function(o){
  if(o.type == 'Circle') drawCircle(o)
  else if(o.type == 'Rectangle') drawRectangle(o)
  else throw new Error('unknown type!')
})
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:49:37+00:00Added an answer on June 14, 2026 at 4:49 pm

    OO polymorphism is not a part of functional programming. However some functional languages (e.g. clojure) have oo polymorphism.

    Another kind of polymorphism is multimethods

    (def circle {:type :circle
                 :radius 50})
    
    (def rectangle {:type :rectangle
                    :width 5
                    :height 10})
    
    (defmulti draw :type)
    
    (defmethod draw :circle [object]
      (println "circle: radius = " (:radius object)))
    
    (defmethod draw :rectangle [object]
      (println "rectangle: "
               "width = " (:width object)
               "height = " (:height object)))
    
    (doseq [o [rectangle circle]] (draw o))
    => rectangle:  width =  5 height =  10
       circle: radius =  50
    

    Or you just can use functional style

    (defn circle [] (println "drawing circle ..."))
    (defn rectangle [] (println "drawing rectangle ..."))
    
    (def objects [circle rectangle])
    
    (doseq [o objects] (o))
    => drawing circle ...
       drawing rectangle ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use polymorphic_path in a functional test in Rails 3. At first
Related: How can I use polymorphism in XML Serialization? I have a class I
I'm working on an experimental programming language that has global polymorphic type inference. I
I'm building a small template hierarchy and try to make use of class polymorphism.
Possible Duplicate: How can I simulate OO-style polymorphism in C? I'm trying to use
Possible Duplicate: Virtual Functions Object Slicing let's consider: #include <vector> #include <iostream> using namespace
I've been programming a while now at school, and I'm working on my first
Polymorphic Enums? In C++, we often use polymorphism to allow old code to handle
In Haskell I can define following data type: data Tree = Empty | Leaf
Use Case Show a photo uploaded by the user in a square box with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.