How much a hypothesis weight?

time to read 3 min | 431 words

Well, roughly 6Gb. That is the download that I had to go through to get Orcas. And I am not even that interested in Orcas itself at the moment. I just want to test a crazy idea that I had.

At any rate, I got the answer that I was looking for. The code below is not legal:

public class Foo
{
    [IsValid( x => x==null )]
    public string Name;
}
public class IsValidAttribute : Attribute
{
    public IsValidAttribute(Expression<StringPredicate> expr)
    {
    }
}

The errors are:

Error 1 The best overloaded method match for 'ConsoleApplication1.IsValidAttribute.IsValidAttribute(System.Linq.Expressions.Expression<ConsoleApplication1.StringPredicate>)' has some invalid arguments C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 19 10 ConsoleApplication1

Error 2 Argument '1': cannot convert from 'lambda expression' to 'System.Linq.Expressions.Expression<ConsoleApplication1.StringPredicate>' C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 19 19 ConsoleApplication1

:-( :-( :-( :-( :-( :-( :-( :-(

I was really hopeful this time, there isn't any technical reason why this can't be done. The compiler is going to turn the lambda expression into Expression<T> anyway, so it is certainly should be possible to take that and put it in the attribute declaration, thus satisfying the requirement that the values will always be constants.

This is actually harder than it seems, because it probably requires a change in the attributes spec, but I think that this is worth it.

At any rate, I opened up a suggestion at Connect, so please go and vote for it.