Enumerations in Scala

I’ve been looking at how to do enumerations in Scala, and there seems to be two popular methods: extending Enumeration and the use of case classes. The following are some examples…

Method 1: Extending Enumeration
Example:

object Animal extends Enumeration {
  val DOG, CAT, FISH, TURTLE = Value
}

Usage:

val a = Animal.DOG
if (a == Animal.DOG) println(":)")

This is nice and simple, and does the job when the enumerated values are intended to be simple and functionless.

However, what if you wanted Animals to have their own functionality, rather than just being an identifier?  Scala’s more flexible method of enumeration could be used…

Method 2: Case Classes
Lets say we wanted to make the animals make a sound:

abstract class Animal {
  def sound() : Any = throw new Exception("This animal does not make "
                                          + "a sound!")
}
case object DOG extends Animal() {
  override def sound() = println("wuuf.")
}
case object CAT extends Animal() {
  override def sound() = println("RAWR!")
}
case object FISH extends Animal()
case object TURTLE extends Animal()

Usage:

val a : Animal = CAT
a.sound()

val b : Animal = TURTLE
b.sound() // this will throw an exception

// can also be used in case statements
val output = a match {
  case CAT => "Hello thar kitteh!"
  case DOG => "Hello thar dawg!"
  case _ => "Hello thar... er... you're not a kitteh nor a dawg :| "
}

You can also do something very interesting things within the case statement, like decomposing the object’s attributes (you’ll need to scroll down a bit to find it).  The backfire to using this method over the Enumeration class is that, unlike Enumeration’s filter() and foreach() capability, you can’t seem to easily list out all the classes that extend the Animal class.

Widgeting Themes with Multiple Sidebars

Unfortunately, this theme (BloggingPro) was not widget enabled, so some work needs to be done. The Widgetizing Themes tutorial was useful for setting one sidebar, but in my case, there are multiple sidebars. Here is some code to make multiple sidebars widget ready…

First, this theme does not have a functions.php file for theme specific functions. Therefore, I uploaded wp-content/themes/[theme name]/functions.php. The file contains the following:

function.php

<?php
if ( function_exists('register_sidebar') ) {
  register_sidebar( array(
  'name' => 'sidebar_left',
  'before_widget' => '',
  'after_widget' => '',
  'before_title' => '<h2>',
  'after_title' => '</h2>') );
  register_sidebar( array(
  'name' => 'sidebar_right',
  'before_widget' => '',
  'after_widget' => '',
  'before_title' => '<h2>',
  'after_title' => '</h2>') );
}
?>

Next, we need to enable dynamic sidebars. Open wp-content/themes/[theme name]/sidebar.php.

We want to add dynamic sidebars for the div classes SRL and SRR. Different themes will use different names for sidebars div class names, so it may not look the exact same, but the concept is the same. After
<div class="SRL"> add:

 <?php if ( !function_exists('dynamic_sidebar')
        || !dynamic_sidebar('sidebar_left') ) : ?>

Right before the ending <div&gt for <div class="SRL"> add:

<?php endif; ?>

Similarly, do the same for the right side bar. After
<div class="SRR"> add:

 <?php if ( !function_exists('dynamic_sidebar')
        || !dynamic_sidebar('sidebar_right') ) : ?>

Right before the ending <div&gt for <div class="SRR"> add:

<?php endif; ?>

Now your theme is widgetized!

Using Perl to Generate Code for SHJS Setup

Using SHJS for syntax colouring requires includes for each language. Here’s a simple Perl script to generate these includes if you downloaded all the files to a directory and was too lazy to manually type them out

opendir(DIR, ".");
@jsFiles = grep(/\.js$/,readdir(DIR));

$output = '<script type="text/javascript" src="sh_main.js"></script>';
$output .= "\n";

foreach $file(@jsFiles)
{
  $output .= '<script type="text/javascript"';
  $output .= 'src="lang/'.$file.'"></script>'."\n";
}

$output .= '<link type="text/css" rel="stylesheet"';
$output .= 'href="css/sh_style.css">'."\n";

open(FILE, ">generate.txt") or die("Error");
print FILE $output;
 Page 3 of 3 « 1  2  3 
Get updates as often as we post! Subscribe to our full feed or comments feed.