using scala-meta, how to infer types - scalameta

I would like to be able to detect the full type name of a type T when it is referred within some other compilation unit A. So say A is
package p
import com.p1.A
import com.p2._
class X {
def method1:A // detect it as com.p1.A, which I can do because of the import
def method2:B // detect it as com.p2.B, which I can't at the moment
}
I get the feeling that scalameta probably is not the best tool for type detection, is there a safe/easy way to detect all types?

Related

Is it possible to automatically load an implicit def if included as a dependency (no importing)

I'm working on a commons library that includes a config library (https://github.com/kxbmap/configs).
This config library uses "kebab-case" when parsing configuration files by default and it can be overridden by an implicit def in scope.
However, I don't want to force that on the users of my commons library when they get access to the config library transitively.
So without me forcing users to import this implicit, like:
import CommonsConfig._
can I somehow override the naming strategy via an implicit that gets into scope by only including my commons library on the classpath. I'm guessing no but I just have to ask :)
So if not, is someone aware of another approach?
kxbmap/configs isn't that well documented to explain this.
Thanks!
Implicits work in compile time, so they cannot get magically present if something is included and then disappear if it isn't.
The closest thing would be something like:
main library
package my.library
// classes, traits, objects but no package object
extension
package my
package object library {
// implicits
}
user's code
import my.library._
however that would only work if there were no package object in main library, only one extension library could pull off this trick at once (Scala doesn't like more than one package object) and user would have to import everything available with a package, always.
In theory you could create a wrapper around all you deps, with your own configs:
final case class MyLibConfig(configsCfg: DerivationConfig)
object MyLibConfig {
implicit val default: MyLibConfig = ...
}
and then derive using this wrapper
def parseThings(args...)(implicit myLibConfig: MyLibConfig) = {
implicit val config: DerivationConfig = myLibConfig.config
// derivation
}
but in practice it would not work (parseThings would have to already know the target type or would need to have the already derived implicits passed). Unless you are up to writing your own derivation methods... avoid it.
Some way of making user just import all relevant stuff is the most maintainable strategy. E.g. you could pull off the same thing authors did and add type aliases for all types that you use, do the same for companion objects and finally put some implicits there:
package my
package object library {
type MyType = some.library.Type
val MyType = some.library.Type
implicit val derivationConfig: DerivationConfig = ...
}

Is there a way to access constants of a (java) class by an alias in scala?

I would like to make a short alias for a java class.
Is there a way to import (or make a type alias to) a java class (e.g. HttpServletResponse), and access its constant values (e.g. HttpServletResponse) using the alias?
Type alias works fine, but I cannot find a way to access the constants of the class.
EDIT:
I'm sorry I asked a wrong question. I knew importing with a short name works.
What I like to do is avoid writing import ...{HttpServletResponse => Response} every file in which MyHttpServlet is mixed-in.
Type aliases in MyHttpServlet makes it possible without importing, but accessing constants is still the issue. (or maybe it's impossible?)
trait MyHttpServlet extends HttpServlet {
import javax.servlet.http.HttpServletResponse
// works
type Response = HttpServletResponse
// compile error: object creation impossible, since it has 36 unimplemented members.
//object Response extends HttpServletResponse
def notAllowed(response: Response): Unit = {
// works
response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED)
// I would like to do something like this
//response.setStatus(Response.SC_METHOD_NOT_ALLOWED)
}
}
Just rename the import.
import javax.servlet.http.{HttpServletResponse => Response}
response.setStatus(Response.SC_METHOD_NOT_ALLOWED)
Type aliases don't allow static member access because one can't call members on a type in scala, it is only possible on values.

How to (properly) enrich the standard library?

I would like to define an implicit conversion from Iterator[T] to a class that I have defined: ProactiveIterator[A].
The question isn't really how to do it but how to do it properly, i.e. where to place the method, so that it is as transparent and unobtrusive as possible. Ideally it should be as the implicit conversion from String to StringOps in scala.Predef If the conversion was from a class in the library to some other class, then it could be defined inside that class, but AFAIK that's not possible here.
So far I have considered to add an object containing these conversions, similarly to JavaConversions, but better options may be possible.
You don't really have much of a choice. All implicits must be contained within some sort of object, and imported with a wildcard import (you could import them individually, but I doubt you want that).
So you'll have some sort of implicits object:
package foo.bar
object Implicits {
implicit class ProactiveIterator[A](i: Iterator[A]) {
...
}
}
Then you must explicitly import it wherever you use it:
import foo.bar.Implicits._
In my opinion, this is a good thing. Someone reading the code might not understand where your pimped methods are coming from, so the explicit import is very helpful.
You can similarly place your implicits within a package object. You would have to import them the same way into other namespaces, but they would be available to classes within the same package.
For example, using the following, anything within foo.bar will have this implicit class available:
package foo
package object bar {
implicit class ProactiveIterator[A](i: Iterator[A]) {
...
}
}
Elsewhere you would import foo.bar._ (which may or may not be as clean, depending on what's in bar).

Import package as another

Let's say I have a Scala project with a bunch of case classes under package com.example.a.b.c. I want to import all these classes into a package com.example.c (which contains a few more non-conflicting case classes) so that anywhere else in my project, I only need to import com.example.c._ to use every case class both from com.example.c and com.example.a.b.c.
How can I do that?
There is discussion of adding an export mechanism which would do what you want, but it's not clear whether that will happen.
In any case, for now the only way is to
Define the type of every class
Set a val equal to every object
So for example,
package bar
case class Foo(i: Int) {}
would need to be mimicked in another package with
package object baz {
type Foo = bar.Foo
val Foo = bar.Foo
}
When faced with this, people usually just settle for an extra import or two.
The import statement in scala just tells the compiler where to find other classes like in java, not like the #include directive in C/C++ where the compiler physically copies the entire header file. If you want to use the case classes from com.example.a.b.c, you should import them from their own package as this is the conventional way.

Use function without explicit import in Scala

I like Scala's sys.error function - but I want to distinguish two cases: internal errors (e.g. database problem) and user errors (invalid input).
I tried extending Scala - but it doesn't seem to work:
package scala
class UserException(msg: String) extends RuntimeException(msg)
package object err {
def internal(message: String): Nothing =
sys.error(message)
def usr(message: String): Nothing =
throw new UserException(message)
}
How should I define err.usr() to be able to use it without an explicit import?
You can't, only scala.Predef is imported by default and it's not user extensible in any useful way.
You could put these definitions on the package object of your package hierarchy. Then, everything on that package will see them without import.