<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: Optional parameters &#8211; Conclusion: Treat like &#8220;unsafe&#8221;</title> <atom:link href="http://www.fsmpi.uni-bayreuth.de/~dun3/archives/optional-parameters-conclusion-treat-like-unsafe/216.html/feed" rel="self" type="application/rss+xml" /><link>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/optional-parameters-conclusion-treat-like-unsafe/216.html</link> <description>This is a collection of interesting stuff that I found on the web or stuff I worked on at the time.</description> <lastBuildDate>Tue, 07 Feb 2012 20:01:32 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.1</generator> <item><title>By: Tobias Hertkorn</title><link>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/optional-parameters-conclusion-treat-like-unsafe/216.html/comment-page-1#comment-301044</link> <dc:creator>Tobias Hertkorn</dc:creator> <pubDate>Thu, 12 Feb 2009 12:08:26 +0000</pubDate> <guid
isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=216#comment-301044</guid> <description>Hi Tum,I&#039;m glad that the style suits you. Though I would argue that it is a direct violation of the Liskov substitution principle. Just imagine:var myVariable = Api.GetMyVariable();
var myMethodResult = myVariable.GetResult();And GetResult() being defined in IMyVariable as GetResult(int test = 2) and in MyVariable as GetResult(int test = 1)Now the Api.GetMyVariable() changes from IMyVariable GetMyVariable() to MyVariable GetMyVariable() which is a valid substitution.
Unfortunatelly your var myMethodResult = myVariable.GetResult(); now changes its meaning without you even noticing.Fun!</description> <content:encoded><![CDATA[<p>Hi Tum,</p><p>I&#8217;m glad that the style suits you. Though I would argue that it is a direct violation of the Liskov substitution principle. Just imagine:</p><p>var myVariable = Api.GetMyVariable();<br
/> var myMethodResult = myVariable.GetResult();</p><p>And GetResult() being defined in IMyVariable as GetResult(int test = 2) and in MyVariable as GetResult(int test = 1)</p><p>Now the Api.GetMyVariable() changes from IMyVariable GetMyVariable() to MyVariable GetMyVariable() which is a valid substitution.<br
/> Unfortunatelly your var myMethodResult = myVariable.GetResult(); now changes its meaning without you even noticing.</p><p>Fun!</p> ]]></content:encoded> </item> <item><title>By: Tum</title><link>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/optional-parameters-conclusion-treat-like-unsafe/216.html/comment-page-1#comment-298193</link> <dc:creator>Tum</dc:creator> <pubDate>Sat, 31 Jan 2009 00:24:31 +0000</pubDate> <guid
isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=216#comment-298193</guid> <description>The output is exactly as I expected.  Optional parameters are not polymorphic.  The compiler inserts them at compile time which means that you&#039;ll get the default value declared with the only default known at compile time.  Making default values work at runtime would be crazy and hurt performance.Like any other feature, as long as you know what is really happening, it makes sense.  Default values are only there for convenience and the default you get will be exactly the default you know about at compile time (the default as declared on the variable type you are calling upon).  This also means that intellisense will be able to let you know what value is is substituting as the default for you.</description> <content:encoded><![CDATA[<p>The output is exactly as I expected.  Optional parameters are not polymorphic.  The compiler inserts them at compile time which means that you&#8217;ll get the default value declared with the only default known at compile time.  Making default values work at runtime would be crazy and hurt performance.</p><p>Like any other feature, as long as you know what is really happening, it makes sense.  Default values are only there for convenience and the default you get will be exactly the default you know about at compile time (the default as declared on the variable type you are calling upon).  This also means that intellisense will be able to let you know what value is is substituting as the default for you.</p> ]]></content:encoded> </item> <item><title>By: Koushik Dutta</title><link>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/optional-parameters-conclusion-treat-like-unsafe/216.html/comment-page-1#comment-297650</link> <dc:creator>Koushik Dutta</dc:creator> <pubDate>Tue, 27 Jan 2009 22:23:56 +0000</pubDate> <guid
isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=216#comment-297650</guid> <description>This behavior makes sense.
Optional parameters (at least in C++, etc) are automatically filled in at compile time, by using the type of the variable that the method is on. The inheritance tree and such are not considered.For the same reason if you had the following, you would not be able to use optional parameters on Derived2.Test, even though Base.Test supports it:public class Derived2 : Base
{
public override string Test(int test)
{
return &quot;Derived &quot; + test.ToString();
}
}</description> <content:encoded><![CDATA[<p>This behavior makes sense.<br
/> Optional parameters (at least in C++, etc) are automatically filled in at compile time, by using the type of the variable that the method is on. The inheritance tree and such are not considered.</p><p>For the same reason if you had the following, you would not be able to use optional parameters on Derived2.Test, even though Base.Test supports it:</p><p>public class Derived2 : Base<br
/> {<br
/> public override string Test(int test)<br
/> {<br
/> return &#8220;Derived &#8221; + test.ToString();<br
/> }<br
/> }</p> ]]></content:encoded> </item> <item><title>By: RednaxelaFX</title><link>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/optional-parameters-conclusion-treat-like-unsafe/216.html/comment-page-1#comment-278250</link> <dc:creator>RednaxelaFX</dc:creator> <pubDate>Wed, 12 Nov 2008 03:36:15 +0000</pubDate> <guid
isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=216#comment-278250</guid> <description>Hey,So I tried the same code in C++, and surprisingly (or maybe not), the behavior is exactly the same as C# 4 in CTP. Is this a tradition or something...Cheers,
- RednaxelaFX (Kris Mok)</description> <content:encoded><![CDATA[<p>Hey,</p><p>So I tried the same code in C++, and surprisingly (or maybe not), the behavior is exactly the same as C# 4 in CTP. Is this a tradition or something&#8230;</p><p>Cheers,<br
/> - RednaxelaFX (Kris Mok)</p> ]]></content:encoded> </item> <item><title>By: Tobias Hertkorn</title><link>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/optional-parameters-conclusion-treat-like-unsafe/216.html/comment-page-1#comment-278097</link> <dc:creator>Tobias Hertkorn</dc:creator> <pubDate>Tue, 11 Nov 2008 16:38:19 +0000</pubDate> <guid
isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=216#comment-278097</guid> <description>Hey Kris,good call, trying out the VB version. And it makes more sense when looking at the VB implementation of the feature. That does get rid of a lot of the problems. Maybe since we are looking at the CTP of C# 4.0 they will &quot;fix&quot; it down the line.Cheers,
Tobi</description> <content:encoded><![CDATA[<p>Hey Kris,</p><p>good call, trying out the VB version. And it makes more sense when looking at the VB implementation of the feature. That does get rid of a lot of the problems. Maybe since we are looking at the CTP of C# 4.0 they will &#8220;fix&#8221; it down the line.</p><p>Cheers,<br
/> Tobi</p> ]]></content:encoded> </item> <item><title>By: RednaxelaFX</title><link>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/optional-parameters-conclusion-treat-like-unsafe/216.html/comment-page-1#comment-277980</link> <dc:creator>RednaxelaFX</dc:creator> <pubDate>Tue, 11 Nov 2008 07:27:55 +0000</pubDate> <guid
isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=216#comment-277980</guid> <description>Hi Tobi,This does look terrible to me. Having known that VB.NET has had the optional parameter feature for a long time, I decided to try the same piece of code in VB.NET and see what happens. Darn, I just don&#039;t know enough VB to get it right for the first time.Class Derived
&#160;&#160;&#160;&#160;Inherits Base
&#160;&#160;&#160;&#160;Public Overrides Function Test( Optional t As Integer = 1 ) As String
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Return &quot;Derived &quot; + t.ToString()
&#160;&#160;&#160;&#160;End Function
End ClassClass Base
&#160;&#160;&#160;&#160;Implements IBase
&#160;&#160;&#160;&#160;Public Overridable Function Test( Optional t As Integer = 1 ) As String Implements IBase.Test
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Return &quot;Base &quot; + t.ToString()
&#160;&#160;&#160;&#160;End Function
End ClassInterface IBase
&#160;&#160;&#160;&#160;Function Test( Optional t As Integer = 1 ) As String
End InterfaceModule Program
&#160;&#160;&#160;&#160;Sub Main()
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim b As Base = New Base()
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;System.Console.WriteLine( b.Test() )
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim bi As IBase = b
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;System.Console.WriteLine( bi.Test() )
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim d As Derived = New Derived()
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;System.Console.WriteLine( d.Test() )
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim d2 As Base = d
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;System.Console.WriteLine( d2.Test() )
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Dim d2i As IBase = d2
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;System.Console.WriteLine( d2i.Test() )
&#160;&#160;&#160;&#160;End Sub
End ModuleVB.NET&#039;s compiler will complain if the default values of optional parameters don&#039;t match, which eliminates the problem you had in the C# 4 code. But then of course, you won&#039;t be able to use different default values in the inheritance chain, which might be bad, or good, depends on what you&#039;re trying to do.
Not sure whether creating a compiler flag for optional parameters is a good idea. Maybe for safety sakes we should suggest C# 4&#039;s optional parameters to behave similiar to VB.NET&#039;s...- RednaxelaFX (Kris Mok)</description> <content:encoded><![CDATA[<p>Hi Tobi,</p><p>This does look terrible to me. Having known that VB.NET has had the optional parameter feature for a long time, I decided to try the same piece of code in VB.NET and see what happens. Darn, I just don&#8217;t know enough VB to get it right for the first time.</p><p>Class Derived<br
/> &nbsp;&nbsp;&nbsp;&nbsp;Inherits Base<br
/> &nbsp;&nbsp;&nbsp;&nbsp;Public Overrides Function Test( Optional t As Integer = 1 ) As String<br
/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return &#8220;Derived &#8221; + t.ToString()<br
/> &nbsp;&nbsp;&nbsp;&nbsp;End Function<br
/> End Class</p><p>Class Base<br
/> &nbsp;&nbsp;&nbsp;&nbsp;Implements IBase<br
/> &nbsp;&nbsp;&nbsp;&nbsp;Public Overridable Function Test( Optional t As Integer = 1 ) As String Implements IBase.Test<br
/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return &#8220;Base &#8221; + t.ToString()<br
/> &nbsp;&nbsp;&nbsp;&nbsp;End Function<br
/> End Class</p><p>Interface IBase<br
/> &nbsp;&nbsp;&nbsp;&nbsp;Function Test( Optional t As Integer = 1 ) As String<br
/> End Interface</p><p>Module Program<br
/> &nbsp;&nbsp;&nbsp;&nbsp;Sub Main()<br
/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim b As Base = New Base()<br
/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Console.WriteLine( b.Test() )<br
/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim bi As IBase = b<br
/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Console.WriteLine( bi.Test() )<br
/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim d As Derived = New Derived()<br
/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Console.WriteLine( d.Test() )<br
/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim d2 As Base = d<br
/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Console.WriteLine( d2.Test() )<br
/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim d2i As IBase = d2<br
/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Console.WriteLine( d2i.Test() )<br
/> &nbsp;&nbsp;&nbsp;&nbsp;End Sub<br
/> End Module</p><p>VB.NET&#8217;s compiler will complain if the default values of optional parameters don&#8217;t match, which eliminates the problem you had in the C# 4 code. But then of course, you won&#8217;t be able to use different default values in the inheritance chain, which might be bad, or good, depends on what you&#8217;re trying to do.<br
/> Not sure whether creating a compiler flag for optional parameters is a good idea. Maybe for safety sakes we should suggest C# 4&#8242;s optional parameters to behave similiar to VB.NET&#8217;s&#8230;</p><p>- RednaxelaFX (Kris Mok)</p> ]]></content:encoded> </item> </channel> </rss>
<!-- This site's performance optimized by W3 Total Cache. Dramatically improve the speed and reliability of your blog!

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)
Database Caching using disk

Served from: btfmx2.fs.uni-bayreuth.de @ 2012-02-12 06:43:05 -->
