Overloading in TypeScript - part 1

April 8, 2013 at 6:12 PMMichele Mottini

Coming from a C#/C++ background I was baffled by the handling of overloading in TypeScript. Having to write a class that could be initialized from either a date or a year/month/day triplet I went for two overloaded constructors:

class DateHour {

  private date: Date;
  private relativeHour: number;

  constructor (year: number, month: number, day: number, relativeHour: number) {
    this.date = new Date(year, month, day);
    this.relativeHour = relativeHour;
  }

  constructor (date: Date, relativeHour: number) {
    this.date = new Date(date.getFullYear(), date.getMonth(), date.getDate());
    this.relativeHour = relativeHour;
  }

}

...that does not work: TypeScript does not accept the two different constructors. The specifications do mention overloading though, so what gives?

The problem is that JavaScript does not know anything about overloading - and so TypeScript cannot generate multiple definitions of the same function differing only by their signature.

What TypeScript can do is to define multiple overloads that all map to the same function body; the class above should be defined like this:

class DateHour {

  private date: Date;
  private relativeHour: number;

  constructor(year: number, month: number, day: number, relativeHour: number);
  constructor(date: Date, relativeHour: number);
  constructor(dateOrYear: any, monthOrRelativeHour: number, day?: number, relativeHour?: number) {
    if (typeof dateOrYear === "number") {
      this.date = new Date(dateOrYear, monthOrRelativeHour, day);
      this.relativeHour = relativeHour;
    } else {
      var date = <Date> dateOrYear;
      this.date = new Date(date.getFullYear(), date.getMonth(), date.getDate());
      this.relativeHour = monthOrRelativeHour;
    }
  }
}

The two overloads are still there, but as simple signature declarations, and the actual implementation of the constructor accepts a superset of the parameters of the two overloads - and determine what to do checking the type of the first parameters.

The clever bit is that in such a case the overloaded signatures hide the implementation one - auto-complete shows only two constructors: 

 and these calls are marked as invalid by the TypeScript compiler:

var start = new DateHour(2013, 0);

var current = new DateHour(new Date(), 1, 4, currentRelativeHour);

even if they match the constructor implementation. The class behaves exactly as if it had the two constructors defined in my first failed attempt.

 Part 2

 

Posted in: Programming

Tags: ,

Setting up the blog

April 6, 2013 at 12:09 PMMichele Mottini

Besides writing the blog I'd like to play a bit with the platform itself, so I wanted blogging software I can work with and not just a shrink-wrapped hosted solution.

I am a .NET/C#/Windows developer, so I looked for .NET-based stuff.

At first I tried FunnelWeb: it is a modern .NET MVC application, geared for programmers (uses Markdown for the posts for example).  It is quite basic in terms of features, requires a SQL database and the community / support does not seem very active, so I dropped it.

I switched to BlogEngine.Net: it is not MVC, but it is a more mature and full-featured product, with a bigger and more active community. As a further plus (for me) it does not require a SQL database.

BlogEngine.NET is the brainchild of Mads Kristensen, the author of the - essential - Web Essentials Visual Studio add-in.

Selected the software, I needed a hosting service. Some initial searches led me to this gallery of hosting services supporting .NET; after some further not-very-scientific searches there I settled to SmarterAsp.NET. The amount of choice is quite overwhelming: I was not expecting so many different hosting services.

So far I am happy with SmarterAsp: they offer a 60 day trial, the setup was easy and the dashboard used  to manage your sites is pretty nice. It might be a tad slow, I am not sure yet.

SmarterAsp offers a number of pre-configured application that can be installed automatically - including BlogEngine.NET, but for some reason their installation script configures it to use a SQL database instead than the file system. That seems overkill for my needs, so I ended up doing a manual installation, that in any case is very easy.

Last step: select a domain name. michelemottini.com seemed too long, I frequently use 'mimo' as my handle, but mimo.com is already taken, so I went for mimosite.com. I bough it directly via SmarterAsp, and I was good to go almost immediately.

 

Posted in: Blogging

Tags:

Welcome

April 3, 2013 at 6:52 PMMichele Mottini

Welcome to my brand new blog!

I have been an avid reader of various blogs - technical and otherwise - for several years now, I thought to have a stab at writing something myself.

The idea is to write prevalently about programming and technology, possibly with a bit of physics, book reviews and 'opinion pieces'.

We'll see how it goes.

Posted in: Blogging

Tags: