Showing posts with label import alias. Show all posts
Showing posts with label import alias. Show all posts
Thursday, January 21, 2010
Exclude style import semantics
I can't remember if I posted this tip yet so I will do it again (or not :-P ).
If you want to import all classes in a package or object exception for one or two there is a neat trick to exclude the unwanted elements:
If you want to import all classes in a package or object exception for one or two there is a neat trick to exclude the unwanted elements:
- /*
- The File class is aliased to _ (oblivion) and then everything is imported (except File since it does not exist in that scope)
- Note: File can be imported later
- */
- scala> import java.io.{File=>_,_}
- import java.io.{File=>_, _}
- scala> new File("it is a file")
- < console>:8: error: not found: type File
- new File("it is a file")
- scala> object O {
- | def one = 1
- | def two = 2
- | def o = 0
- | }
- defined module O
- /*
- Same tricks can apply to importing methods and fields from objects
- */
- scala> import O.{o=>_, _}
- import O.{o=>_, _}
- scala> one
- res2: Int = 1
- scala> two
- res3: Int = 2
- scala> o
- < console>:15: error: not found: value o
- o
- // Once a class is imported into scope it can not be removed
- scala> import java.io.File
- import java.io.File
- scala> import java.io.{File=>_}
- import java.io.{File=>_}
- /*
- this still works because the previous import statement only adds an alias it does not remove the alias
- */
- scala> new File("..")
- res6: java.io.File = ..
- // There can be multiple aliases in a scope
- scala> import java.io.{File=>jfile}
- import java.io.{File=>jfile}
- scala> new jfile("..")
- res0: java.io.File = ..
- // one more example of importing from objects
- scala> case class X(a:Int, b:Int, c:Int)
- defined class X
- scala> val x = new X(1,2,3)
- x: X = X(1,2,3)
- scala> import x.{a=>_,b=>_,_}
- import x.{a=>_, b=>_, _}
- scala> c
- res1: Int = 3
- scala> b
- < console>:14: error: not found: value b
- b
- ^
Labels:
import,
import alias,
intermediate,
Scala
Wednesday, September 9, 2009
Using objects to access trait functionality
Today's topic is based on an article by Bill Venners. http://www.artima.com/scalazine/articles/selfless_trait_pattern.html. I recommend reading that article as it goes into much more detail. I also recommend taking a look at the earlier topic that covers companion objects.
The normal way to use a trait is to mix it in to an object. However there can be a problem mixing two traits containing methods with equal signatures. If the two traits are not designed to work together then you will get a compile error. Otherwise one method will override the other. Either way you cannot access both methods. There is an additional way to access the functionality of a trait. You can create an object (not instance) that extends the trait and import the methods when you need them.
If the trait is stateless then the object can be shared if not then make sure that sharing the object is carefully handled.
Examples:
The normal way to use a trait is to mix it in to an object. However there can be a problem mixing two traits containing methods with equal signatures. If the two traits are not designed to work together then you will get a compile error. Otherwise one method will override the other. Either way you cannot access both methods. There is an additional way to access the functionality of a trait. You can create an object (not instance) that extends the trait and import the methods when you need them.
If the trait is stateless then the object can be shared if not then make sure that sharing the object is carefully handled.
Examples:
- scala> trait T1 {
- | def talk = "hi"
- | }
- defined trait T1
- scala> trait T2 {
- | def talk = "hello"
- | }
- defined trait T2
- // Cannot extend C with T1 and T2 because they are not designed to work together
- scala> class C extends T1 with T2
:6: error: error overriding method talk in trait T1 of type => java.lang.String; - method talk in trait T2 of type => java.lang.String needs override modifier
- class C extends T1 with T2
- ^
- scala> class C extends T1
- defined class C
- // objects can have state so becareful how you share them
- scala> object Obj1 extends T1
- defined module Obj1
- scala> object Obj2 extends T2
- defined module Obj2
- // You can give aliases to the imported methods and use them in the class
- scala> class C {
- | import Obj1.{talk => hi}
- | import Obj2.{talk => hello}
- | def sayHi = hi
- | def sayHello = hello
- | }
- defined class C
- scala> val c = new C
- c: C = C@54d8fd1a
- scala> c.sayHi
- res0: java.lang.String = hi
- scala> c.sayHello
- res1: java.lang.String = hello
- scala> class C extends T1 {
- | import Obj2.{talk => hello}
- | def helloTalk = hello
- | }
- defined class C
- scala> val c2 = new C
- c2: C = C@2ee634bf
- scala> c2.talk
- res2: java.lang.String = hi
- scala> c2.helloTalk
- res5: java.lang.String = hello
Labels:
import,
import alias,
object,
Scala,
traits
Subscribe to:
Comments (Atom)