Weakly Typed Languages |
Written by Nikos Vaggalis | ||||
Wednesday, 03 November 2010 | ||||
Page 3 of 3
Static typesNow, if we throw the static type system into the mix, having static and weak typing at the same time, we can risk serious data loss. Example 8 In Perl mixing ints and doubles is all right since there is no data loss from rounding or truncation (since the int is implicitly converted to a double): $a=10; However, in VB.NET such mixing is not as it implicitly converts 17.3 to 17. (Note that VB.NET rounds; for example if the value was 7.7 it would be rounded up to 8, while C# just truncates, i.e. 7.7 would become 7).
Example 9 Module Module1 VB does not warn of the possible data loss and just converts the value. In the next article we'll take a look at how C# would respond in a similar case, see Example 15. Imagine that you are faced with one of the following four scenarios where a COM+ server is accessed from a VB client using automation. The client passes a parameter of a certain type and at the server side the value is being converted to an improper one. Example 10 Here my_method(integer) is passed in a double; the double will be converted to an int because the method expects an int: Module Module1
Example 11 Here my_method(string) is passed in a double; the double will be converted to a string. Note that 7,3 is a string and not the number 7.3: Module Module1 Public Class MyClass1 Example 12 Module Module1 Public Class MyClass1 Example 13 Or try adding a string with a double using "&": Module Module1 Public Class MyClass1 (Note that the VB.NET examples were executed with Option Strict Off which makes the language act weakly typed, while Option Strict On would make it act strongly typed and would have the same exact results as the C# equivalent examples. i.e. Example 9 would fail with "Option Strict On disallows implicit conversions from 'Double' to 'Integer") In the next part we'll look at strong typing and draw conclusions about its particular advantages and disadvantages. ReferencesSee the righthand bar above for full details. Perl Cookbook, 2nd Ed Programming Perl, 3rd Ed
Related Articles
Comments
or email your comment to: comments@i-programmer.info To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
<ASIN:1430227931> <ASIN:0596003137> <ASIN:0596000278> |
||||
Last Updated ( Friday, 09 November 2012 ) |