DCSIMG
Contextual Extension Methods - Technicals and Technicalities

Technicals and Technicalities

Ariel's uneditable Bliki

Contextual Extension Methods

Me like!

 

int sleepTime = 1.Seconds();
while (condition)
{
    Thread.Sleep(sleepTime*=2); 
    if(sleepTime>10.Minutes())
    {
        //give up
        return;
    }
}
DoWork();

 

And no, those extensions are not defined under the System namespace.

Comments

shayf said:

Ruby on Rails had those years ago!

# December 14, 2009 1:40 PM

Avi Pinto said:

nice one

# December 16, 2009 11:09 AM

Adrian said:

(int sleepTime = 1.Seconds();

while (condition)

{

   Thread.Sleep(sleepTime*=2);

   if(sleepTime>10.Minutes())

   {

       //give up

       return;

   }

}

DoWork();).DontLike();

# December 17, 2009 12:01 PM

Adrian said:

No no. Wait.

You'll wait 17 minutes until giving up.

You'll wait 1 second, then another 2, then another 4, after 9 iterations, you'll wait 8.5 minutes. After the 10th - you've waited 17 minutes.

# December 17, 2009 12:08 PM

Ariel said:

Adrian - I really don't care about the algorithm. It's the extenstion methods thing.

And if we are talking algorithm - it doesn't matter - the time cap is just a way to "give up" after some amount of time. I don't plan on really getting to it, nor do I mind if it's not exact.

It could have been written with a timer which simply times how long it was "trying", if that was an issue.

What about SCMM?

# December 20, 2009 1:51 AM

Adrian said:

Ariel - I don't care that you don't care about the algorithm. I'm adding interesting content to your blog.

Thinking about the trivial algorithm and finding out that it has a bug - is more interesting than the extension method.

SCMM - You'll be the first to know about it - and get a free life-time license.

# December 20, 2009 9:55 AM

Dew Drop – December 27, 2009 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop – December 27, 2009 | Alvin Ashcraft's Morning Dew

# December 28, 2009 12:49 AM

James Michael Hare said:

The only thing I don't know if I like is that it relies on the fact that many of the methods that will be consuming the return from these extensions are geared towards milliseconds.  If you have a method that takes a timeout in seconds instead of milliseconds, you could inadvertently introduce a bug.

Perhaps (though long winded as it may be) something like:

10.MinutesToMs()

To avoid confusion?

or 10.Minutes(Time.Milliseconds)

I know they're a bit longer, but would reduce any ambiguity.

# December 31, 2009 6:48 PM

James Michael Hare said:

Aother thing you could do off integer would be to create an extension to in effect power a foreach loop:

10.Times(() => { Console.WriteLine("Hello"); });

# December 31, 2009 6:51 PM

Ariel said:

James -

I agree that the unit of measurement is unclear, and that's why the "contextual" is in the title.

I wouldn't like to have an extension method on int called "Minutes", but is the scope of my class here (or even in the scope of System.Threading") - this could be the correct usage.

I totally agree that this should not be available everywhere.

# January 9, 2010 12:31 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: