implicit

Why can't the first parameter list of a class be implicit?

限于喜欢 提交于 2019-11-27 15:49:30
问题 scala> class A(implicit a: Int); defined class A scala> class B()(implicit a: Int); defined class B scala> new A()(1) res1: A = A@159d450 scala> new B()(1) res2: B = B@171f735 scala> new A(1) <console>:7: error: too many arguments for constructor A: ()(implicit a: Int)A new A(1) Why does Scalac insert an empty parameter list before the implicit parameter list provided in the class declaration? This seems to be a feature, not a bug, judging by the commentary in the scalac sources: // convert

How does SQL Server decide format for implicit datetime conversion?

耗尽温柔 提交于 2019-11-27 14:01:55
declare @str_datetime varchar(50) set @str_datetime='30-04-2012 19:01:45' -- 30th April 2012 declare @dt_datetime datetime select @dt_datetime=@str_datetime This is giving following error: Msg 242, Level 16, State 3, Line 4 The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. My question is how SQL Server decides which format to use for implicit datetime conversion? This can depend on a variety of factors - the operating system's regional settings, the current user's language and dateformat settings. By default, Windows uses US English , and the user

Using context bounds “negatively” to ensure type class instance is absent from scope

天涯浪子 提交于 2019-11-27 13:16:45
tl;dr : How do I do something like the made up code below: def notFunctor[M[_] : Not[Functor]](m: M[_]) = s"$m is not a functor" The ' Not[Functor] ', being the made up part here. I want it to succeed when the 'm' provided is not a Functor, and fail the compiler otherwise. Solved : skip the rest of the question and go right ahead to the answer below. What I'm trying to accomplish is, roughly speaking, "negative evidence". Pseudo code would look something like so: // type class for obtaining serialization size in bytes. trait SizeOf[A] { def sizeOf(a: A): Long } // type class specialized for

How does this recursive List flattening work?

梦想的初衷 提交于 2019-11-27 11:21:49
问题 A while back this was asked and answered on the Scala mailing list: Kevin: Given some nested structure: List[List[...List[T]]] what's the best (preferably type-safe) way to flatten it to a List[T] Jesper: A combination of implicits and default arguments works: case class Flat[T, U](fn : T => List[U]) implicit def recFlattenFn[T, U](implicit f : Flat[T, U] = Flat((l : T) => List(l))) = Flat((l : List[T]) => l.flatMap(f.fn)) def recFlatten[T, U](l : List[T])(implicit f : Flat[List[T], U]) = f

Why is there an implicit type conversion from pointers to bool in C++?

你离开我真会死。 提交于 2019-11-27 09:40:40
Consider the class foo with two constructors defined like this: class foo { public: foo(const std::string& filename) {std::cout << "ctor 1" << std::endl;} foo(const bool some_flag = false) {std::cout << "ctor 2" << std::endl;} }; Instantiate the class with a string literal, and guess which constructor is called? foo a ("/path/to/file"); Output: ctor 2 I don't know about you, but I don't find that the most intuitive behavior in programming history. I bet there is some clever reason for it, though, and I'd like to know what that might be? It's very common in C to write this void f(T* ptr) { if

Why does the compiler choose bool over string for implicit typecast of L“”?

混江龙づ霸主 提交于 2019-11-27 09:05:54
Having recently introduced an overload of a method the application started to fail. Finally tracking it down, the new method is being called where I did not expect it to be. We had setValue( const std::wstring& name, const std::wstring& value ); std::wstring avalue( func() ); setValue( L"string", avalue ); std::wstring bvalue( func2() ? L"true", L"false" ); setValue( L"bool", bvalue ); setValue( L"empty", L"" ); It was changed so that when a bool value is stored we use the same strings (internal data storage of strings) setValue( const std::wstring& name, const std::wstring& value ); setValue(

Scala: generic weighted average function

浪尽此生 提交于 2019-11-27 07:50:54
问题 I want to implement a generic weighted average function which relaxes the requirement on the values and the weights being of the same type. ie, I want to support sequences of say: (value:Float,weight:Int) and (value:Int,weight:Float) arguments and not just: (value:Int,weight:Int) . [See my earlier question in the run up to this.] This is what I currently have: def weightedSum[A: Numeric](weightedValues: GenSeq[(A, A)]): (A, A) def weightedAverage[A: Numeric](weightedValues: GenSeq[(A, A)]): A

Android, How to read QR code in my application?

[亡魂溺海] 提交于 2019-11-27 05:58:20
In my application I need to read Qr code. I searched the net and found Zing codes however lots of developers had problem with using it and it seems it is buggy! If i assume that my customers has qr reader installed on their device, how can i use those applications and call them via implicit intents? if user doesn't have any qr reader, what will happen to the application? if it crashes, may i ask user to download for example QrDroid and after that use it? try { Intent intent = new Intent("com.google.zxing.client.android.SCAN"); intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); // "PRODUCT_MODE for

Scala: Implicit parameter resolution precedence

非 Y 不嫁゛ 提交于 2019-11-27 04:39:14
Suppose we have implicit parameter lookup concerning only local scopes: trait CanFoo[A] { def foos(x: A): String } object Def { implicit object ImportIntFoo extends CanFoo[Int] { def foos(x: Int) = "ImportIntFoo:" + x.toString } } object Main { def test(): String = { implicit object LocalIntFoo extends CanFoo[Int] { def foos(x: Int) = "LocalIntFoo:" + x.toString } import Def._ foo(1) } def foo[A:CanFoo](x: A): String = implicitly[CanFoo[A]].foos(x) } In the above code, LocalIntFoo wins over ImportedIntFoo . Could someone explain how it's considered more specific using "the rules of static

How can implicits with multiple inputs be used in Scala?

雨燕双飞 提交于 2019-11-27 04:37:40
问题 For example, how can I write an expression where the following is implicitly applied: implicit def intsToString(x: Int, y: Int) = "test" val s: String = ... //? Thanks 回答1: Implicit functions of one argument are used to automatically convert values to an expected type. These are known as Implicit Views. With two arguments, it doesn't work or make sense. You could apply an implicit view to a TupleN : implicit def intsToString( xy: (Int, Int)) = "test" val s: String = (1, 2) You can also mark