-2

Can any language be used to program in any paradigm? For example C doesn't have classes but s it is possible to program in OOP. There are some languages (such as assembly) I can't see using OOP in.

Celeritas
  • 14,489
  • 36
  • 113
  • 194

3 Answers3

1

Yes, simply due to the fact you can implement an interpreter for your $favorite $paradigm in the host language.

Practically though, this is not feasible, efficient or right.

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
  • 1
    But then are you programming in the host language, or programming in your new language? At uni we wrote an interpreter for an imperative scripting language in Haskell; I wouldn't have said we were programming in Haskell when we wrote code to feed to this interpreter. And I find the suggestion that you're programming in C when you write Python code to be pretty ludicrous. – Ben May 20 '12 at 03:02
  • 1
    The OP's question is pretty ludicrous. The implication of sensible embedding of semantics A in semantics B for all semantics A B is non-sensical. – Don Stewart May 20 '12 at 04:39
  • That doesn't make it a ludicrous question, that makes the answer a straightforward "no". If you believe that, why did you write a "yes" answer based on a pretty enormous stretch to the concept of programming in a language? – Ben May 20 '12 at 05:06
0

C++ is ultimately assembly, you just have a compiler to write the assembly for you from a nicer description. So sure you can do OOP in assembly, just as you can do OOP in C; it's just that a lot of the OO concepts end up being implemented with convention and programmer discipline rather than being forced by the structure of the language, with the result that huge classes of bugs become possible that your language tools probably won't be very good at helping you find.

Similar arguments follow for most paradigm/language mismatches. Lots of object-oriented programs have been written in C this way, so it can even be a somewhat practical thing to do, not just an academic matter.

It can be a little harder when what you want is to remove restrictions rather than add them.

In purity-enforced languages such as Haskell and Mercury you can't suddenly break out object-oriented style packets-of-encapsulated-mutable-state in the middle of arbitrary pure code (at least not without using "all bets are off" features like unsafePerformIO in Haskell or promise_pure in Mercury to lie to the compiler, at which point your program may well completely fail to work unless you can wrap a pure interface around the regions in which you do this). However you can write whole programs in procedural or object-oriented style in these languages, by never leaving the mechanism they use to do IO.

Likewise, if you consider the use of duck typing in dynamic languages to be a paradigm, it's pretty painful to get something similar in languages with static typing, but you can always find a way to represent your dynamic types as data. But you again find yourself doing thing with convention and reimplementation that you would get for free if you were really using a duck typing language.

I'm pretty sure it would be hard to find a language (usable for writing general purpose programs) that can't be adapted to write code in any paradigm you like. The adaptation may not produce very efficient code (sometimes it can though; adapting C or assembly to any paradigm can usually be made pretty much as efficient as if you had a language tuned for that paradigm), and it will almost certainly be horribly inefficient in terms of programmer time.

Ben
  • 68,572
  • 20
  • 126
  • 174
-2

No, not all languages can be used to program in any paradigm. However, the more popular ones - python, c++, etc all allows you to chose how you want to program. Even php is adding OO support.

Ayrx
  • 2,092
  • 5
  • 26
  • 34