2,499 questions
0
votes
4
answers
532
views
Safe & Simple Access to Explicit Interface Members in C#
When I am working with explicit interface implementations in C#, it often becomes necessary to cast an object to one of its interfaces in order to access a member of that interface. Because of the ...
12
votes
3
answers
7k
views
How do implicit conversions work in Scala?
And more specifically how does the BigInt works for convert int to BigInt?
In the source code it reads:
...
implicit def int2bigInt(i: Int): BigInt = apply(i)
...
How is this code invoked?
I can ...
27
votes
2
answers
3k
views
Is it guaranteed that new Integer(i) == i in Java?
Consider the following snippet:
int i = 99999999;
byte b = 99;
short s = 9999;
Integer ii = Integer.valueOf(9); // should be within cache
System.out.println(new Integer(i) == i); ...
5
votes
4
answers
4k
views
C# implicit conversions
I'm currently working on an application where I need to load data from an SQL database and then assign the retrieved values into the properties of an object. I'm doing this by using reflection since ...
1
vote
3
answers
456
views
Implicit cast from now() to a date field
I have an issue with MySQL 5.1. A datetime data type isn't implicitly casted to match a date column.
SELECT * FROM my_table WHERE my_date_field = NOW()
This request doesn't return any rows using ...
8
votes
1
answer
364
views
Auto-(un)boxing fail for compound assignment
Thanks to the implicit casting in compound assignments and increment/decrement operators, the following compiles:
byte b = 0;
++b; b++; --b; b--;
b += b -= b *= b /= b %= b;
b <<= b >>= b ...
32
votes
1
answer
3k
views
Varying behavior for possible loss of precision
In Java, when you do
int b = 0;
b = b + 1.0;
You get a possible loss of precision error. But why is it that if you do
int b = 0;
b += 1.0;
There isn't any error?
3
votes
6
answers
1k
views
Using implicit conversion as a substitute for multiple inheritance in .NET
I have a situation where I would like to have objects of a certain type be able to be used as two different types. If one of the "base" types was an interface this wouldn't be an issue, but in my ...
7
votes
1
answer
4k
views
Scala traits and implicit conversion confusion
The following lines work when I enter them by hand on the Scala REPL (2.7.7):
trait myTrait {
override def toString = "something"
}
implicit def myTraitToString(input: myTrait): String = input....
2
votes
4
answers
2k
views
Implicit casting in VB.NET
The question is intended for lazy VB programmers. Please.
In vb I can do and I won't get any errors.
Example 1
Dim x As String = 5
Dim y As Integer = "5"
Dim b As Boolean = "True"
Example 2
Dim a ...
18
votes
1
answer
2k
views
Why does Option not extend the Iterable trait directly?
Option is implicitly convertible to an Iterable - but why does it not just just implement Iterable directly:
def iterator = new Iterator[A] {
var end = !isDefined
def next() = {
val n = if (...
11
votes
4
answers
3k
views
Explanation of casting/conversion int/double in C#
I coded some calculation stuff (I copied below a really simplifed example of what I did) like CASE2 and got bad results. Refactored the code like CASE1 and worked fine. I know there is an implicit ...
2
votes
1
answer
340
views
Question about implicit operator overloading in c#
MyClass c = 10;
Is there any way to make this code work? I know that through the implicit operator overloading you can get the opposite to work:
int i = instanceOfMyClass;
Thanks
5
votes
1
answer
1k
views
Why does this implicit type conversion in C# fail?
Background:
Let's assume I've got the following class:
class Wrapped<T> : IDisposable
{
public Wrapped(T obj) { /* ... */ }
public static implicit operator Wrapped<T>(T obj)
...
1
vote
1
answer
165
views
How do i cast A to object to class A when B can typcast to A?
Basically i want to do this. aa causes a bad cast exception.
NOTE: o can be ANYTHING. It may not be B, it can be C, D, E, F etc. But this should work as long as o is a class that can typecast into A (...
-5
votes
3
answers
286
views
How do I use implicit and explicit casts? [duplicate]
Possible Duplicate:
Identify the implicit cast and explicit cast
int a = 2, b = 3;
float f = 2.5;
double d = -1.2;
int int_result;
float real_result;
real_result = a * f;
real_result = (float) a ...
3
votes
4
answers
6k
views
Implicit casting Integer calculation to float in C++
Is there any compiler that has a directive or a parameter to cast integer calculation to float implicitly. For example:
float f = (1/3)*5;
cout << f;
the "f" is "0", because calculation's ...
2
votes
2
answers
1k
views
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)...
0
votes
4
answers
2k
views
Why friend overloaded operator is preferred to conversion operator in this case
Hi I have a code like this, I think both the friend overloaded operator and conversion operator have the similar function. However, why does the friend overloaded operator is called in this case? What'...
3
votes
1
answer
933
views
scala 2.8 implict java collections conversions
I have problem with JavaConversions with 2.8 beta:
import scala.collection.JavaConversions._
class Utils(dbFile : File, sep: String) extends IUtils {
(...)
def getFeatures() : java.util.List[...
2
votes
4
answers
485
views
why does a char + another char = a weird number
Here's the code snippet:
public static void main (String[]arg)
{
char ca = 'a' ;
char cb = 'b' ;
System.out.println (ca + cb) ;
}
The output is:
195
Why is this the case? I would ...
27
votes
5
answers
8k
views
How to tell if Type A is implicitly convertible to Type B
Given Type a and Type b, how can I, at runtime, determine whether there's an implicit conversion from a to b?
If that doesn't make sense, consider the following method:
public PropertyInfo ...
61
votes
3
answers
6k
views
Conditional operator cannot cast implicitly?
I'm a little stumped by this little C# quirk:
Given variables:
Boolean aBoolValue;
Byte aByteValue;
The following compiles:
if (aBoolValue)
aByteValue = 1;
else
aByteValue = 0;
But this ...
0
votes
2
answers
259
views
Implicit Conversion
My program was:-
#include < iostream.h>
#include < conio.h>
struct base
{
protected:
void get()
{
cin>>a>>b;
}
public:
base(int i=0, int j=0);
...
1
vote
5
answers
1k
views
Stopping an implicit cast on operator delete
My String class provides an operator char* overload to allow you to pass the string to C functions.
Unfortunately a colleague of mine just inadvertently discovered a bug.
He effectively had the ...
8
votes
2
answers
832
views
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) {
...
13
votes
3
answers
5k
views
Is there a way to do dynamic implicit type casting in C#?
Given this class with an implicit cast operator:
public class MyDateTime
{
public static implicit operator MyDateTime(System.Int64 encoded)
{
return new MyDateTime(encoded);
}
...
7
votes
2
answers
2k
views
Implicit version of IsAssignableFrom?
In my code using reflections i wrote
if (f.FieldType.IsAssignableFrom("".GetType()))
I have a class that has an implicit conversion to strings. However the if statement above doesnt catch it. How can ...
5
votes
1
answer
2k
views
Problem with implicit conversion and null
I have this function
public static implicit operator MyClass(string v) { return new MyClass(v); }
and write var.myclass = null;. This calls the implicit operator and passes null as string, which ...
3
votes
1
answer
2k
views
Invaid cast exception, even when I have an implicit cast operator defined (in asp mvc app)
I have an mvc model class created and one of the properties is of type 'MyObject'. It also has a System.ComponentModel.DataAnnotations.StringLength attribute on it.
MyObject as implicit cast ...
3
votes
2
answers
1k
views
C++ templates and ambiguity problem
I have a subset of a pointer class that look like:
template <typename T>
struct Pointer
{
Pointer();
Pointer(T *const x);
Pointer(const Pointer &x);
template <...
1
vote
4
answers
1k
views
simulate C++ function template instantiation with implicit conversion
I already asked two questions related to what I'm trying to do (one resolved, one of which I will close soon). I know that C++ template instantiation does not allow any implicit conversions (see for ...
1
vote
1
answer
570
views
System.Type; implicit cast to string
While looking at the System.Type class under the Code Definition Window, I cannot seem to understand how an instance of this class is implicitly cast to string. For example, on the following code:
...
5
votes
3
answers
2k
views
No implicit int -> short conversion in ternary statement
short s;
s = (EitherTrueOrFalse()) ? 0 : 1;
This fails with:
error CS0266: Cannot implicitly
convert type 'int' to 'short'. An
explicit conversion exists (are you
missing a cast?)
Can anyone ...
4
votes
4
answers
2k
views
Will the c# compiler perform multiple implicit conversions to get from one type to another?
Let's say you have yourself a class like the following:
public sealed class StringToInt {
private string _myString;
private StringToInt(string value)
{
_myString = value;
...
8
votes
5
answers
12k
views
C++ operator overloading and implicit conversion
I have a class that encapsulates some arithmetic, let's say fixed point calculations. I like the idea of overloading arithmetic operators, so I write the following:
class CFixed
{
CFixed( int );
...
2
votes
3
answers
7k
views
Which Json deserializer renders IList<T> collections?
I'm trying to deserialize json to an object model where the collections are represented as IList<T> types.
The actual deserializing is here:
JavaScriptSerializer serializer = new ...
2
votes
1
answer
1k
views
Runtime InvalidCastException with implicit cast operator
I have a C# library that internal clients configure with VB.Net
Their scripts are throwing an InvalidCastException where they really shouldn't.
So the code is something like this (massively ...
11
votes
6
answers
8k
views
Getting rid of error C2243
Is it possible to getting rid of error C2243?
class B {};
class D : protected B {};
D d;
B *p = &d; // conversion from 'D *' to 'B &' exists, but is inaccessible
I had this error in my app ...
5
votes
10
answers
5k
views
Why is implicit conversion allowed from superclass to subclass?
Can someone tell me why the line with "//Compiles" compiles, and why the line with "//Doesn't Compile" does not?
I don't understand why A would be implicitly convertible to B, not the other way ...
9
votes
9
answers
11k
views
C++ rely on implicit conversion to bool in conditions?
I found the following rule in a coding standards sheet :
Do not rely on implicit conversion to bool in conditions.
if (ptr) // wrong
if (ptr != NULL) // ok
How reasonable/usefull is this ...
1
vote
2
answers
660
views
JavaScript implicit conversions in equality with a string
How does JavaScript behave for the comparisons true == "true" and (0 == "0")?
6
votes
4
answers
2k
views
Why do implicit conversion member functions overloading work by return type, while it is not allowed for normal functions?
C++ does not allow polymorphism for methods based on their return type. However, when overloading an implicit conversion member function this seems possible.
Does anyone know why? I thought operators ...
30
votes
4
answers
7k
views
What is the difference between double a = a + int b and int a += double b?
Why does:
public class Addition {
public static void main() {
int a = 0;
double b = 1.0;
a = a + b;
System.out.println(a);
}
}
not compile but:
public class Addition {
...
23
votes
5
answers
11k
views
implicit operator
I just saw it was using in one of the recent answers:
public static implicit operator bool(Savepoint sp)
{
return sp != null;
}
Why do we need word implicit here, and what does it mean?
3
votes
5
answers
2k
views
Unable to find operator via implicit conversion in C++
When writing a class to act as a wrapper around a heap-allocated object, I encountered a problem with implicit type conversion that can be reduced to this simple example.
In the code below the ...
156
votes
16
answers
98k
views
Can we define implicit conversions of enums in c#?
Is it possible to define an implicit conversion of enums in c#?
something that could achieve this?
public enum MyEnum
{
one = 1, two = 2
}
MyEnum number = MyEnum.one;
long i = number;
If not, ...
64
votes
8
answers
27k
views
Can you use keyword explicit to prevent automatic conversion of method parameters?
I know you can use C++ keyword 'explicit' for constructors of classes to prevent an automatic conversion of type. Can you use this same command to prevent the conversion of parameters for a class ...
71
votes
1
answer
39k
views
implicit operator using interfaces
I have a generic class that I'm trying to implement implicit type casting for. While it mostly works, it won't work for interface casting. Upon further investigation, I found that there is a ...