How to import class from other directory in scala? - scala

I need to make use of the UIController class in the PriceAlert Class, but it is not possible, even though they are in the same package: co.uproot.abandon.
The thing is I can use the PriceAlert class in the UIController class but not the other way around.
files layout

Look dependence of modules in your root build.sbt. It's possible that gui depends on base so you can use PriceAlert from base inside UIController from gui.
If you need to use classes from different modules in each other then this means that you have cyclic dependence of modules
sbt: cyclic dependence between modules?

Related

Best way to solve imports of file in same package but in different directory with Gatling

I want to clean my code structure and put class/object files in another directories in my gatling project.
If i put all simulation class and utils class in the same directory and same package i do not need an import statement and everything works fine.
Let's say my structure is as follow :
/user-files
----/simulations
--------MySimulation.scala
----/utils
--------Router.scala
I have tried several import or naming configuration to be able to use Router in my Simulation.
Follow package naming as directories structure
Put simulations and utils class in the same package
I have also tried different style of import
//using package
import packagename.Router
//another try
import packagename.Router._
//without package name
import Router._
My attempt to search a solution on scala docs or stack overflow didn't helped me.
This is the error given after executing gatling.bat
not found: value Router
You can't do that this way: there's one single source folder, which is by default /user-files/simulations.
If you want to use folders/packages (which is a good thing), you can have a structure such as:
/user-files
----/simulations
--------MySimulation.scala
--------/utils
------------Router.scala
Then, in Scala, packages and folder hierarchy are not related, BUT it's a good practice to use the same convention as in Java.
So, you would have:
package utils
object Router
then in MySimulation:
import utils.Router

Get IntelliJ or SBT to give two names to a package

Please tell me a more describing title :P
I have an SBT project in IntelliJ and its src directory constructed like:
src/main/scala/package1/AAA.scala
src/main/scala/package1/BBB.scala
src/main/scala/package2/CCC.scala
src/main/scala/...
So now these classes are imported like:
import package1.AAA._
But actually I want to import these classes like:
import org.myname.package1.AAA._
At the moment I have two ways; one is to put org.myname to all the class files' package attributes like:
package org.myname.package1
class AAA {...
which works, but doing this manually is time consuming, and makes things complicated.
Another one is to reformat the directories like:
src/main/scala/org/myname/package1/AAA.scala
src/main/scala/org/myname/package1/BBB.scala
src/main/scala/org/myname/package2/CCC.scala
src/main/scala/org/myname/...
which works too, but I'd not like to make the looking of the structure like this.
Is there an automatic way to include all classes into org.myname without changing the structure, with the SBT or IntelliJ features?
Create a new package named org.myname.
Move the package1 inside org.myname
Move package2 inside org.myname
IntellIJ IDEA will fix everything for you.

How to organize classes inside scala project?

In Java I used to put classes inside packages with long informative names by template domain.company.project.module.ets and used to names like:
ch.qos.logback.classic.net
org.hibernate.cache
com.google.common.collect
But in the sources of akka and sbt projects there are a lot of classes inside packages with simple names:
akka.actor
sbt
And that's not all. There are many classes inside classes. And by classes I also mean objects and traits in all possible combinations. There are object inside traits, traits inside classes, etc. But the level of nesting is always 1 (or should I say 2?).
Obviously, there is another approach to class organization. I wonder if you can give me a tip of how to name packages in scala and when to put classes inside each other?
Hi I will reccomend you to read chater seven of scala for the imatient is free available here: http://typesafe.com/resources/free-books.
As a summary the keypoints, as sayed in the book:
- Packages nest just like inner classes.
- Package paths are not absolute.
- A chain x.y.z in a package clause leaves the intermediate packages x and x.y invisible.
- Package statements without braces at the top of the file extend to the entire file.
- A package object can hold functions and variables.
- Import statements can import packages, classes, and objects.
- Import statements can be anywhere.
- Import statements can rename and hide members.
- java.lang, scala, and Predef are always imported.

SystemVerilog: Using packages with classes and virtual interfaces

I'm a relative newbie to SystemVerilog.
I have a package with class A defined in it. This class uses a virtual interface, since
it's a driver (BFM) in a testbench. I'm using a package so I can use the same BFM in
other designs.
In my testbench, I import the A class and pass to it an instance of the virtual interface.
However, when a task in the class tries to assign a value to a signal in the interface, I'm getting a compilation error.
What am I doing wrong?
How can one package a BFM with a virtual interface?
Thanks,
Ran
SystemVerilog packages cannot include interfaces within the actual package. So your interface needs to be compiled along with you package source. The classes you define will reside in the package while the interface definition resides in the global scope where modules live.
Classes within packages can make references to virtual interfaces, but you need to make sure the interface is compiled and visible, apart from the package source.
Strictly according to the spec, I don't think this is possible since it adds an implicit external dependency:
Items within packages are generally type definitions, tasks, and
functions. Items within packages shall not have hierarchical
references to identifiers except those created within the package or
made visible by import of another package. A package shall not refer
to items defined in the compilation unit scope.
It doesn't say anything about the design element namespace, which is where interface declarations live, but accessing any member of an interface requires a hierarchical reference.
You should consider packages to be completely self-contained, other than pre-processor directives and import.
Generally the class declaration not present before its usage is resolved with the help of systemverilog typedef definition. For example "Class A uses Class B" and "Class B uses class A" then typedef is used to resolve the stalemate.
Now when you bring in the package with the above scenario then one needs to ensure both Class A and Class B have to be in same package. If they are not then the compile wont go through.
The reason being the SystemVerilog parser will need the definition of the classes indicated with the typedef at the end of the package parsing. This fails.
This issue needs to watched out that "typedef does not apply across package".

Where to store UNIVERSAL methods in Perl

So I understand that the special UNIVERSAL class is to be considered the base class from which all other objects are created. So if a specific method is not found after Perl traverses the inheritance hierarchy, it will look in the UNIVERSAL class to see if the method can be found there. However, when you create a distribution, no UNIVERSAL.pm package is created. Also, the UNIVERSAL methods 'DOES' and 'can' already exist without a UNIVERSAL.pm... As such, I am not sure if I should be writing UNIVERSAL methods into random packages like so:
sub UNIVERSAL::nicemethod{
launch_teh_missles();
}
Or should I be creating a separate UNIVERSAL package and .pm file? What is considered best practice?
You can add new methods to UNIVERSAL the same way as to any other package:
package
UNIVERSAL; # Line break to fool CPAN indexer
sub nice_method {
...
}