How can I compile a Scala and LWJGL3 application? - scala

I'm having a lot of trouble compiling a Scala program with LWJGL3. I suspect that I either don't understand Scala's import statement correctly, or I don't understand Scala's classpath correctly. I've placed the LWJGL3 Nightly download in an org folder at my project root. This is the error scalac gives me:
Minimum.scala:1: error: object opengl is not a member of package org.lwjgl
import org.lwjgl.opengl._
^
Minimum.scala:3: error: object glfw is not a member of package org.lwjgl
import org.lwjgl.glfw.GLFW._
^
Minimum.scala:4: error: object opengl is not a member of package org.lwjgl
import org.lwjgl.opengl.GL11._
^
Minimum.scala:5: error: object system is not a member of package org.lwjgl
import org.lwjgl.system.MemoryUtil._
^
(Plus lots of error: not found: value _______ errors because it couldn't find the various functions.)
Here's my code (in Minimum.scala at the project root):
import org.lwjgl.opengl._
import org.lwjgl.glfw.GLFW._
import org.lwjgl.opengl.GL11._
import org.lwjgl.system.MemoryUtil._
object Minimum {
def main(args: Array[String]): Unit = {
glfwInit()
long windowHandle = glfwCreateWindow(500, 500, "Hello, World!", NULL, NULL)
glfwMakeContextCurrent(windowHandle)
GL.createCapabilities()
glClearColor(0.0f, 1.0f, 0.0f, 0.0f)
while (!glfwWindowShouldClose(windowHandle)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glfwSwapBuffers(windowHandle)
glfwPollEvents()
}
glfwDestroyWindow(windowHandle)
}
}
I've also tried these import statements instead, which were suggested here:
import org.lwjgl._, glfw._, opengl._
import Callbacks._, GLFW._, GL11._
import org.lwjgl.system.MemoryUtil._
Finally, I'm compiling with this command:
scalac -classpath ".;org/lwjgl/*;org/lwjgl-egl/*;org/lwjgl-glfw/*;org/lwjgl-jawt/*;org/lwjgl-jemalloc/*;org/lwjgl-lmdb/*;org/lwjgl-nanovg/*;org/lwjgl-nfd/*;org/lwjgl-nuklear/*;org/lwjgl-openal/*;org/lwjgl-opencl/*;org/lwjgl-opengl/*;org/lwjgl-opengles/*;org/lwjgl-ovr/*;org/lwjgl-par/*;org/lwjgl-sse/*;org/lwjgl-stb/*;org/lwjgl-tinyfd/*;org/lwjgl-vulkan/*;org/lwjgl-xxhash/*" Minimum.scala
Which I've tried numerous variations on including omitting the *, adding *.jar, moving the lwjgl libraries to another folder, giving just the org/ directory rather than the complete list, and replacing the ; with :. This is essentially the command I used to compile a nearly identical Java application, which worked perfectly.
Does anyone know how I can get my program to compile?

Related

Adding unit test to a legacy Scala project in IntelliJ

I have a legacy Scala project to work with using maven as build tool.
Initially there is core code only under folder structure src/main/scala/ with package x.y.z and a file Main.scala with code as:
object Main {
def cube(x: Int) = {
x * x * x
}
}
There are no tests for the code. So I manually added a folder structure as test/scala under src.
Then I copied the package as for core code, ie x.y.z and added MainTest.scala with test code as:
class MainTest extends FunSuite {
test("Main.cube") {
assert(Main.cube(3) === 27)
}
}
Running test gives error as:
Error:(8, 12) not found: value Main
assert(Main.cube(3) === 27)
Why do I get this error?
For below project structure MainTest.scala and Main.scala are in the same package x.y.z therefore no package import is required, However MainTempTest.scala and Main.scala are in different package therefore an explicit import has to be done import x.y.z.Main
src
-main
--scala
---x.y.z
----Main.scala
-test
--scala
---MainTempTest.scala
---x.y.z
----MainTest.scala

Is it possible to test chisel Reg() in console?

To test Chisel code, I launch a console sbt then scala in the directory of my project where is the file build.sbt. I can import chisel3 library :
$ cd myproject
$ sbt
sbt:myproject> console
scala> import chisel3._
import chisel3._
Then I can test some chisel code for data type for example :
scala> val plop = "b01010101".U(20.W)
plop: chisel3.UInt = UInt<20>(85)
But I can test Reg() or other Module() elements :
scala> val plopReg = RegInit(23.U(24.W))
java.lang.IllegalArgumentException: requirement failed: must be inside Builder context
at scala.Predef$.require(Predef.scala:281)
at chisel3.internal.Builder$.dynamicContext(Builder.scala:232)
at chisel3.internal.Builder$.currentClock(Builder.scala:308)
at chisel3.internal.Builder$.forcedClock(Builder.scala:318)
at chisel3.RegInit$.apply(Reg.scala:155)
at chisel3.RegInit$.apply(Reg.scala:173)
... 36 elided
Is there a tips to test these chisel element in console ? Or is it mandatory to write a file code source ?
What's going on here is that UInt is a Chisel type while Reg is a hardware type.
You can play with hardware types only inside a module. I often do something like the following to play with them on the console:
import chisel3._
import chisel3.stage.{ChiselStage, ChiselGeneratorAnnotation}
import chisel3.util.Cat
import firrtl.EmittedCircuitAnnotation
class Foo extends MultiIOModule {
val in = IO(Input(Bool()))
val out = IO(Output(Bool()))
val tmp = RegNext(~in)
out := tmp
}
val args = Array(
"-X", "verilog",
"-E", "high",
"-E", "middle",
"-E", "low",
"-E", "verilog")
(new ChiselStage).execute(args, Seq(ChiselGeneratorAnnotation(() => new Foo)))
You can then look at the various outputs inside your chisel3 top-level directory.
More Information
What's going on, specifically, is that UInt (and things like it) are factory methods that generate classes (technically UInt is really an object that extends UIntFactory). When you do UInt(4.W), that's constructing a new UInt. You should be able to construct new classes anywhere you want which is why this works on the console.
However, when you do Reg(UInt(4.W)) that's interacting with global mutable state used during the elaboration process to associate a register with a specific module. This global mutable state is stored inside the Builder. The error that you get is coming from the Builder where you've tried to use its methods without first being inside a module.

imported object is permanently hidden warning by definition of object in Scala Spec

I am trying to write a spec and then I get a compilation error:
imported `Writer' is permanently hidden by definition of object ZipWriter in package util
[error] import com.thing.util.Writer
I have this object:
object Writer
and my spec does this:
package com.thing.util
import com.thing.util._
import java.io.File
class WriterSpec {
behavior of "buildFilePath"
it must "append the compressionExtension to the file" in {
val files: Array[File] = Array(new File("/some/filepath.someData"))
val compressed = Writer.buildFilePath(files, "gz")
compressed must be("/some/filepath.someData.gz")
}
}
that works but if i try to specifc the Writer in the import statement directly, all fails. What gives?
package com.thing.util
import com.gemini.util.Writer
import java.io.File
class WriterSpec {
behavior of "buildFilePath"
it must "append the compressionExtension to the file" in {
val files: Array[File] = Array(new File("/some/filepath.someData"))
val compressed = Writer.buildFilePath(files, "gz")
compressed must be("/some/filepath.someData.gz")
}
}
package com.thing.util
import com.thing.util._
It never makes sense to import anything (or everything) from your own package in the beginning of the file, all of it is already visible.
i try to specifc the Writer in the import statement directly, all fails
The message says that because there is already com.thing.util.Writer, importing com.gemini.util.Writer doesn't do anything, Writer in your code still means com.thing.util.Writer (and if you need to use com.gemini.util.Writer, you'll need to write it out or use renaming imports). Normally it should be a warning, you must be passing the compiler option which turns warnings into errors.

Explain why import scala.Predef.String fails build

I found very strange issue with Scala import.
I wrote sample class:
package test_scala_predef
object App extends App {
classOf[T]
println( "Hello World!" )
}
class T {
}
This class compiles without any errors.
But, if I add
import scala.Predef.String
then I get compilation errors:
[INFO] Compiling 1 source files to /home/uthark/src/_/test_scala_predef/target/classes at 1374028063588
[ERROR] /home/uthark/src/_/test_scala_predef/src/main/scala/test_scala_predef/App.scala:10: error: not found: value classOf
[INFO] classOf[T]
[INFO] ^
[ERROR] /home/uthark/src/_/test_scala_predef/src/main/scala/test_scala_predef/App.scala:11: error: not found: value println
[INFO] println( "Hello World!" )
[INFO] ^
[ERROR] two errors found
I have an idea, that after I add specific import from scala.Predef, then implict import of scala.Predef._ is not added. But I can't find anything about it in the Scala documentation. Can anyone point me to the relevant section in documentation?
I checked Scala Language Specification (PDF), section 12.5 covering scala.Predef but also didn't find any related.
I use latest stable scala version available (2.10.2 at the moment)
I found the answer in the sources.
https://github.com/scala/scala/blob/master/src/compiler/scala/tools/nsc/typechecker/Contexts.scala:
/** List of symbols to import from in a root context. Typically that
* is `java.lang`, `scala`, and [[scala.Predef]], in that order. Exceptions:
*
* - if option `-Yno-imports` is given, nothing is imported
* - if the unit is java defined, only `java.lang` is imported
* - if option `-Yno-predef` is given, if the unit body has an import of Predef
* among its leading imports, or if the tree is [[scala.Predef]], `Predef` is not imported.
*/
protected def rootImports(unit: CompilationUnit): List[Symbol] = {
assert(definitions.isDefinitionsInitialized, "definitions uninitialized")
if (settings.noimports) Nil
else if (unit.isJava) RootImports.javaList
else if (settings.nopredef || treeInfo.noPredefImportForUnit(unit.body)) {
debuglog("Omitted import of Predef._ for " + unit)
RootImports.javaAndScalaList
}
else RootImports.completeList
}
This answers my question.
The key sentence is this:
Exceptions: ... if the unit body has an import of Predef among its leading imports
Also, in chat on #scala irc it was suggested to create bug in Scala issue tracking system, so I did it. https://issues.scala-lang.org/browse/SI-7672

Program works when run with scala, get compile errors when try to compile it with scalac

I am testing the code below, does a basic database query. It works fine when I run it from the CLI using "scala dbtest.scala", but gives me compile errors when I try to compile it with scalac :
[sean#ibmp2 pybackup]$ scalac dbtest.scala
dbtest.scala:5: error: expected class or object definition
val conn_str = "jdbc:mysql://localhost:3306/svn?user=svn&password=svn"
^
dbtest.scala:8: error: expected class or object definition
classOf[com.mysql.jdbc.Driver]
^
dbtest.scala:11: error: expected class or object definition
val conn = DriverManager.getConnection(conn_str)
^
dbtest.scala:12: error: expected class or object definition
try {
^
four errors found
import java.sql.{Connection, DriverManager, ResultSet};
import java.util.Date
// Change to Your Database Config
val conn_str = "jdbc:mysql://localhost:3306/svn?user=xx&password=xx"
// Load the driver
classOf[com.mysql.jdbc.Driver]
// Setup the connection
val conn = DriverManager.getConnection(conn_str)
try {
// Configure to be Read Only
val statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
// Execute Query
val rs = statement.executeQuery("SELECT * FROM backup")
// Iterate Over ResultSet
var svnFiles = Set[String]()
while (rs.next) {
val repos = rs.getString("repos")
val lm = rs.getDate("lastModified")
val lb = rs.getDate("lastBackedup")
if (lm.getTime() > lb.getTime()) {
println(repos + " needs backing up")
svnFiles += repos
}
else {
println(repos + " doesn't need backing up")
}
}
println(svnFiles)
}
finally {
conn.close
}
You need either a class, object, or trait at the top level to make it a legal source to compile. scala interpreter expects definitions and expressions, whereas scalac expects something that can turn into Java .class files.
//imports here
object DbTest {
def main(args: Array[String]) {
// your code here
}
}
Create a file called HelloWorld.scala, and enter the following:
object HelloWorld {
def main(args: Array[String]){
println("Hello World")
}
}
To compile the example, we use scalac, the Scala compiler. scalac works like most compilers: it takes a source file as argument, maybe some options, and produces one or several object files. The object files it produces are standard Java class files.
From the command line, run:
scalac HelloWorld.scala
This will generate a few class files in the current directory. One of them will be called HelloWorld.class, and contains a class which can be directly executed using the scala command.
Once compiled, a Scala program can be run using the scala command. Its usage is very similar to the java command used to run Java programs, and accepts the same options. The above example can be executed using the following command, which produces the expected output:
Now run:
scala HelloWorld.scala
Now "Hello World", will be printed to the console.
After researching this functionality, I found an article, which explains this in detail, and posted that information here on SO to help programmers understand this aspect of Scala development.
Source: http://docs.scala-lang.org/tutorials/scala-for-java-programmers.html