Weakly Typed Languages
Written by Nikos Vaggalis   
Wednesday, 03 November 2010
Article Index
Weakly Typed Languages
VB.NET weak typing
Problems from static typing

Static types

Now, 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;
$b=7.3;
$c=$a+$b;
print $c; #17.3

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
Sub Main()
Dim a As Integer = 10
Dim b As Double = 7.3
Dim c As Integer = a + b
System.Console.Write(c) ''17
System.Console.Read()
End Sub
End Module

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
Sub Main()
Dim obj As New MyClass1()
Dim a As Double = 7.3
obj.my_method(a)
System.Console.Read()
End Sub
End Module

Public Class MyClass1
Sub my_method(ByVal x As Integer)
Dim b As Integer
b = x
System.Console.Write(b) ‘’7
End Sub
End Class

 

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
Sub Main()
Dim obj As New MyClass1()
Dim a As Double = 7.3
obj.my_method(a)
System.Console.Read()
End Sub
End Module
Public Class MyClass1
Sub my_method(ByVal x As String)
System.Console.Write(x) ‘’7,3
End Sub
End Class

Example 12
Now try adding a string with a double using concatentation:

Module Module1
Sub Main()
Dim obj As New MyClass1()
Dim a As Double = 7.3
obj.my_method(a)
System.Console.Read()
End Sub
End Module
Public Class MyClass1
Sub my_method(ByVal x As Double)
Dim b As String
b = String.Concat("hi", x)
System.Console.Write(b)‘’hi7,3
End Sub
End Class

Example 13

Or try adding a string with a double using "&":

Module Module1
Sub Main()
Dim obj As New MyClass1()
Dim a As Double = 7.3
obj.my_method(a)
System.Console.Read()
End Sub
End Module
Public Class MyClass1
Sub my_method(ByVal x As Double)
Dim b As String
b = "hi" & x
System.Console.Write(b)‘’hi7,3
End Sub
End Class

(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.

References

See the righthand bar above for full details.

Perl Cookbook, 2nd Ed

Programming Perl, 3rd Ed

 

Related Articles

Type Systems Demystified

Strong Typing

 

blog comments powered by Disqus

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.

Banner

 

<ASIN:1430227931>

<ASIN:0596003137>

<ASIN:0596000278>



Last Updated ( Friday, 09 November 2012 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.