Thursday, November 21, 2013

Assert T

Here is some trick for getting over type erasure in Scala.


I want to create the following method that will expect an exception of particular type:

def assertT[T <: Throwable ](body: =>Unit) = {
  try {
    body
    assert(false)
  } catch {
    case e : T => assert(true)
  }
}

But due to type erasure, the case condition ( : T ) will be never checked. Scala compiler will warn you with "abstract type pattern T is unchecked since it is eliminated by erasure".

To fix this, you need a runtime class info and runtime class check:

def assertT[T <: Throwable : ClassTag](body: =>Unit) = {
  val clazz = implicitly[ClassTag[T]].runtimeClass
  try {
    body
    assert(false)
  } catch {
    case e if clazz.isInstance(e) => assert(true)
  }
}

where ClassTag is scala.reflect.ClassTag

Sunday, May 19, 2013

Capital Eye

What will the following line print out?

Console.WriteLine("i".ToUpper());

The answer is obvious: "It depends"

String.ToUpper as well as string.ToLower will use current thread culture, and in most cases it will print "I", but there is two cultures, where capital "i" is not "I", but "İ" - Turkish (tr) and Azeri Latin (az-Latn).

So never ever use ToUpper() and ToLower() in internal comparisons or serialization (I have found this while creating SQL queries, something like "... where id in [1]".ToUpper()), use ToLowerInvariant() and ToUpperInvariant() instead.

Compiled lambda issue



Never ever return the result of Expression<T>.Compile() method. If your compiled expression throws an exception, you will not get any info about the compiled lambda. For example, I have the NullReferenceException with following stack trace:


System.NullReferenceException: Object reference not set to an instance of an object.
   at lambda_method(Closure , BugDto )
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Linq.Lookup`2.Create[TSource](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.GroupedEnumerable`3.GetEnumerator()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
...

There is nothing that could help me to find the cause of exception, I don't know where the expression was compiled or even created.

The best you can do is to return Expression<T> itself and use IQueryable methods instead of IEnumerable.

Monday, October 15, 2012

Queryable extensions

It is possible to extend a LINQ vocabulary with some custom methods that will be used later in query providers to build a custom query to outer source. Also, there is a little trick that allows using such extensions in Linq-to-objects expressions.

Here is a standard stub for an IQueryable method:


public static IQueryable<Tuple<int, TKey>> CountBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
{
    return source.Provider.CreateQuery<Tuple<int, TKey>>(
            Expression.Call(
              null,
              ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
              new[] { source.Expression, Expression.Quote(keySelector) }
                                                       ));
}

To add this methods for LINQ-to-Object you have to add the method for IEnumerable as well with same parameters (just replace Expression<Func<...>> with Func<...>) to the same type where IQueryable method is defined:


public static IEnumerable<Tuple<int,TKey>> CountBy<TSource,TKey>(this IEnumerable<TSource> source, Func<TSource,TKey> keySelector)
{
    return source.GroupBy(keySelector).Select(x => Tuple.Create(x.Count(), x.Key));
}


Friday, September 21, 2012

Pearls of scala I - By-name parameters

In C# there is no way to define short-circuit evaluated 'and' function (for example).

The naive implementation like following is not good enough.

bool And(bool a, bool b){
  if (a)
    return b;
  else 
    return false;
}

Let define an identity function:


bool Id(bool value){
  Console.WriteLine(value);
  return value;
}


And let's compare output of the following statements:

And(Id(false),Id(false))    and    Id(false) && Id(false)

The first statement will produce:
False
False

The second one:
False

The second statement will not be fully evaluated. As far as first operand of && returns false, the statement evaluates to false regardless of second operand.

Instead the first one have to evaluate both parameters before evaluating the method body.

The only way to imitate the short-circuit evaluation is to pass ugly Action<bool> as a second parameter:


bool And(bool a, Action<bool> b){
  if (a)
   return b();
  else 
    return false;
}

var result = And(Id(false), ()=>Id(false));

In Scala, by-name parameters are part of language, so we could easily transform long-circuit 'and' function to  a short-circuit with minimum changes in function definition and without any changes in function usages:


Long-circuit:

def and(a:Boolean, b:Boolean) = if (a) b else false

Short-circuit:

def and(a:Boolean, b: => Boolean) = if (a) b else false

'=> Boolean' states for 'by-name' parameter, it would be evaluated only in function body as many times as requested (even zero, like in 'and(false,someLongFunction())')




Wednesday, May 30, 2012

MakeGenericType

When does the following line of code could fail with "The number of generic arguments provided doesn't equal the arity of the generic type definition"?


var t = typeof (SomeGenericClass<,,>).MakeGenericType (typeof (int), typeof (int), typeof (int));

Where SomeGenericClass is defined as:

public class SomeGenericClass<TT1T2>
{
}

Answer (thx for Elena):

If SomeGenericClass is defined inside another generic type, its generic types arguments will consist of root type generic arguments and its own generic arguments. MakeGenricType will cut of all generic type arguments, so you have to provide types for base type definition too:


public class SomeRootClass<T3>
{
  public class SomeGenericClass<TT1T2>
  {
  }
...

var t = typeof (SomeGenericClass<,,>).MakeGenericType (typeof (T3), typeof (int), typeof (int), typeof (int));
...
}

Thursday, May 24, 2012

Reverse

Three tiny puzzles:
  1. Given the following interface:

    interface
     INode
    {
             INode Next { getset; }
             object Data { get; }
    }

    and a reference to root 
    INode root reverse a one-linked list. Do not use any additional collections, as arrays, lists or stacks.
    
    
  2. The same for this interface:

    interface INode
    {
             INode Next { get}
             object Data { getset; }
    }

  3. And the same for this:

    interface
     INode
    {
             INode Next { get}
             object Data { get; }
    }
First and second tasks are almost the same, the 3rd solution will be good for 1st and 2nd as well.