implicits

behaviour of implicit function declaration

孤街浪徒 提交于 2019-12-08 14:12:13
问题 I know it is wrong to use a function without prototype. But when I was fiddling around, I came across this strange and conflicting behavior. test1 #include <stdio.h> #include <limits.h> void main(){ char c='\0'; float f=0.0; xof(c,f);/* at this point implicit function declaration is generated as int xof(int ,double ); */ } int xof(char c,float f) { printf("%d %f\n", c,f); } Implicit function declaration would be int xof(int ,double ); error is variablename.c:8:5: error: conflicting types for

Groovy equivalent for Scala implicit parameters - extended

我是研究僧i 提交于 2019-12-08 08:03:35
问题 This question extends my previous one Groovy equivalent for Scala implicit parameters Not sure if this is the right way to develop from a previous topic, but anyway.. I am looking for a way to express in groovy something like this: // scala object A { def doSomethingWith(implicit i:Int) = println ("Got "+i) } implicit var x = 5 A.doSomethingWith(6) // Got 6 A.doSomethingWith // Got 5 x = 0 A.doSomethingWith // Got 0 In general, I would like to execute a piece of logic, and have variables in

Implicit resolution with covariance

早过忘川 提交于 2019-12-08 06:49:27
问题 Why does the following code work when Foo is invariant, but not when it is covariant? The covariant version of Foo produces a type error saying that in the call of useF1 , the argument has type Foo[T] but F1 is required. A similar error is produced for useF2 . If the variance annotation is removed from Foo , the code works. The pattern match against F1 exposes the fact that T = Int , so that x has type Foo[Int] . The implicit conversion function is used to convert Foo[Int] to F1 in the

play json writes subclass gives ambiguous implicit values error

岁酱吖の 提交于 2019-12-07 15:11:53
问题 I'm using the play framework, and have an abstract class: abstract class Base{...} which has its own implicit JSON writer within the companion object object Base { implicit val baseWrites: Writes[Base] = (...)(unlift(Base.unapply)) } I subclass this abstract class: case class SubClass{...} which also has its own implicit JSON writer within its companion object object SubClass { implicit val subClassWrites: Writes[SubClass] = (...)(unlift(SubClass.unapply)) } When I try to serialize the

Clean up signatures with long implicit parameter lists

天大地大妈咪最大 提交于 2019-12-07 12:14:40
问题 Is there an elegant solution to somehow clean up implicit parameter lists making signatures more concise? I have code like this: import shapeless._ import shapeless.HList._ import shapeless.ops.hlist._ import shapeless.poly._ trait T[I, O] extends (I => O) trait Validator[P] object Validator{ def apply[P] = new Validator[P]{} } object valid extends Poly1 { implicit def caseFunction[In, Out] = at[T[In, Out]](f => Validator[In]) } object isValid extends Poly2 { implicit def caseFolder[Last, New

How to define type lambda properly?

∥☆過路亽.° 提交于 2019-12-06 19:23:56
问题 I used =:= as example type lambda for purpose of making simple minimal example. =:= type take two arguments, I'd like to curry one at type level. I take naive implementation type Curry[G] = {type l[L] = L =:= G} but in practical uses it causes errors: type X = Int type Y = Int type CurryInt[T] = T =:= Int type Curry[G] = {type l[L] = L =:= G} type CurrStatic = {type l[L] = L =:= Int} object CurryObj {type l[L] = L =:= Int} trait Apply[P[_], T] implicit def liftApply[P[_], T](implicit ev : P[T

Test two scala shapeless HList types for equivalence via implicit

半腔热情 提交于 2019-12-06 10:21:19
I'm interested in testing whether two HList heterogeneous records are "equivalent"; that is, they have the same key/val pairs, but not necessarily in the same order. Is there a predefined type predicate that does what EquivHLists does in the code fragment below? // shapeless heterogeneous records with "equivalent" types. // these should compile if given as the arguments to 'f' below. val hrec1 = ("a" ->> 1) :: ("b" ->> 2) :: HNil val hrec2 = ("b" ->> 2) :: ("a" ->> 1) :: HNil // only compiles if two HList records contain same information def f(hr1: H1 <: HList, hr2 : H2 <: HList)(implicit

Groovy equivalent for Scala implicit parameters

北战南征 提交于 2019-12-06 07:43:14
Is there some Groovy alternative to express something like the following: def doSomethingWith(implicit i:Int) = println ("Got "+i) implicit var x = 5 doSomethingWith(6) // Got 6 doSomethingWith // Got 5 x = 0 doSomethingWith // Got 0 Update: see a followup question here: Groovy equivalent for Scala implicit parameters - extended You can use closures with a default parameter: doSomethingWith = { i = value -> println "Got $i" } value = 5 doSomethingWith(6) // Got 6 doSomethingWith() // Got 5 value = 0 doSomethingWith() // Got 0 This is how I do implicits in Groovy @Test def void customString() {

play json writes subclass gives ambiguous implicit values error

倖福魔咒の 提交于 2019-12-06 03:37:47
I'm using the play framework, and have an abstract class: abstract class Base{...} which has its own implicit JSON writer within the companion object object Base { implicit val baseWrites: Writes[Base] = (...)(unlift(Base.unapply)) } I subclass this abstract class: case class SubClass{...} which also has its own implicit JSON writer within its companion object object SubClass { implicit val subClassWrites: Writes[SubClass] = (...)(unlift(SubClass.unapply)) } When I try to serialize the subclass object using Json.toJson(SubClass), I get an error: [error] both value subClassWrites in object

What to do with operations for a specific kind of collection?

好久不见. 提交于 2019-12-06 02:35:52
In several different places in my application, I need to take a Seq[SalesRow] and return a Map[String,SalesRow] , where the string is the name of a country. I need to use this in several places. For example, I take a list of all SalesRows and get a global breakdown of sales by country. But in other places, I want to break down my sales by month and then by country (so Map[Month,Seq[SalesRow]] becomes Map[Month,Map[String,Seq[SalesRow]]] ) - in still other places, I want to break down by day and then by country. My question is: where do I put the (small) amount of logic that takes a Seq