The Artima Developer Community
Sponsored Link

Artima Developer Spotlight Forum
Ted Neward on Scala Packages and Access Modifiers

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Ted Neward on Scala Packages and Access Modifiers Posted: Jul 30, 2008 8:50 PM
Reply to this message Reply
Advertisement

In the latest installment of his IBM developerWorks series, Packages and access modifiers, Ted Neward explains how Scala's package and access modifiers offer potentially greater flexibility than similar constructs do in Java:

Scala's packaging, import, and access modifier mechanisms provide the fine degree of control and encapsulation that a traditional Java programmer has never enjoyed. For example, they offer the ability to import select methods of an object, making them appear as global methods, without the traditional drawbacks of global methods; they make working with those methods exceedingly easy, particularly if those methods are methods that provide higher-order functionality...

Scala takes a slightly different approach in respect to packaging, treating it as a combination of the Java language's declaration approach and C#'s scoped approach. With that in mind, a Java developer can do the traditional Java approach and put a package declaration at the top of a .scala file just as normal Java classes do; the package declaration applies across the entire file scope just as it does in Java code. Alternatively, a Scala developer can use Scala's package "scoping" approach in which curly braces delimit the scope of the package statement...

package com
{
  package tedneward
  {
    package scala
    {
      package demonstration
      {
        object App
        {
          def main(args : Array[String]) : Unit =
          {
            System.out.println("Howdy, from packaged code!")
            args.foreach((i) => System.out.println("Got " + i) )
          }
        }
      }
    }
  }
}

Neward points out that the latter construct is equivalent to the following:

package com.tedneward.scala.demonstration
{
  object App
  {
      def main(args : Array[String]) : Unit =
      {
        System.out.println("Howdy, from packaged code!")
        args.foreach((i) => System.out.println("Got " + i) )
      }
  }
}

As well, Neward shows that in Scala, the import statement is not relegated to the beginning of a file, but can be used anywhere in code, and that using import that way provides a scope for the imported classes.

Neward also discusses access modifiers in Scala, pointing out that:

Scala:

  • Does away with package-level qualification (in a way)
  • Uses "public" by default
  • Specifies "private" to mean "accessible only to this scope"

"protected" is definitely different from its counterpart in Java code; where a Java protected member is accessible to both subclasses and the package in which the member is defined, Scala chooses to grant access only to subclasses. This means that Scala's version of protected is more restrictive (although arguably more intuitively so) than the Java version.

Finally, Neward's article discusses more fine-grained uses of access modifiers, such as the ability to qualify an access modifier with a package name, for instance.

What do you think of Scala's packages and access modifiers? Do you think the added flexibility would solve problems you're currently encountering in your Java code?

Topic: Ted Neward on Scala Packages and Access Modifiers Previous Topic   Next Topic Topic: The Type-Safe Builder Pattern in Scala

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use