CSC/ECE 517 Fall 2009/wiki2 6 ee: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 25: Line 25:
*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.
*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]
*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===
Line 36: Line 51:
*But in order to act as an OO language, Scala also embraces the everything-is-an-object paradigm
*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
*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
*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]
[http://www.scala-lang.org/node/25]


====Example====
====Example====
Line 43: Line 57:


<pre>
<pre>
//Note that the arguments to the constructor are just after the class name
//Note that the arguments to the constructor are just after the class name
class FancyGreeter(greeting: String)
class FancyGreeter(greeting: String)
Line 52: Line 65:
val g = new FancyGreeter("Salutations, world")
val g = new FancyGreeter("Salutations, world")
g.greet
g.greet
</pre>
</pre>


Using traits
Example using a trait


<pre>
<pre>
trait Introduction{
trait Introduction{
   def intro() = "Hello there"
   def intro() = "Hello there"
Line 66: Line 77:
   override def intro() = super.intro() + "!"
   override def intro() = super.intro() + "!"
}
}
</pre>
</pre>
[http://www.artima.com/scalazine/articles/steps.html]


==References==
==References==
Line 77: Line 88:
#[http://www.artima.com/weblogs/viewpost.jsp?thread=163733 A Brief History of Scala]
#[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.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;;

[6]

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() + "!"
}

[9]

References

  1. Introduction to Object Oriented Programming Concepts
  2. Procedural Programming
  3. Functional Programming for the Rest Of Us
  4. A History of Caml
  5. About OCaml
  6. A Brief History of Scala
  7. The Scala Programming Language
  8. First Steps to Scala