implicit

Implicit conversion without assignment?

可紊 提交于 2019-12-12 12:19:24
问题 Preserved question - see Edit at the bottom I'm working on a small functional library, basically to provide some readability by hiding basic cyclomatic complexities. The provider is called Select<T> (with a helper factory called Select ), and usage is similar to public Guid? GetPropertyId(...) { return Select .Either(TryToGetTheId(...)) .Or(TrySomethingElseToGetTheId(...)) .Or(IGuessWeCanTryThisTooIfWeReallyHaveTo(...)) //etc. ; } and the library will take care of the short circuiting, etc. I

Scala: How to invoke method with type parameter and manifest without knowing the type at compile time?

夙愿已清 提交于 2019-12-12 12:06:14
问题 I have a function with the following signature: myFunc[T <: AnyRef](arg: T)(implicit m: Manifest[T]) = ??? How can I invoke this function if I do not know the exact type of the argument at the compile time? For example: val obj: AnyRef = new Foo() // At compile time obj is defined as AnyRef, val objClass = obj.getClass // At runtime I can figure out that it is actually Foo // Now I would need to call `myFunc[Foo](obj.asInstanceOf[Foo])`, // but how would I do it without putting [Foo] in the

Why implicit parameters cannot be passed explicitly when they undergo an implicit conversion?

蹲街弑〆低调 提交于 2019-12-12 11:34:08
问题 Why iWantInt(a) does not compile while iWantInt(b) and - what is more surprising - iWantInt does ? How can I understand this behaviour ? Why can I not pass explicitly a to iWantInt when I can pass it implicitly ? How is iWantA different from iWantInt ? Why does it not work the same way ? Is this behavior documented/explained somewhere ? If Scala would accept iWantInt(a) what kind of problems would that cause ? Why is it forbidden ? class A class B object Run extends App { implicit val a = new

Applying implicit generic parameters to a list via ClassTag

微笑、不失礼 提交于 2019-12-12 05:49:59
问题 I would like to apply a function to all objects in a list, where all objects in the list inherit from a common class. In this function, I would like to use an implicit class to ensure that the correct operation is applied based on the object's type. For example, I want to ensure that all Employee objects in a list are converted using the employeeConverter below. Calling convert with the Employee directly works just fine, but applying convert to a list of Employee objects is a compiler error.

low priority and high priority implicits in scala

强颜欢笑 提交于 2019-12-12 04:18:48
问题 In following code from Scala's puzzlers, it seems that there is no conflict in implicits because TestAlarmHandler is more specific. I didn't understand the explanation though. Why is TestAlarmHandler is more specific than DefaultAlarmHandler ? object Scanner { trait Console { def display(item: String) } trait AlarmHandler extends (() => Unit) def scanItem(item: String)(implicit c: Console) { c.display(item) } def hitAlarmButton()(implicit ah: AlarmHandler) { ah() } } class OperatingMode {

Implicit typecasting not working if passing integer value as argument in java

。_饼干妹妹 提交于 2019-12-12 02:15:34
问题 In the following code , implicit typecasting for an integer value 9 take place and assigned to variable of byte datatype which is of size 8 bits. class Demo1 { public static void main(String args[]) { byte b=9; System.out.println(b); } } The code happily compiled and executed . but when i wrote the following code it gave me compilation error class Demo2 { void func1(byte b) { System.out.println(b); } public static void main(String[] args) { Demo2 d1=new Demo2(); d1.func1(9); } } please

Implicit expression language object “component” not working in jsf 2.2.6

可紊 提交于 2019-12-11 22:22:12
问题 I'm migrating a jsf 2.0 application to jsf 2.2.6. There is a extensive use of implicit EL object component as styleClass="#{component.valid?'':'err'}". In jsf 2.2.6 (jsf-impl-2.2.6-jbossorg-4.jar) valid is not recognized, throwing "ServletException: The class 'javax.faces.component.html.xxx' does not have the property 'valid". Is this functionality deprecated in jsf 2.x.x? Can be related to JBoss EL? 回答1: It seems that you trying the component.valid on element that does not support it at all,

Defining instances of a third-party typeclass, implicit not found but explicit works fine

◇◆丶佛笑我妖孽 提交于 2019-12-11 17:24:35
问题 I'm working with Slick's GetResult typeclass and wanted to use Shapeless to derive instances of GetResult[Option[(A, B, C...)]] What I want: Given an implicit GetResult[Option[A]], GetResult[Option[B]], ... , implicitly generate a GetResult[Option[(A, B, ...)]] What I tried trait CanGetOption[T] { def getOption: GetResult[Option[T]] } object CanGetOption { // convenience implicit resolver def apply[T](implicit canGetOption: CanGetOption[T]): CanGetOption[T] = canGetOption // base case: HNil

Specifying Legal and/or Illegal Implicits at Compile-time?

ⅰ亾dé卋堺 提交于 2019-12-11 16:29:23
问题 Let's say that I have two particular object 's from which I retrieve imports. Assume that both objects have multiple, useful imports that I want to use. I'm only including 1 for simplicity of this example: scala> object Implicits1 { implicit def good: String => Int = _ => 42 } defined object Implicits1 scala> object Implicits2 { implicit def bad: String => Int = _ => 666 } defined object Implicits2 Then, given foo : scala> def foo(x: Int): Int = x foo: (x: Int)Int I perform my wildcard

How to return a property of the class instead of the class itself?

两盒软妹~` 提交于 2019-12-11 14:51:53
问题 I'm currently converting and casting from Class to Class2 using the implicit operator. But what I want to do is, that, whenever I refer to foo (Class<Class2>) , I'd like for Goo(Class) to be returned so that I can access the public Properties of it directly, without having to cast it first into a new variable. In other words, I want that when I access Class<Class> , I'd like Goo to be returned. I'm aware I might have not been able to explain properly so feel free to ask in the comment section