implicit-conversion

Implicit Conversion over a Collection

谁说我不能喝 提交于 2020-01-10 18:54:06
问题 I ran into a problem this week regarding implicit conversions in C# on collections. While this (using implicit ) may not be our final approach, I wanted to at least finish out the code to offer the team as an option. I boiled down the problem to the following sample case: I have two classes in my example: one that represents a business object (Foo) and one that represents the client version (View Object) of this business item (FooVO), as defined below... public class Foo { public string Id

Difference between implicit conversion and explicit conversion [duplicate]

吃可爱长大的小学妹 提交于 2020-01-10 10:23:09
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Implicit VS Explicit Conversion What is the difference between "implicit conversion" and "explicit conversion"? Is the difference different in Java and C++? 回答1: An explicit conversion is where you use some syntax to tell the program to do a conversion. For example (in Java): int i = 999999999; byte b = (byte) i; // The type cast causes an explicit conversion b = i; // Compilation error!! No implicit conversion

Difference between implicit conversion and explicit conversion [duplicate]

房东的猫 提交于 2020-01-10 10:22:21
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Implicit VS Explicit Conversion What is the difference between "implicit conversion" and "explicit conversion"? Is the difference different in Java and C++? 回答1: An explicit conversion is where you use some syntax to tell the program to do a conversion. For example (in Java): int i = 999999999; byte b = (byte) i; // The type cast causes an explicit conversion b = i; // Compilation error!! No implicit conversion

Enumeration and mapping with Scala 2.10

好久不见. 提交于 2020-01-10 05:13:33
问题 I'm trying to port my application to Scala 2.10.0-M2. I'm seeing some nice improvements with better warnings from compiler. But I also got bunch of errors, all related to me mapping from Enumeration.values . I'll give you a simple example. I'd like to have an enumeration and then pre-create bunch of objects and build a map that uses enumeration values as keys and then some matching objects as values. For example: object Phrase extends Enumeration { type Phrase = Value val PHRASE1 = Value("My

Scala - implicit macros & materialisation

寵の児 提交于 2020-01-06 15:17:17
问题 The use cases for implicit macros is supposed to be the so-called "materialisation" of type class instances. Unfortunately, the example in the documentation is a bit vague on how that is achieved. Upon being invoked, the materializer can acquire a representation of T and generate the appropriate instance of the Showable type class. Let's say I have the following trait ... trait PrettyPrinter[T]{ def printed(x:T) : String } object PrettyPrinter{ def pretty[T](x:T)(implicit pretty:PrettyPrinter

Function template parameters failing to convert type during compilation

瘦欲@ 提交于 2020-01-06 11:20:14
问题 While trying to use a function template that calls a class's specialized static function template it is failing to convert its parameter from the template parameter list. Here is the function that I'm calling in main: template<class Engine, typename Type, template<typename = Type> class Distribution, class... DistParams> Type randomGenerator( RE::SeedType seedType, std::size_t seedValue, std::initializer_list<std::size_t> list, DistParams... params ) { static Type retVal = 0; static Engine

How does Swift's int literal to float inference work?

*爱你&永不变心* 提交于 2020-01-06 04:43:06
问题 Swift can infer int literals to be doubles or floats let x: Float = 3 It even works with arithmetic. It will convert everything before doing the math, so this is also 3: let y: Float = 5/2 + 0.5 But what are the actual rules for this? There are ambiguous situations, for example if the inference is for a parameter: func foobar(_ x: Int) -> Int { return x } func foobar(_ x: Float) -> Float { return y } foobar(1/2) In this case it infers it as an int and returns 0, but if you delete the first

Can an implicit conversion of an implicit value satisfy an implicit parameter?

こ雲淡風輕ζ 提交于 2020-01-05 07:00:38
问题 I'm defining some Scala implicits to make working with a particular unchangeable set of Java classes easier. The following Scala code is a simplified example that obviously looks crazy, in the real world I'm trying to grab particular resources (rather than numeric age) implicitly from the Monkey, Tree & Duck for use in various methods like purchaseCandles() : // actually 3 Java classes I can not change: case class Monkey(bananas: Int) case class Tree(rings: Int) case class Duck(quacks: Seq

Why is this implicit function not being applied?

蹲街弑〆低调 提交于 2020-01-04 09:13:54
问题 Trying out the implicit conversion of TupleN proposed by @Alexey Romanov in How to apply implicit conversions between tuples? Given the following implicits: object ImplicitConversions { object A { implicit def toA(x: Int): A = A(x) } case class A(v: Int) extends AnyVal implicit def liftImplicitTuple2[A, B, A1, B1](tuple: (A, B)) (implicit f1: A => A1, f2: B => B1): (A1, B1) = (f1(tuple._1), f2(tuple._2)) } and the Tuple (1,2) , I can manually apply liftImplicitTuple2 and have it invoke the A

Higher order functions: automatic generation vs manual definition

喜夏-厌秋 提交于 2020-01-04 02:57:08
问题 I'm trying to delay evaluation for a bit, and so I prefer to work with functions as long as possible. I have class Function which defines composition and pointwise arithmetics for functions: from functools import reduce def compose(*funcs): ''' Compose a group of functions f1, f2, f3, ... into (f1(f2(f3(...)))) ''' result = reduce(lambda f, g: lambda *args, **kaargs: f(g(*args, **kaargs)), funcs)) return Function(result) class Function: ''' >>> f = Function(lambda x : x**2) >>> g = Function