mail-archives/mono-list/2003-October/016484.html

197 строки
8.0 KiB
HTML
Исходник Постоянная ссылка Ответственный История

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE> [Mono-list] implicit, explicit, and why does C# have these?
</TITLE>
<LINK REL="Index" HREF="index.html" >
<LINK REL="made" HREF="mailto:dlamotta%40email.com">
<META NAME="robots" CONTENT="index,nofollow">
<LINK REL="Previous" HREF="016481.html">
<LINK REL="Next" HREF="016482.html">
</HEAD>
<BODY BGCOLOR="#ffffff">
<H1>[Mono-list] implicit, explicit, and why does C# have these?
</H1>
<B>David La Motta
</B>
<A HREF="mailto:dlamotta%40email.com"
TITLE="[Mono-list] implicit, explicit, and why does C# have these?">dlamotta@email.com
</A><BR>
<I>Fri, 17 Oct 2003 09:59:39 -0400</I>
<P><UL>
<LI> Previous message: <A HREF="016481.html">[Mono-list] implicit, explicit, and why does C# have these?
</A></li>
<LI> Next message: <A HREF="016482.html">[Mono-list] implicit, explicit, and why does C# have these?
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#16484">[ date ]</a>
<a href="thread.html#16484">[ thread ]</a>
<a href="subject.html#16484">[ subject ]</a>
<a href="author.html#16484">[ author ]</a>
</LI>
</UL>
<HR>
<!--beginarticle-->
<PRE>I like the == example better than the switch example--because that's
what toString() is for ;-)
Thanks for the examples...
Implicit and explicit have become clearer in my mind, which is what the
book I was reading didn't accomplish.
// David
Todd Berman wrote:
&gt;<i>Actually, I must say that I am glad the implicit/explicit operators are
</I>&gt;<i>defined.
</I>&gt;<i>
</I>&gt;<i>For example, let's say you have a class that does a lot of stuff, but
</I>&gt;<i>also contains a Uri. Now, it is nice to be able to say if(UriContainer
</I>&gt;<i>== someUri) without having to worry about the casting.
</I>&gt;<i>
</I>&gt;<i>Or, for a real world example, take the WSE2 Addressing class Action. It
</I>&gt;<i>is a class that represents an XmlElement (Note, this class does *not*
</I>&gt;<i>inherit from an XmlElement at all). The InnerText of this xml element is
</I>&gt;<i>a string that contains the 'action' of the Soap Addressing Header. With
</I>&gt;<i>implicit operators it is possible to do this:
</I>&gt;<i>
</I>&gt;<i> switch(ActionObject) {
</I>&gt;<i> case &quot;urn:test:action1&quot;:
</I>&gt;<i> //Your code here.
</I>&gt;<i> break;
</I>&gt;<i> case &quot;urn:test:action2&quot;:
</I>&gt;<i> //Your code here.
</I>&gt;<i> break;
</I>&gt;<i> }
</I>&gt;<i>
</I>&gt;<i>Admittedly, there are for sure other ways to accomplish the same goal,
</I>&gt;<i>but none are nearly as readable as that.
</I>&gt;<i>
</I>&gt;<i>Now, the language could have been set up to just make all operators
</I>&gt;<i>implicit, and again, I am glad they didn<64>t do that, because it allows me
</I>&gt;<i>as a API programmer to construct my API in such a fashion that its
</I>&gt;<i>somewhat self documenting to the end developer.
</I>&gt;<i>
</I>&gt;<i>--Todd
</I>&gt;<i>
</I>&gt;<i>-----Original Message-----
</I>&gt;<i>From: <A HREF="mailto:mono-list-admin@lists.ximian.com">mono-list-admin@lists.ximian.com</A>
</I>&gt;<i>[mailto:<A HREF="mailto:mono-list-admin@lists.ximian.com">mono-list-admin@lists.ximian.com</A>] On Behalf Of David La Motta
</I>&gt;<i>Sent: October 17, 2003 9:23 AM
</I>&gt;<i>To: Jonathan Pryor
</I>&gt;<i>Cc: Mono-List
</I>&gt;<i>Subject: Re: [Mono-list] implicit, explicit, and why does C# have these?
</I>&gt;<i>
</I>&gt;<i>Thanks for the explanation. I can see how the implicit operator can be
</I>&gt;<i>useful in the example you describe; still, I think it wasn't necessary
</I>&gt;<i>for C# to expose them to us. I.e., let us deal with the explicit casts
</I>&gt;<i>and spare the confusion they may cause. In other words, an implicit
</I>&gt;<i>cast from a Pear object to a Truck object can seem quite odd, assuming
</I>&gt;<i>their inheritance tree has nothing in common.
</I>&gt;<i>
</I>&gt;<i>// David
</I>&gt;<i>
</I>&gt;<i>Jonathan Pryor wrote:
</I>&gt;<i>
</I>&gt;<i>It's good to keep this in mind: C#'s &quot;builtin&quot; types (int, long, etc.)
</I>&gt;<i>are actually aliases for managed types (System.Int32, System.Int64,
</I>&gt;<i>etc.). These managed types are (for Mono, at least) written in C#.
</I>&gt;<i>
</I>&gt;<i>You expect the following code to work:
</I>&gt;<i>
</I>&gt;<i> int n = 42;
</I>&gt;<i> long l = n;
</I>&gt;<i> short s = (short) n;
</I>&gt;<i>
</I>&gt;<i>Which means that the following code also works:
</I>&gt;<i>
</I>&gt;<i> System.Int32 n = 42;
</I>&gt;<i> System.Int64 l = n;
</I>&gt;<i> System.Int16 s = (System.Int16) n;
</I>&gt;<i>
</I>&gt;<i>Which means that the managed implementation of those managed types needs
</I>&gt;<i>*some way* to represent to the compiler/runtime that some coercions are
</I>&gt;<i>&quot;safe&quot; (can be done implicitly), while others are &quot;unsafe&quot; (can be done
</I>&gt;<i>explicitly).
</I>&gt;<i>
</I>&gt;<i>C#'s implicit/explicit operators are how this is declared and defined.
</I>&gt;<i>
</I>&gt;<i>As for the differences between implicit &amp; explicit, you understand the
</I>&gt;<i>difference already. Implicit == doesn't need a cast; explicit ==
</I>&gt;<i>requires a cast.
</I>&gt;<i>
</I>&gt;<i> - Jon
</I>&gt;<i>
</I>&gt;<i>On Thu, 2003-10-16 at 16:52, David La Motta wrote:
</I>&gt;<i>
</I>&gt;<i>So I was reading my &quot;C# for Java Developers&quot; book and I came across the
</I>&gt;<i>implicit and explicit operators. Java doesn't have these but a friend
</I>&gt;<i>suggests C++ does. Being that I am not a C++ developer I really can't
</I>&gt;<i>comment much on it, except to say that it looks like C# just decided to
</I>&gt;<i>copy functionality from C++, just because it is a &quot;cute&quot; feature of the
</I>&gt;<i>language. I also can't quite put my finger on the difference between
</I>&gt;<i>implicit and explicit, so if anybody has any insight on these, please,
</I>&gt;<i>do share.
</I>&gt;<i>
</I>&gt;<i>It seems to me that explicit is used when you want to force your API
</I>&gt;<i>clients to use a cast when dealing with different types. So lets say
</I>&gt;<i>that I have:
</I>&gt;<i>
</I>&gt;<i>public static implicit operator Foo(Bar bar) {...} and I also have a
</I>&gt;<i>method called
</I>&gt;<i>public Foo morph(Foo foo) {...}
</I>&gt;<i>
</I>&gt;<i>If I was ever to use my morph method with a Bar, I could issue the call
</I>&gt;<i>like:
</I>&gt;<i>
</I>&gt;<i>Bar bee = new Bee();
</I>&gt;<i>Foo faa = morph(bee);
</I>&gt;<i>
</I>&gt;<i>And the compiler would be happy. If I was to change implicit for
</I>&gt;<i>explicit in the operator's declaration, the way of calling the method
</I>&gt;<i>would be:
</I>&gt;<i>
</I>&gt;<i>Foo faa = morph((Foo) bee); // with explicit cast
</I>&gt;<i>
</I>&gt;<i>Is this it, or is there more to it than this? I also am aware that this
</I>&gt;<i>
</I>&gt;<i>isn't really a &quot;mono&quot; question per se, but I thought some of you would
</I>&gt;<i>be willing to shed some light on the topic... :-)
</I>&gt;<i>
</I>&gt;<i>Thanks!
</I>&gt;<i>
</I>&gt;<i>// David
</I>&gt;<i>
</I>&gt;<i>_______________________________________________
</I>&gt;<i>Mono-list maillist - <A HREF="mailto:Mono-list@lists.ximian.com">Mono-list@lists.ximian.com</A>
</I>&gt;<i><A HREF="http://lists.ximian.com/mailman/listinfo/mono-list">http://lists.ximian.com/mailman/listinfo/mono-list</A>
</I>&gt;<i>
</I>&gt;<i>
</I>&gt;<i>
</I>&gt;<i>
</I>&gt;<i>
</I>&gt;<i>
</I>
</PRE>
<!--endarticle-->
<HR>
<P><UL>
<!--threads-->
<LI> Previous message: <A HREF="016481.html">[Mono-list] implicit, explicit, and why does C# have these?
</A></li>
<LI> Next message: <A HREF="016482.html">[Mono-list] implicit, explicit, and why does C# have these?
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#16484">[ date ]</a>
<a href="thread.html#16484">[ thread ]</a>
<a href="subject.html#16484">[ subject ]</a>
<a href="author.html#16484">[ author ]</a>
</LI>
</UL>
</body></html>