зеркало из https://github.com/mono/mail-archives.git
197 строки
8.0 KiB
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:
|
||
|
||
><i>Actually, I must say that I am glad the implicit/explicit operators are
|
||
</I>><i>defined.
|
||
</I>><i>
|
||
</I>><i>For example, let's say you have a class that does a lot of stuff, but
|
||
</I>><i>also contains a Uri. Now, it is nice to be able to say if(UriContainer
|
||
</I>><i>== someUri) without having to worry about the casting.
|
||
</I>><i>
|
||
</I>><i>Or, for a real world example, take the WSE2 Addressing class Action. It
|
||
</I>><i>is a class that represents an XmlElement (Note, this class does *not*
|
||
</I>><i>inherit from an XmlElement at all). The InnerText of this xml element is
|
||
</I>><i>a string that contains the 'action' of the Soap Addressing Header. With
|
||
</I>><i>implicit operators it is possible to do this:
|
||
</I>><i>
|
||
</I>><i> switch(ActionObject) {
|
||
</I>><i> case "urn:test:action1":
|
||
</I>><i> //Your code here.
|
||
</I>><i> break;
|
||
</I>><i> case "urn:test:action2":
|
||
</I>><i> //Your code here.
|
||
</I>><i> break;
|
||
</I>><i> }
|
||
</I>><i>
|
||
</I>><i>Admittedly, there are for sure other ways to accomplish the same goal,
|
||
</I>><i>but none are nearly as readable as that.
|
||
</I>><i>
|
||
</I>><i>Now, the language could have been set up to just make all operators
|
||
</I>><i>implicit, and again, I am glad they didn<64>t do that, because it allows me
|
||
</I>><i>as a API programmer to construct my API in such a fashion that its
|
||
</I>><i>somewhat self documenting to the end developer.
|
||
</I>><i>
|
||
</I>><i>--Todd
|
||
</I>><i>
|
||
</I>><i>-----Original Message-----
|
||
</I>><i>From: <A HREF="mailto:mono-list-admin@lists.ximian.com">mono-list-admin@lists.ximian.com</A>
|
||
</I>><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>><i>Sent: October 17, 2003 9:23 AM
|
||
</I>><i>To: Jonathan Pryor
|
||
</I>><i>Cc: Mono-List
|
||
</I>><i>Subject: Re: [Mono-list] implicit, explicit, and why does C# have these?
|
||
</I>><i>
|
||
</I>><i>Thanks for the explanation. I can see how the implicit operator can be
|
||
</I>><i>useful in the example you describe; still, I think it wasn't necessary
|
||
</I>><i>for C# to expose them to us. I.e., let us deal with the explicit casts
|
||
</I>><i>and spare the confusion they may cause. In other words, an implicit
|
||
</I>><i>cast from a Pear object to a Truck object can seem quite odd, assuming
|
||
</I>><i>their inheritance tree has nothing in common.
|
||
</I>><i>
|
||
</I>><i>// David
|
||
</I>><i>
|
||
</I>><i>Jonathan Pryor wrote:
|
||
</I>><i>
|
||
</I>><i>It's good to keep this in mind: C#'s "builtin" types (int, long, etc.)
|
||
</I>><i>are actually aliases for managed types (System.Int32, System.Int64,
|
||
</I>><i>etc.). These managed types are (for Mono, at least) written in C#.
|
||
</I>><i>
|
||
</I>><i>You expect the following code to work:
|
||
</I>><i>
|
||
</I>><i> int n = 42;
|
||
</I>><i> long l = n;
|
||
</I>><i> short s = (short) n;
|
||
</I>><i>
|
||
</I>><i>Which means that the following code also works:
|
||
</I>><i>
|
||
</I>><i> System.Int32 n = 42;
|
||
</I>><i> System.Int64 l = n;
|
||
</I>><i> System.Int16 s = (System.Int16) n;
|
||
</I>><i>
|
||
</I>><i>Which means that the managed implementation of those managed types needs
|
||
</I>><i>*some way* to represent to the compiler/runtime that some coercions are
|
||
</I>><i>"safe" (can be done implicitly), while others are "unsafe" (can be done
|
||
</I>><i>explicitly).
|
||
</I>><i>
|
||
</I>><i>C#'s implicit/explicit operators are how this is declared and defined.
|
||
</I>><i>
|
||
</I>><i>As for the differences between implicit & explicit, you understand the
|
||
</I>><i>difference already. Implicit == doesn't need a cast; explicit ==
|
||
</I>><i>requires a cast.
|
||
</I>><i>
|
||
</I>><i> - Jon
|
||
</I>><i>
|
||
</I>><i>On Thu, 2003-10-16 at 16:52, David La Motta wrote:
|
||
</I>><i>
|
||
</I>><i>So I was reading my "C# for Java Developers" book and I came across the
|
||
</I>><i>implicit and explicit operators. Java doesn't have these but a friend
|
||
</I>><i>suggests C++ does. Being that I am not a C++ developer I really can't
|
||
</I>><i>comment much on it, except to say that it looks like C# just decided to
|
||
</I>><i>copy functionality from C++, just because it is a "cute" feature of the
|
||
</I>><i>language. I also can't quite put my finger on the difference between
|
||
</I>><i>implicit and explicit, so if anybody has any insight on these, please,
|
||
</I>><i>do share.
|
||
</I>><i>
|
||
</I>><i>It seems to me that explicit is used when you want to force your API
|
||
</I>><i>clients to use a cast when dealing with different types. So lets say
|
||
</I>><i>that I have:
|
||
</I>><i>
|
||
</I>><i>public static implicit operator Foo(Bar bar) {...} and I also have a
|
||
</I>><i>method called
|
||
</I>><i>public Foo morph(Foo foo) {...}
|
||
</I>><i>
|
||
</I>><i>If I was ever to use my morph method with a Bar, I could issue the call
|
||
</I>><i>like:
|
||
</I>><i>
|
||
</I>><i>Bar bee = new Bee();
|
||
</I>><i>Foo faa = morph(bee);
|
||
</I>><i>
|
||
</I>><i>And the compiler would be happy. If I was to change implicit for
|
||
</I>><i>explicit in the operator's declaration, the way of calling the method
|
||
</I>><i>would be:
|
||
</I>><i>
|
||
</I>><i>Foo faa = morph((Foo) bee); // with explicit cast
|
||
</I>><i>
|
||
</I>><i>Is this it, or is there more to it than this? I also am aware that this
|
||
</I>><i>
|
||
</I>><i>isn't really a "mono" question per se, but I thought some of you would
|
||
</I>><i>be willing to shed some light on the topic... :-)
|
||
</I>><i>
|
||
</I>><i>Thanks!
|
||
</I>><i>
|
||
</I>><i>// David
|
||
</I>><i>
|
||
</I>><i>_______________________________________________
|
||
</I>><i>Mono-list maillist - <A HREF="mailto:Mono-list@lists.ximian.com">Mono-list@lists.ximian.com</A>
|
||
</I>><i><A HREF="http://lists.ximian.com/mailman/listinfo/mono-list">http://lists.ximian.com/mailman/listinfo/mono-list</A>
|
||
</I>><i>
|
||
</I>><i>
|
||
</I>><i>
|
||
</I>><i>
|
||
</I>><i>
|
||
</I>><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>
|