CSC/ECE 517 Fall 2009/wiki2 6 ee: Difference between revisions
No edit summary |
(→OCaml) |
||
(14 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
==Programing Paradigms== | ==Programing Paradigms== | ||
Before delving into Hybrid Languages, here is a quick review of the paradigms we will be discussing. | |||
===Object Oriented Programming=== | ===Object Oriented Programming=== | ||
In Object Oriented Programming, code is grouped together into collections called Objects. Objects keep track of data and contain methods relevant to the data of the object. An object is described by a class. In order for an object to be used, it must be instantiated from a class. Objects can utilize other objects to help represent their data in meaningful ways (ie a school object can have many student objects).[http://www.codeproject.com/KB/architecture/OOP_Concepts_and_manymore.aspx#OOP] | |||
===Procedural Programming=== | ===Procedural Programming=== | ||
With procedural programming, operations are listed one after another in the order they are to be executed. Procedural programming also incorporates the notion of functions, which are chucks of code that can be called repeatedly, usually to accomplish one specific task. Functions take in data to be processed as arguments and output the result of the computation. However, the output of a function can vary greatly, depending on the state of the program at any given moment.[http://knol.google.com/k/chris/procedural-programming/3qdf5q2hz62mx/93#] | |||
===Functional Programming=== | ===Functional Programming=== | ||
Functional Programming has strong roots in mathematics and focuses on calculations. Functions are called only to compute data and do not change the state of the program. The output of functions in functional programming depend only on the arguments to the function, with no dependency on the state of the system. Also, all variables are final - that is, once assigned to, they cannot be changed. All these factors make functional programs very reliable and repeatable.[http://www.defmacro.org/ramblings/fp.html] | |||
==Hybrid Object Oriented Programming Languages== | ==Hybrid Object Oriented Programming Languages== | ||
The following Languages use Hybrid Object Oriented Programming Paradigms: | |||
===OCaml=== | ===OCaml=== | ||
OCaml was an addition to the Caml programming language, which was itself derived from ML. ML (Meta-Language) was originally developed as a highly functional programming language. It was used to assist with mathematical proofs. Caml was derived from ML to create a flexible programming language and it began to incorporate more procedural programming aspects. Finally, OCaml was implemented, which added object oriented features such as multiple inheritance and module hierarchy management.[http://caml.inria.fr/about/history.html] | |||
====Design Considerations==== | |||
*By default, data in OCaml (variables, arrays, ect) are not modifiable, because of its roots in functional programming. However, to allow for more expressive procedural programming, variables are allowed to be declared as modifiable. | |||
*To ensure safety, OCaml is statically typed and type matching is enforced at compile time. This prevents many common run time errors, but does require attention to detail by the programmer. This strong-typing is also true of objects in OCaml. At compile time, OCaml makes sure that no object will receive a message it is not able to understand. | |||
*OCaml allows for the definition of new data types using the concept of a data constructor.[http://caml.inria.fr/about/index.en.html] | |||
====Examples==== | |||
Basic class for Hello World program | |||
<pre> | |||
class hello_all = | |||
object (self) | |||
val hello = "Hi there" | |||
method greet = | |||
print_string hello | |||
end;; | |||
</pre> | |||
[http://www.ocaml-tutorial.org/objects] | |||
===Scala=== | ===Scala=== | ||
Scala was written from the ground up Martin Odersky. It is completely interoperable with Java and naively compiles to Java byte code. Because of this, any Scala program can invoke any Java library, which lets developers leverage a multitude of available Java resources, while working within the Scala language.[http://www.artima.com/weblogs/viewpost.jsp?thread=163733] | |||
====Design Considerations==== | |||
*Scala aims to allow programmers to leverage the universal compatibility and extensive library support of the java language, while using a compact, flexible and easy to use syntax | |||
*For safety of data, Scala is statically typed | |||
*For convenience, Scala does not require the type of a variable to be declared, the Scala compiler can infer the type | |||
*Scala is a functional language, in that it allows function passing and nested functions | |||
*But in order to act as an OO language, Scala also embraces the everything-is-an-object paradigm | |||
*Scala allows for multiple inheritance using mixins | |||
*Instead of interfaces, Scala uses the idea of "traits" which are like interfaces, but they ''can'' have fully defined methods[http://www.scala-lang.org/node/25] | |||
====Example==== | |||
"Fancy" Hello world: | |||
<pre> | |||
//Note that the arguments to the constructor are just after the class name | |||
class FancyGreeter(greeting: String) | |||
{ | |||
def greet() = println(greeting) | |||
} | |||
val g = new FancyGreeter("Salutations, world") | |||
g.greet | |||
</pre> | |||
Example using a trait | |||
<pre> | |||
trait Introduction{ | |||
def intro() = "Hello there" | |||
} | |||
= | trait Excited extends Introduction { | ||
override def intro() = super.intro() + "!" | |||
} | |||
</pre> | |||
[http://www.artima.com/scalazine/articles/steps.html] | |||
== | ==References== | ||
#[http://www.codeproject.com/KB/architecture/OOP_Concepts_and_manymore.aspx#OOP Introduction to Object Oriented Programming Concepts] | |||
#[http://knol.google.com/k/chris/procedural-programming/3qdf5q2hz62mx/93# Procedural Programming] | |||
#[http://www.defmacro.org/ramblings/fp.html Functional Programming for the Rest Of Us] | |||
#[http://caml.inria.fr/about/history.html A History of Caml] | |||
#[http://caml.inria.fr/about/index.en.html About OCaml] | |||
#[http://www.artima.com/weblogs/viewpost.jsp?thread=163733 A Brief History of Scala] | |||
#[http://www.scala-lang.org/node/25 The Scala Programming Language] | |||
#[http://www.artima.com/scalazine/articles/steps.html First Steps to Scala] |
Latest revision as of 03:05, 15 October 2009
Hybrid Object Oriented Programing Languages
Hybrid Object Oriented Programming Languages are object oriented languages that also include one or more additional programing paradigms. Commonly, hybrid object oriented languages include procedural and functional programing features, in addition to being object oriented. Having multiple paradigms embraced in one language adds flexibility to the developer, letting them leverage the most appropriate programing methods, without having to switch languages.
Programing Paradigms
Before delving into Hybrid Languages, here is a quick review of the paradigms we will be discussing.
Object Oriented Programming
In Object Oriented Programming, code is grouped together into collections called Objects. Objects keep track of data and contain methods relevant to the data of the object. An object is described by a class. In order for an object to be used, it must be instantiated from a class. Objects can utilize other objects to help represent their data in meaningful ways (ie a school object can have many student objects).[1]
Procedural Programming
With procedural programming, operations are listed one after another in the order they are to be executed. Procedural programming also incorporates the notion of functions, which are chucks of code that can be called repeatedly, usually to accomplish one specific task. Functions take in data to be processed as arguments and output the result of the computation. However, the output of a function can vary greatly, depending on the state of the program at any given moment.[2]
Functional Programming
Functional Programming has strong roots in mathematics and focuses on calculations. Functions are called only to compute data and do not change the state of the program. The output of functions in functional programming depend only on the arguments to the function, with no dependency on the state of the system. Also, all variables are final - that is, once assigned to, they cannot be changed. All these factors make functional programs very reliable and repeatable.[3]
Hybrid Object Oriented Programming Languages
The following Languages use Hybrid Object Oriented Programming Paradigms:
OCaml
OCaml was an addition to the Caml programming language, which was itself derived from ML. ML (Meta-Language) was originally developed as a highly functional programming language. It was used to assist with mathematical proofs. Caml was derived from ML to create a flexible programming language and it began to incorporate more procedural programming aspects. Finally, OCaml was implemented, which added object oriented features such as multiple inheritance and module hierarchy management.[4]
Design Considerations
- By default, data in OCaml (variables, arrays, ect) are not modifiable, because of its roots in functional programming. However, to allow for more expressive procedural programming, variables are allowed to be declared as modifiable.
- To ensure safety, OCaml is statically typed and type matching is enforced at compile time. This prevents many common run time errors, but does require attention to detail by the programmer. This strong-typing is also true of objects in OCaml. At compile time, OCaml makes sure that no object will receive a message it is not able to understand.
- OCaml allows for the definition of new data types using the concept of a data constructor.[5]
Examples
Basic class for Hello World program
class hello_all = object (self) val hello = "Hi there" method greet = print_string hello end;;
Scala
Scala was written from the ground up Martin Odersky. It is completely interoperable with Java and naively compiles to Java byte code. Because of this, any Scala program can invoke any Java library, which lets developers leverage a multitude of available Java resources, while working within the Scala language.[7]
Design Considerations
- Scala aims to allow programmers to leverage the universal compatibility and extensive library support of the java language, while using a compact, flexible and easy to use syntax
- For safety of data, Scala is statically typed
- For convenience, Scala does not require the type of a variable to be declared, the Scala compiler can infer the type
- Scala is a functional language, in that it allows function passing and nested functions
- But in order to act as an OO language, Scala also embraces the everything-is-an-object paradigm
- Scala allows for multiple inheritance using mixins
- Instead of interfaces, Scala uses the idea of "traits" which are like interfaces, but they can have fully defined methods[8]
Example
"Fancy" Hello world:
//Note that the arguments to the constructor are just after the class name class FancyGreeter(greeting: String) { def greet() = println(greeting) } val g = new FancyGreeter("Salutations, world") g.greet
Example using a trait
trait Introduction{ def intro() = "Hello there" } trait Excited extends Introduction { override def intro() = super.intro() + "!" }