implicit

Confused about Scala method calling conventions, specifically the sum function on Seq

情到浓时终转凉″ 提交于 2019-12-04 16:26:52
问题 I was playing around with the new Scala IDE (Eclipse 3.6.2 + Scala IDE 2.0.0 [Scala 2.9.0]) and I tried to do something simple like this: (1 to 10).sum That works fine, but I've been doing a lot of Groovy also recently and I automatically wrote: (1 to 10).sum() This second version gives me a compiler error in the IDE with the following message: not enough arguments for method sum: (implicit num: Numeric[B])B. Unspecified value parameter num. I see on the Scala API that there are two versions

Shapeless deconstruct tuple in type parameter declaration

时光怂恿深爱的人放手 提交于 2019-12-04 14:14:28
I am using a RightFolder that returns a Tuple2 and would like to return the _1 part. The first version rightFoldUntupled1 works fine but uses an additional IsComposite typeclass. In the second version rightFoldUntupled2 I try to achieve the same without IsComposite by destructuring P as a Product2[_, Values] . That doesn't work any more - the compiler is happy to witness that P is a Product2[_,_] , but as soon as I use a named type parameter for _1 it doesn't compile any more. Any idea why? The full source (with sbt ready to ~run with implicit debug output) is here: https://github.com

Implicit conversion is not applied if a method has type parameter

大城市里の小女人 提交于 2019-12-04 13:45:46
问题 The question is based on the discussion here. This is the setup: implicit def CToC2(obj: C1): C2 = { new C2() } class C1() { def f[U](f: (Int, Int) => U): U = f(1, 1) } class C2() { def f[U](f: ((Int, Int)) => U): U = f(2, 2) } I'd expect that trying to call the function with a signature that exists in C2 , scala would use the implicit conversion to satisfy the call: val c1 = new C1() val ff: ((Int, Int)) => Unit = t => println(t._1 + t._2) But this fails: scala> c1.f(ff) Error:(16, 7) type

Scala implicitly vs implicit arguments

我的梦境 提交于 2019-12-04 10:21:13
问题 I am new to Scala, and when I look at different projects, I see two styles for dealing with implicit arguments scala]]>def sum[A](xs:List[A])(implicit m:Monoid[A]): A = xs.foldLeft(m.mzero)(m.mappend) sum:[A](xs:List[A])(implicit m:Monoid[A])A and scala]]>def sum[A:Monoid](xs:List[A]): A ={ val m = implicitly[Monoid[A]] xs.foldLeft(m.mzero)(m.mappend) } sum:[A](xs:List[A])(implicit evidence$1:Monoid[A])A Based off the type of both functions, they match. Is there a difference between the two?

Generate functions at macros expansion time

陌路散爱 提交于 2019-12-04 09:43:28
I would like to generate functions for a class accepting 1 type parameter case class C[T] (t: T) depending on the T type parameter. The functions I would like to generate are derived by the functions available on T . What I would like exactly, is to make all the functions available for T , also available for C . As an example for C[Int] , I would like to be able to call on C any function available on Int and dispatch the function call to the Int contained in C . val c1 = new C(1) assert(c1 + 1 == 2) How can I achieve this by using Scala 2 or dotty macros? Or, can this be achieved in another

ASP.NET: explicit vs implicit localization?

坚强是说给别人听的谎言 提交于 2019-12-04 06:17:33
To my mind the advantage of implicit localization over explicit localization is that if you have more than one property to localize for a given control, it's a more economical syntax. In the case where you just need to localize some text I use the asp:Localize control which only has a single property (Text) that renders to the UI. Is there a reason to use one over the other? Any style preference? Are there any speed differences? Implicit <asp:Localize ID="Localize1" runat="server" meta:resourcekey="Something" /> vs Explicit <asp:Localize ID="Localize1" runat="server" Text="<%$ Resources

Implicit parameter resolution - setting the precedence

你说的曾经没有我的故事 提交于 2019-12-04 05:18:11
I am trying to create a typeclass Default that supplies the default value for a given type. Here is what I have come up with so far: trait Default[A] { def value: A } object Default { def withValue[A](a: A) = new Default[A] { def value = a } def default[A : Default]: A = implicitly[Default[A]].value implicit val forBoolean = withValue(false) implicit def forNumeric[A : Numeric] = withValue(implicitly[Numeric[A]].zero) implicit val forChar = withValue(' ') implicit val forString = withValue("") implicit def forOption[A] = withValue(None : Option[A]) implicit def forAnyRef[A >: Null] = withValue

How To Get “Find Usages” working with implicit operator methods?

╄→гoц情女王★ 提交于 2019-12-04 05:13:09
I never liked implicit operators (prefer extension methods) because it is hard to see visually when that cast/conversion happens in the code. Imagine if you have example like below: public static implicit operator Deal(string dealAsXml) { //convert the xml into Deal object } Above implicit operator helps you to cast/convert deal in Xml format into Deal Object. Usually when you right click on a method, you can use "Find Usages" (or Alt+F7) on it, which is quite helpful, is there anything similar for implicit operators ? I think that's another reason to use the Extensions methods where possible.

Scala Generics and Numeric Implicits

爱⌒轻易说出口 提交于 2019-12-04 05:04:44
I need to pass two functions as parameters to a scala function. That function should then evaluate them and get a number from them where it will then operate on. This number can be either a Int, Double or any other numeric type. I would like the function to work, whatever the types it is working with. The example bellow explains the issue. import Numeric.Implicits._ class Arithmetic[T : Numeric](val A: Connector[T], val B: Connector[T]) { val sum = new Connector({ A.value + B.value }) } class Constant[T](var x: T) { val value = new Connector({ x }) } class Connector[T](f: => T) { def value: T

Templates with implicit parameters, forward declaration, C++

萝らか妹 提交于 2019-12-04 04:16:16
There is a declaration of template class with implicit parameters: List.h template <typename Item, const bool attribute = true> class List: public OList <item, attribute> { public: List() : OList<Item, attribute> () {} .... }; I tried to use the fllowing forward declaration in a different header file: Analysis.h template <typename T, const bool attribute = true> class List; But G++ shows this error: List.h:28: error: redefinition of default argument for `bool attribute' Analysis.h:43: error: original definition appeared here If I use the forward declaration without implicit parameters template