Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access implicit "implicit" i.e. def a[A :B] or def a[A <% B]?

for example I need to access manifest in function def a[A:ClassManifest] to get erasure class. I can use Predef.implicitly function but in that case my code will be as long as if I use full form def a[A](implicit b:ClassManifest[A]). So is there are convenient generated names for those implicit arguments?

like image 578
yura Avatar asked Feb 27 '12 18:02

yura


People also ask

What is implicit conversion in SQL Server?

Implicit conversions are not visible to the user. SQL Server automatically converts the data from one data type to another. For example, when a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds.

What is an implicit conversion?

An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.

What is implicit keyword in C#?

According to MSDN, an implicit keyword is used to declare an implicit user-defined type conversion operator. In other words, this gives the power to your C# class, which can accepts any reasonably convertible data type without type casting.

What is the use of Implicits in Scala?

The implicit system in Scala allows the compiler to adjust code using a well-defined lookup mechanism. A programmer in Scala can leave out information that the compiler will attempt to infer at compile time. The Scala compiler can infer one of two situations: A method call or constructor with a missing parameter.


1 Answers

There are three predefined methods in Predef that will do it for Manifests, ClassManifests and OptManifests: manifest[T], classManifest[T] and optManifest[T], respectively. You can write your own such “implicit getters” for other type classes according to the same pattern. Here is for instance manifest[T]:

def manifest[T](implicit m: Manifest[T]) = m

So here's how you could write your own:

trait UsefulTypeclass[A] {
  def info = 42 // sample method
}

// the “implicit getter”
def usefulTypeclass[A](implicit tc: UsefulTypeclass[A]) = tc

// a method that uses the implicit getter
def foo[A: UsefulTypeclass] =
  usefulTypeclass[A].info
like image 79
Jean-Philippe Pellet Avatar answered Oct 11 '22 14:10

Jean-Philippe Pellet