Archive for the ‘strong typing’ Category

Dotnet generics constraint

June 2nd, 2010

When working with generics in dotnet (like templates in C++) there is something called “constraint” that limits the type of generic you can use.

Say you have a method

AreEqual bool<T>(T x, T y)
{
return x == y;
}

It will stop in the compiler since T isn’t necessarily comparable.  So we have to make sure T is.

AreEqual bool<T>(T x, T y) where T:IComparable
{
return x == y;
}

This example requries T to implement IComparable; i.e. int and string but not necessarily Customer.

Other constraints are “struct” constraining T to value types like int and enums, “class” for contraining to classes or “new()” for requiring a default constructor (constructor without parameter).  One can also constrain to inherit from a class and even send this class as a parameter like T.

The constraints are limited and I have often stepped into limitations I cannot recall right now.

More info here: http://msdn.microsoft.com/en-us/library/d5x73970%28VS.90%29.aspx.

>List Find Delegate

January 5th, 2009

>

Here is a short example of how to use the Find method together with an anonymous delegate in C#.
(there are plenty of examples on the web but most forget to use an external variable)

var myExternalVariable = “Cool”;
List list = new List(new string[]    {“Cows”,”Are”,”Cool”});
var possibleWord =
    list.Find(delegate(string s)
        { return s == myExternalVariable; });

if you want to be even hairier, translate to lambda

var possibleWord = list.Find(s => s == myVariable);

Sort.

Volta and Script# – type safe javascript

December 11th, 2007

UPDATE 20201218: Volta never made it. Instead we today use Typescript.

This message will be repeated in english.

Så länge jag har känt mig själv har jag klagat på den svaga typningen i Javascript.
Jag kan förstå att implementationen är olika mellan olika webbläsare och nästan, men bara nästan, förstå Microsofts egocentriska volt med IE att göra en egen händelsemodell. Men jag kan inte förstå det strategiska beslutet av Netscape att göra ett svagt typat språk.

För några år sedan ramlade jag över en lösning, Script#, som kunde omvandla typsäker C#-kod till Javascript. Den var ett experiment så jag vågade aldrig ta in den i produktion och hade inte tid att leka med den. Vad jag inte visste förrän nyligen var att Microsoft har tyckt som jag.

Så nu har de lanserat Volta.

As long as I have known myself I have been irritated on the weak type model of Javascript.
I can understand that the implementation of Javascript differs between the web browsers. And I can almost understand Microsofts egocentric trick with Javascript in the earlier IE versions. But I have never understood Netscape’s reasoning behind making a language weakly typed.

Some years ago I stumbled over a solution, Script#, to convert type save C# code to Javascript. It was an experiment so I never dared to bring it into production even though I wanted to. What I didn’t know was that obviously someone at Microsoft thought like I.

So now they have launched Volta.

[Update: Bytte mjuk mot svag.]