Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python like package name aliasing in Scala

Tags:

scala

I know that in Scala you can alias things inside package like that: import some.package.{someObject => someAlias}

Is there a way of creating alias for package name, not for classes/objects inside it ?

For example in Python you can do: import package as alias

like image 932
Michal Fudala Avatar asked Dec 25 '22 23:12

Michal Fudala


2 Answers

You can alias a package name the same way you alias an object.

import scala.collection.{mutable => m}

val buffer = m.ListBuffer(1, 2, 3, 4)

buffer: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)

Fun fact: You can also alias object methods this way.

import scala.collection.mutable.ListBuffer.{apply => makeBuffer}

scala> makeBuffer(1, 2, 3, 4)
res5: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)
like image 51
Michael Zajac Avatar answered Jan 05 '23 14:01

Michael Zajac


import org.joda.time.{DateTime => joda}

Now you can use joda as synonym for DateTime

joda.parse("2014-12-23")
like image 34
Diego Martinoia Avatar answered Jan 05 '23 14:01

Diego Martinoia