[Mono-list] implicit, explicit, and why does C# have these?

David La Motta dlamotta@email.com
Fri, 17 Oct 2003 09:23:14 -0400


This is a multi-part message in MIME format.
--------------050506000809050306040000
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Thanks for the explanation.  I can see how the implicit operator can be 
useful in the example you describe; still, I think it wasn't necessary 
for C# to expose them to us.  I.e., let us deal with the explicit casts 
and spare the confusion they may cause.  In other words, an implicit 
cast from a Pear object to a Truck object can seem quite odd, assuming 
their inheritance tree has nothing in common.

// David

Jonathan Pryor wrote:

>It's good to keep this in mind: C#'s "builtin" types (int, long, etc.)
>are actually aliases for managed types (System.Int32, System.Int64,
>etc.).  These managed types are (for Mono, at least) written in C#.
>
>You expect the following code to work:
>
>	int n = 42;
>	long l = n;
>	short s = (short) n;
>
>Which means that the following code also works:
>
>	System.Int32 n = 42;
>	System.Int64 l = n;
>	System.Int16 s = (System.Int16) n;
>
>Which means that the managed implementation of those managed types needs
>*some way* to represent to the compiler/runtime that some coercions are
>"safe" (can be done implicitly), while others are "unsafe" (can be done
>explicitly).
>
>C#'s implicit/explicit operators are how this is declared and defined.
>
>As for the differences between implicit & explicit, you understand the
>difference already.  Implicit == doesn't need a cast; explicit ==
>requires a cast.
>
> - Jon
>
>On Thu, 2003-10-16 at 16:52, David La Motta wrote:
>  
>
>>So I was reading my "C# for Java Developers" book and I came across the 
>>implicit and explicit operators.  Java doesn't have these but a friend 
>>suggests C++ does.  Being that I am not a C++ developer I really can't 
>>comment much on it, except to say that it looks like C# just decided to 
>>copy functionality from C++, just because it is a "cute" feature of the 
>>language.  I also can't quite put my finger on the difference between 
>>implicit and explicit, so if anybody has any insight on these, please, 
>>do share.
>>
>>It seems to me that explicit is used when you want to force your API 
>>clients to use a cast when dealing with different types.  So lets say 
>>that I have:
>>
>>public static implicit operator Foo(Bar bar) {...}    and I also have a 
>>method called
>>public Foo morph(Foo foo) {...}
>>
>>If I was ever to use my morph method with a Bar, I could issue the call 
>>like:
>>
>>Bar bee = new Bee();
>>Foo faa = morph(bee);
>>
>>And the compiler would be happy.  If I was to change implicit for 
>>explicit in the operator's declaration, the way of calling the method 
>>would be:
>>
>>Foo faa = morph((Foo) bee);   // with explicit cast
>>
>>Is this it, or is there more to it than this?  I also am aware that this 
>>isn't really a "mono" question per se, but I thought some of you would 
>>be willing to shed some light on the topic...  :-)
>>
>>Thanks!
>>
>>// David
>>
>>_______________________________________________
>>Mono-list maillist  -  Mono-list@lists.ximian.com
>>http://lists.ximian.com/mailman/listinfo/mono-list
>>    
>>
>
>  
>

--------------050506000809050306040000
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
  <title></title>
</head>
<body text="#000000" bgcolor="#ffffff">
Thanks for the explanation.&nbsp; I can see how the implicit operator can be
useful in the example you describe; still, I think it wasn't necessary
for C# to expose them to us.&nbsp; I.e., let us deal with the explicit casts
and spare the confusion they may cause.&nbsp; In other words, an implicit
cast from a Pear object to a Truck object can seem quite odd, assuming
their inheritance tree has nothing in common.<br>
<br>
// David<br>
<br>
Jonathan Pryor wrote:<br>
<blockquote type="cite" cite="mid1066342412.339.27.camel@melchior.magi">
  <pre wrap="">It's good to keep this in mind: C#'s "builtin" types (int, long, etc.)
are actually aliases for managed types (System.Int32, System.Int64,
etc.).  These managed types are (for Mono, at least) written in C#.

You expect the following code to work:

	int n = 42;
	long l = n;
	short s = (short) n;

Which means that the following code also works:

	System.Int32 n = 42;
	System.Int64 l = n;
	System.Int16 s = (System.Int16) n;

Which means that the managed implementation of those managed types needs
*some way* to represent to the compiler/runtime that some coercions are
"safe" (can be done implicitly), while others are "unsafe" (can be done
explicitly).

C#'s implicit/explicit operators are how this is declared and defined.

As for the differences between implicit &amp; explicit, you understand the
difference already.  Implicit == doesn't need a cast; explicit ==
requires a cast.

 - Jon

On Thu, 2003-10-16 at 16:52, David La Motta wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">So I was reading my "C# for Java Developers" book and I came across the 
implicit and explicit operators.  Java doesn't have these but a friend 
suggests C++ does.  Being that I am not a C++ developer I really can't 
comment much on it, except to say that it looks like C# just decided to 
copy functionality from C++, just because it is a "cute" feature of the 
language.  I also can't quite put my finger on the difference between 
implicit and explicit, so if anybody has any insight on these, please, 
do share.

It seems to me that explicit is used when you want to force your API 
clients to use a cast when dealing with different types.  So lets say 
that I have:

public static implicit operator Foo(Bar bar) {...}    and I also have a 
method called
public Foo morph(Foo foo) {...}

If I was ever to use my morph method with a Bar, I could issue the call 
like:

Bar bee = new Bee();
Foo faa = morph(bee);

And the compiler would be happy.  If I was to change implicit for 
explicit in the operator's declaration, the way of calling the method 
would be:

Foo faa = morph((Foo) bee);   // with explicit cast

Is this it, or is there more to it than this?  I also am aware that this 
isn't really a "mono" question per se, but I thought some of you would 
be willing to shed some light on the topic...  :-)

Thanks!

// David

_______________________________________________
Mono-list maillist  -  <a class="moz-txt-link-abbreviated" href="mailto:Mono-list@lists.ximian.com">Mono-list@lists.ximian.com</a>
<a class="moz-txt-link-freetext" href="http://lists.ximian.com/mailman/listinfo/mono-list">http://lists.ximian.com/mailman/listinfo/mono-list</a>
    </pre>
  </blockquote>
  <pre wrap=""><!---->
  </pre>
</blockquote>
</body>
</html>

--------------050506000809050306040000--