implicit

Using a context bound in a class type parameter

岁酱吖の 提交于 2019-12-01 17:50:58
I was under the impression that context bounds would work only on methods: trait Target[T] class Post { def pinTo[T : Target](t:T) } apparently context bounds can be used in class (and presumably trait ) too: trait Target[T] class Post[T:Target] { def pintTo[T](t:T) } Now I'm confused as to how the evidence can be provided to Post ? class Business implicit object ev extends Target[Business] // is implicit necessary here ? val p = new Post[Business] // ?? how do I provide ev ? related to Modeling a binary relationship between two types The A: Foo notation for context bounds is only a shortcut

In c# 3.0, is it possible to add implicit operators to the string class?

一笑奈何 提交于 2019-12-01 16:57:03
问题 something like public static class StringHelpers { public static char first(this string p1) { return p1[0]; } public static implicit operator Int32(this string s) //this doesn't work { return Int32.Parse(s); } } so : string str = "123"; char oneLetter = str.first(); //oneLetter = '1' int answer = str; // Cannot implicitly convert ... 回答1: No, there's no such thing as extension operators (or properties etc) - only extension methods . The C# team have considered it - there are various

Strings and ints, implicit and explicit

微笑、不失礼 提交于 2019-12-01 16:02:30
Had a coworker ask me this, and in my brain befuddled state I didn't have an answer: Why is it that you can do: string ham = "ham " + 4; But not: string ham = 4; If there's an implicit cast/operation for string conversion when you are concatenating , why not the same when assigning it as a string? (Without doing some operator overloading, of course) When concatenating the compiler turns the statement "ham" + 4 into a call to String.Concat , which takes two object parameters, so the value 4 is boxed and then ToString is called on that. For the assignment there is no implicit conversion from int

using coalescing null operator on nullable types changes implicit type

巧了我就是萌 提交于 2019-12-01 15:12:29
I would expect the next three lines of code to be the same: public static void TestVarCoalescing(DateTime? nullableDateTime) { var dateTimeNullable1 = nullableDateTime.HasValue ? nullableDateTime : DateTime.Now; var dateTimeNullable2 = nullableDateTime != null ? nullableDateTime : DateTime.Now; var dateTimeWhatType = nullableDateTime ?? DateTime.Now; } In all cases, I assign nullableDateTime to the new variable. I would expect the type of all variables to become DateTime? since that is the type of nullableDateTime . But to my surprise, the type of dateTimeWhatType just becomes DateTime , so

Strings and ints, implicit and explicit

坚强是说给别人听的谎言 提交于 2019-12-01 14:45:14
问题 Had a coworker ask me this, and in my brain befuddled state I didn't have an answer: Why is it that you can do: string ham = "ham " + 4; But not: string ham = 4; If there's an implicit cast/operation for string conversion when you are concatenating , why not the same when assigning it as a string? (Without doing some operator overloading, of course) 回答1: When concatenating the compiler turns the statement "ham" + 4 into a call to String.Concat , which takes two object parameters, so the value

Opening System Application Using Intent

我与影子孤独终老i 提交于 2019-12-01 14:15:21
I am trying to make a simple application which will send the user to a specific (system installed) app (system settings, calendar, browser, etc.) when clicked by the user from the home screen or app drawer. For instance, I am currently trying to open the system settings whenever my app is launched, just like a shortcut for settings does. Is it possible to implement this the way I want it? Does anyone have any suggestions? Here is my code: import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; public class MainActivity extends Activity { public

using coalescing null operator on nullable types changes implicit type

时间秒杀一切 提交于 2019-12-01 14:00:25
问题 I would expect the next three lines of code to be the same: public static void TestVarCoalescing(DateTime? nullableDateTime) { var dateTimeNullable1 = nullableDateTime.HasValue ? nullableDateTime : DateTime.Now; var dateTimeNullable2 = nullableDateTime != null ? nullableDateTime : DateTime.Now; var dateTimeWhatType = nullableDateTime ?? DateTime.Now; } In all cases, I assign nullableDateTime to the new variable. I would expect the type of all variables to become DateTime? since that is the

Scala implicits resolution mechanism is declaration order dependent?

雨燕双飞 提交于 2019-12-01 11:20:25
During daily Scala coding I faced an issue that Scala implicits resolution depends on declaration order. A simple example: object example extends App { trait FooTypeClass[T] { def foo: T } def bar[T](implicit tc: FooTypeClass[T]) = println(tc.foo) class A { // bar[A] doesn't compile } object A { implicit object aFoo extends FooTypeClass[A] { def foo: A = new A { override def toString = "a" } } } bar[A] } It does compile, but if I uncomment the commented line, it won't find the required implicit in scope. So, to make it compile I should place object A declaration before class A . That means my

Opening System Application Using Intent

↘锁芯ラ 提交于 2019-12-01 09:43:00
问题 I am trying to make a simple application which will send the user to a specific (system installed) app (system settings, calendar, browser, etc.) when clicked by the user from the home screen or app drawer. For instance, I am currently trying to open the system settings whenever my app is launched, just like a shortcut for settings does. Is it possible to implement this the way I want it? Does anyone have any suggestions? Here is my code: import android.app.Activity; import android.content

Constructing a value through two implicit constructors?

◇◆丶佛笑我妖孽 提交于 2019-12-01 08:56:00
TLDR : I have two templatized classes Outer and Inner . Inner<X> can be implicitly constructed from X , and Outer<Y> can be implicitly constructed from Y . Should Outer<Inner<X>> = X() work? More details: Suppose I have the following two classes: template<typename T> class Inner { public: Inner(const T& value) {} Inner(T&& value) {} }; template<typename T> class Outer { public: Outer(const T& value) {} Outer(T&& value) {} }; Consider the following function: struct SomeType{}; Outer<Inner<SomeType>> DoSomethingFails() { SomeType value; return value; } g++ complains: no viable conversion from