implicit

Avoiding implicit def ambiguity in Scala

I am trying to create an implicit conversion from any type (say, Int) to a String... An implicit conversion to String means RichString methods (like reverse) are not available. implicit def intToString(i: Int) = String.valueOf(i) 100.toCharArray // => Array[Char] = Array(1, 0, 0) 100.reverse // => error: value reverse is not a member ...

Object converting string into "A"

I would like to write a class looking like this: class Store[+A](dest: Symbol)(implicit c: String => A) extends Action(dest) { override def update(options: HashMap[Symbol,Any], arg: String): Unit = { options += ((dest -> c(arg))) } } object Store { def apply[A](dest: Symbol)(c: String=>A) = new Store[A](dest)(c) def apply[A...

Scala implicit usage choices

I've been wondering whether transparent implicit conversions are really such a good idea and whether it might actually be better to use implicits more, um, explicitly. For example, suppose I have a method which accepts a Date as a parameter and I have an implicit conversion which turns a String into a Date: implicit def str2date(s: Stri...

C++ implicit function calls

Will c++ implicit function calls be a feature of C++0x ? It is an interesting feature, but I haven't seen any progress on this and the GCC C++0x page didn't even mention it. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1611.pdf ...

Automatic type conversion with Castle ActiveRecord properties.

I have a Castle ActiveRecord class with a DateTime property. I am importing data from a text file, and would love to be able to do something like this: string date_started = "09/25/2009"; MyClass myclass = new MyClass; myclass.date_started = date_started; On the final assignment, behind the scenes, it would ideally check the type of d...

Is there ever an excuse for throwing an Exception from an implicit conversion?

From MSDN: By eliminating unnecessary casts, implicit conversions can improve source code readability. However, because implicit conversions can occur without the programmer's specifying them, care must be taken to prevent unpleasant surprises. In general, implicit conversion operators should never throw exceptions and never lose inf...

Scala Implicit convertions: 2 way to invoke

@lucastex posted about the Java Elvis operator, and I tried something in Scala to get the same effect. I've just converted everything to a new Structural Type with the ?: operator taking an object of the same type as argument. So Say: implicit def toRockStar[B](v : B) = new { def ?:(opt: => B) = if (v == n...

C++ implicit conversion to bool

In an effort to make my enums more typesafe, I've been using macro-generated overloaded operators to disallow comparing enums against anything but an identically typed enum: #include <boost/static_assert.hpp> #define MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, op) \ template<typename T> \ inline bool operator op(enumtype lhs, T rhs) \ ...

Implicit operator - when is it a good/bad idea?

I'm working on an application where the notion of Direction (forwards/backwards) is very important. The problem is that spread throughout the code base there are several different conventions: in some places it is true/false, and in others +1/-1. To try and bring this together, I created: public class DirectionClass { public boo...

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

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 answ...

Is there a way to control which implicit conversion will be the default used?

Suppose I have this: class String2(val x:String) { def *(times:Int) : String = { val builder = new StringBuilder() for( i <- 0 until times) { builder.append(x) } builder.toString() } } now if I add this implicit: implicit def gimmeString2(y:String) = new String2(y) I will get a co...

Implicit VB performance question

Sometimes I have to implement an interface or inherit a virtual (MustInherit) that the base method expects an object, whilst I know that the value I will be passing will always be an Integer for example. What should be the best performance from the examples below: Public Sub DoSomething(ByVal obj As Object) 'option 1: Dim x As ...

Queries that implicit SQL joins can't do?

I've never learned how joins work but just using select and the where clause has been sufficient for all the queries I've done. Are there cases where I can't get the right results using the WHERE clause and I have to use a JOIN? If so, could someone please provide examples? Thanks. ...

Using string constants in implicit conversion

Consider the following code: public class TextType { public TextType(String text) { underlyingString = text; } public static implicit operator String(TextType text) { return text.underlyingString; } private String underlyingString; } TextType text = new TextType("Something"); String str = text; //...

The behavior of a C compiler with old-styled functions without prototypes

When my program consists of two files: main.c #include <stdio.h> int main(void) { printf("%lf\n",f()); return 0; } func.c double f(int a) { return 1; } compiler do not show any errors. When my program consists of only one file: main.c #include <stdio.h> int main(void) { printf("%lf\n",f()); retur...

Preventing implicit cast of numerical types in constructor in C++

I have a constructor of the form: MyClass(int a, int b, int c); and it gets called with code like this: MyClass my_object(4.0, 3.14, 0.002); I would like to prevent this automatic conversion from double to int, or at least get warnings at compile time. It seems that the "explicit" keyword does not work in these case, right? ...

When must we use implicit and explicit operators in C#?

What is the usage of these operators? ...

How can implicits with multiple inputs be used in Scala?

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 ...

C++, how implicit conversion/constructor are determined?

How does C++ determine implicit conversion/construction of objects few levels deep? for example: struct A {}; struct B: A {}; struct C { operator B() { return B(); } }; void f(A a) {} int main(void) { f(C()); } Does it create tree of all possible conversions and chooses appropriate terminal? Something else? Thanks ...

Convert Option[Object] to Option[Int] Implicitly

I'm working with legacy Java code which returns java.lang.object. I'm passing it into a function and I'd like to do some implicit conversions as such: implicit def asInt( _in:Option[Object] ) = _in asInstanceOf[ Option[Int] ] implicit def asDouble( _in:Option[Object] = _in asInstanceOf[ Option[Double] ] private def parseEntry( _name:S...