Strong Typing
Written by Nikos Vaggalis   
Thursday, 18 November 2010
Article Index
Strong Typing
Using conversion classes
Perl
Casting
Implicit conversion
Altering representation

Conversion classes

The previous example highlights the importance of widening and narrowing conversions between related types. However this does not mean that strongly typed languages cannot convert between unrelated types.

This can be achieved by using conversion classes such as System.Convert :

string mystring = "10";
int myint = Convert.ToInt32(s);

or by extending the language by providing user defined implicit or explicit conversions from and to:

  • predefined types and user defined types
  • user defined types and user defined types

or by operator overloading.

Let's take a look at operator overloading in C#:

Example 17

using System;
namespace ConsoleApplication2
{
 class Program
{
static void Main(string[] args)
{
double a = 7.3;
string x = "hi";
x = a + x;
System.Console.Write(x); //7.3hi
}
}
}

C# turned weakly typed?

How could this implicit conversion, in example 17, from double to string be possible with a strongly typed language?

Let's a take a look at what the MSDN Library has to say on the  "+" operator :

“Binary + operators are predefined for numeric and string types. For numeric types, + computes the sum of its two operands. When one or both operands are of type string, + concatenates the string representations of the operands"

There is a built-in overload of the plus operator which can make it work with numbers and strings, and that is why I deliberately used the minus '-' operator, which is not overloaded, in an earlier C# example. In the earlier Example 5 using the '+' operator could lead to wrong assumptions - that C# is weakly typed instead of strongly, although it acted as a weak type in that case.

However, if we turn the left value of the assignment from string to double:

a = a + x;

we now get a compile time error:

Error 1  Cannot implicitly convert type 'string' to 'double'

C# being a static language has all the type information beforehand and can check the validity of the statement at compile time.

In this case we have a string and double - can we add them? It seems the “+” operator overload allows that but the result of the addition is a string; may we then assign the result to a double variable? No, we may not. It's complicated - and if you don't believe me try explaining this strange set of rules to a beginner!

An untyped language by nature does not have compile time type information but this does not mean that it does not do any type checks; those checks are deferred to until runtime since the type information is not available until then.

The equivalent of the C# Example 5 with the subtraction operator '-', in VB.NET would compile successfully and fail at runtime:

Module Module1
Sub Main()
Dim a As Double = 7.3
Dim x As String = "hi"
a = x - a
System.Console.Write(a)
End Sub
End Module

C# Perl-like behavior?

Example 18

If you need Perl-like dynamic behavior from C# you could alter the previous example by using the dynamic type as:

using System;
namespace ConsoleApplication2
{
 class Program
 {
  static void Main(string[] args)
  {
dynamic a = 7.3;
dynamic x = "hi";
double c = a + x;
System.Console.Write(c);
}
}
}

which produces the runtime error:

double c = a + x;
//Cannot implicitly convert type 'string' to 'double'

It does compile but fails at runtime since by using the dynamic type, all type checking is deferred until runtime where the actual types are discovered and subsequently the operation is checked for its validity

On the other hand, the following not only compiles but also executes with no runtime exception since the DLR infers that the type of variable 'd' should be that of a string:

dynamic d = a + x; //7.3hi

And as a matter of fact, we can get to the actual runtime type assigned to the variable with:

System.Console.Write(d.GetType()); 
//System.String

(As a side note, the 'dynamic' keyword which is bound to a runtime mechanism , is very different from the type inference 'var' keyword which is bound to a compile time mechanism.)

<ASIN:143022455X>

<ASIN:0596800959>

<ASIN:0321658701>



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.