Imports
The first thing that you may notice in some code you're reading is the use of _ in imports. The best way I've found to deal with _ is to replace it in my head with "whatever". It works in imports, but it also works in other cases where you encounter _.
So, when you see
import com.nancydeschenes.mosaique.model._
it means "import whatever you find under com.nancydeschenes.mosaique.model"
You can lump together multiple imports from the same package:
import scala.xml.{ NodeSeq, Group }
Imports are relative:
import net.liftweb._ import http._
imports whatever's needed in net.liftweb, and whatever's needed from net.liftweb.http.
But what if I have a package called http, and don't want net.liftweb.http? use the _root_ package:
import net.liftweb._ import _root_.http._
Packages
Like Java, code goes in packages. Unlike Java, packages aren't dictated by the directory structure. You can put multiple public classes in one file, and the location of the file doesn't imply what package it will be in. Do keep things simple, tho, if you ever want to be able to re-read yourself.
You can use the same package statement you would with Java:
package com.nancydeschenes.mosaique.snippet
Or, you can use the package block:
package com.nancydeschenes.mosaique { // Some stuff package snippet { // stuff that ends up in com.nancydeschenes.mosaique.snippet } }
It was nice to know all about how scala imports and packages works. Scala’s import statement almost look like Java’s, but with some little variations that allow you to create more concise code.
ReplyDelete