implicit-conversion

Missing Java error on conditional expression?

With methods test1() and test2(), I get a Type Mismatch Error: Cannot convert from null to int, which is correct; but why am I not getting the same in method test3()? How does Java evaluate the conditional expression differently in that case? (obviously, a NullPointerException will rise at runtime). Is it a missing error? public class ...

C# enum to string auto-conversion?

Is it possible to have the compiler automatically convert my Enum values to strings so I can avoid explicitly calling the ToString method every time. Here's an example of what I'd like to do: enum Rank { A, B, C } Rank myRank = Rank.A; string myString = Rank.A; // Error: Cannot implicitly convert type 'Rank' to 'string' string myStrin...

Are operands inside an expression promoted to larger types according to the following rules?

If numeric expression contains operands (constants and variables) of different numeric types, are operands promoted to larger types according to the following rules: if operands are of types byte, sbyte, char, short, ushort, they get converted to int type If one of the operands is int, then all operands are converted to int if expressi...

C++ implicit conversions and ambiguity in overloaded function call

I am facing the following problem: I have a class V (say a vector) from which I can produce two classes: CI and I (think of const_iterator and iterator). If I have a const V then I can only produce CI (again think of iterator and const_iterator). Essentially I would like to subsitute (const V& v) with (CI ci) and (V& v) with (I i). Mor...

C# using LINQ and Nullable Boolean

I have the below linq query which takes a text field which may be Y, N or DBnull and populates a boolean? parameter with either True, False or null depending on the value of the field. var dset = from i in tbdc.Talkbacks where i.talkback_id == id select new Talkback(i.talkback_id, i.acad...

Can I "pimp my library" with an analogue of TraversableLike.map that has nicely variant types?

Suppose I want to add functionality like map to a Scala List, something along the lines of list mapmap f, which applies the function f to each element of list twice. (A more serious example might be implementing a parallel or distributed map, but I don't want to get distracted by details in that direction.) My first approach would be o...

How do I allow my class to be implicitly converted to a string in C#?

Here's what I want to do... public class A { public string Content { get; set; } } A a = new A(); a.Content = "Hello world!"; string b = a; // b now equals "<span>Hello world!</span>" So I want to control how a is converted into a String somehow ...

Type Comparisons in SQL

I've got the following bit of code as part of a SQL Query: INSERT INTO [Database] SELECT DISTINCT @ssId FROM [Document_Map] WHERE (LabelId IN (SELECT Tokens FROM StringSplitter(@sortValue, '|', 1)) The code works fine as long as @SortValue is an integer, (LabelId is an int as well) or integers separat...

A problem of implicit conversions in scala 2.8

I want to write a implicit conversion of Tuple2[A,B] to Seq[C] where C is super type of both A and B. My first try as following: implicit def t2seq[A,B,C](t: (A,B))(implicit env: (A,B) <:< (C,C)): Seq[C] = { val (a,b) = env(t) Seq(a,b) } But it doesn't work: scala> (1,2): Seq[Int] <console>:7: error: type mismatch; found :...

Examples of Implicit Variable Assignment in C#

I noticed you can do this sort of thing in C#: XNamespace c = "http://s.opencalais.com/1/pred/"; Notice the string value is implicitly converted to different type. Are there other places this can be done? What are some common patterns and practices around this sort of thing? ...

Why is currying and uncurrying not implicit in scala

If I have a function: f : A => B => C I can define an implicit conversion such that this can be used where a function (A, B) => C is expected. This goes in the other direction also. Why are these conversions not implicit (or available implicitly)? I am assuming that bad things could happen for some value of bad things. What value is ...

Scala: convert a return type into a custom trait

I've written a custom trait which extends Iterator[A] and I'd like to be able to use the methods I've written on an Iterator[A] which is returned from another method. Is this possible? trait Increment[+A] extends Iterator[A]{ def foo() = "something" } class Bar( source:BufferedSource){ //this ain't working def getContents(...

In Scala, how come `println(1,2)` works?

In Scala (2.7.7final), the Predef.println method is defined as having the following signature: def println (x : Any) : Unit How come, then that the following works: scala> println(1,2) (1,2) Does the compiler automatically convert a comma-separated list of arguments into a Tuple? By what magic? Is there an implicit conversion goi...

Equivalent implicit operators: why are they legal?

Update! See my dissection of a portion of the C# spec below; I think I must be missing something, because to me it looks like the behavior I'm describing in this question actually violates the spec. Update 2! OK, upon further reflection, and based on some comments, I think I now understand what's going on. The words "source type" in t...

Why is implicit conversion from Long to RichLong not applied where a supertype of RichLong is expected?

Scala 2.8 spec says in section 7.3 (highlighting is mine): Implicit parameters and methods can also define implicit conversions called views. A view from type S to type T is defined by an implicit value which has function type S=>T or (=>S)=>T or by a method convertible to a value of that type. Views are applied in two situati...

Weird Select Result

Hi All, I have a table with an ID field of INT Type. I am doing some data validation and noticed that SELECT * from mytable where id=94 and SELECT * from mytable where id='94' works perfectly, but if I use SELECT * from mytable where id='94dssdfdfgsdfg2' it gave me the same result! How is this possible? ...

Question regarding implicit conversions in the C# language specification

Section 6.1 Implicit conversions defines an identity conversion thusly: An identity conversion converts from any type to the same type. This conversion exists such that an entity that already has a required type can be said to be convertible to that type. Now, what is the purpose of sentences such as these? (In §6.1.6 Implicit ...

Could not find implicit value for parameter ordering

Hi, I get the following error when trying to compile this: Btree.scala:9: error: could not find implicit value for parameter ordering: Ordering[K] abstract class Node[K,V] extends TreeMap[K,V] TreeMap is supposed to accept an implicit Ordering[A] val which I provide. Perhaps the implicit parameter needs to be in object Tester ...

implicit operator on generic types

Is there anything wrong with using an implicit operator like the following: //linqpad c# program example void Main() { var testObject = new MyClass<int>() { Value = 1 }; var add = 10 + testObject; //implicit conversion to int here add.Dump(); // 11 } class MyClass<T> { public T Value { get; set; } public static imp...

Why cannot this case of implicit conversions be optimized?

Why cannot Scala optimize the following: a. implicit def whatever[A](a: A) = new { ... } to: b. class some$generated$name(a: A) { ... } implicit def whatever[A](a: A) = new some$generated$name(a) ? Why does it have to use structural typing in this case? I would like Scala compiler to perform this optimization as writing in sty...