Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# compiler bug? Why doesn't this implicit user-defined conversion compile?

Given the following struct:

public struct Foo<T> {    public Foo(T obj) { }     public static implicit operator Foo<T>(T input)    {       return new Foo<T>(input);    } } 

This code compiles:

private Foo<ICloneable> MakeFoo() {     string c = "hello";     return c; // Success: string is ICloneable, ICloneable implicitly converted to Foo<ICloneable> } 

But this code doesn't compile -- why?

private Foo<ICloneable> MakeFoo() {     ICloneable c = "hello";     return c; // Error: ICloneable can't be converted to Foo<ICloneable>. WTH? } 
like image 350
Judah Gabriel Himango Avatar asked Jul 30 '09 19:07

Judah Gabriel Himango


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Apparently, implicit user defined conversions don't work when one of the types is an interface. From the C# specs:


6.4.1 Permitted user-defined conversions

C# permits only certain user-defined conversions to be declared. In particular, it is not possible to redefine an already existing implicit or explicit conversion. For a given source type S and target type T, if S or T are nullable types, let S0 and T0 refer to their underlying types, otherwise S0 and T0 are equal to S and T respectively. A class or struct is permitted to declare a conversion from a source type S to a target type T only if all of the following are true:

  • S0 and T0 are different types.
  • Either S0 or T0 is the class or struct type in which the operator declaration takes place.
  • Neither S0 nor T0 is an interface-type.
  • Excluding user-defined conversions, a conversion does not exist from S to T or from T to S.

In your first method, both types are not interface types, so the user defined implicit conversion works.

The specs are not very clear, but it seems to me that if one of the types involved is an interface type, the compiler doesn't even try to look up any user-defined implicit conversions.

like image 71
Philippe Leybaert Avatar answered Oct 05 '22 00:10

Philippe Leybaert