Strongly typed properties in Java vs C#

Posted on Thu 19 January 2012 in Coding • Tagged with delegate, generics, method group, csharp, java

The background for this post was that I needed to implement strongly typed properties in a C# project. With strongly typed property, I specifically mean a strongly typed version of public object GetProperty(string propertyName), where the property name is a constant (or name) of some sort, and the return …


Continue reading

Generic extension method with multiple constraints

Posted on Wed 18 January 2012 in Coding • Tagged with constraints, extension methods, generics

In a .NET project, I was coding an extension method for forms, and for various reasons I needed to call a form method which came from an interface. Instead of coding an ugly type check and subsequent type cast, I was happy to discover that I could simply create a …


Continue reading

Playing with Java annotation processing

Posted on Fri 06 January 2012 in Coding • Tagged with annotations, apt, Java, spi

I found Project Lombok via a Tweet, and was intrigued when I saw that a single annotation could trigger automatic code generation transparently during compilation of a source file. How do they do that?

Some digging lead me to discover Java’s annotation processing API, and how one can use …


Continue reading

Generator combined with with/using statement - Python vs C#

Posted on Fri 30 December 2011 in Coding • Tagged with c#, generator, Python

The other day, I tried to return a generator expression from within a with statement in Python, like so:

#!/usr/bin/python

def readlines(fn):
    with open(fn) as fd: # <-- the with statement
        return (line for line in fd) # <-- the generator expression

if __name__ == "__main__":
    for line in readlines("/proc …

Continue reading

Mocking iteration over file in Python

Posted on Sat 24 December 2011 in Coding • Tagged with file, mock, test, Python

Let’s say that I want to mock reading from a file using file’s iterator capability, i.e. (simplified version):

def readfile(f):
    using open(f) as fd:
        for line in fd:
            yield line

I could mock the readfile function, but then my mock would have to replicate the …


Continue reading

Split on separator but keep the separator, in Python

Posted on Fri 23 December 2011 in Coding • Tagged with reduce, split, string, Python

I have a function that reads from a file using file’s iterator capability, i.e. (simplified version, in reality there’s going on more within the generator):

def readfile(f):
    using open(f) as fd:
        for line in fd:
            yield line

If the file contains something like a\nb …


Continue reading

Function return value caching in F#

Posted on Tue 20 December 2011 in Coding • Tagged with caching, f#, return value

In my TestNess project I have decided to try to rewrite certain parts in F# to see if the code can benefit from functional programming. It is also a learning thing as I’m definitively not well versed in functional programming - yet :-). One of the specific problems I try to …


Continue reading

A Maven POM for building JSR335 code

Posted on Mon 19 December 2011 in Coding • Tagged with closures, defender methods, java8, maven

While playing around with lambdas and defender methods developed within the scope of Project Lambda, I thought I’d create a Maven POM file to facilitate compiling, testing and weaving (the process of making defender methods work in the JVM) of the code I write. The entire project can be …


Continue reading

Some notes on building Project Lambda to test closures and defender methods in Java

Posted on Thu 15 December 2011 in Coding • Tagged with closures, defender methods, jdk8

Check out Project Lambda if you don’t already know what I’m talking about! It adds lambda functions and interface extension methods to Java. Lambda functions allows for very compact and concise code as well as functional programming aspects. Interface extension methods gives you the ability to add to …


Continue reading

Forcing a .NET application to use a 4.0 CLR

Posted on Wed 14 December 2011 in Coding • Tagged with clr, dotnet

I was in a situation where I had an application built with .NET 3.5, but the application needed to launch a sub system build with .NET 4.0. This didn’t work, obviously, as the 3.5 CLR refused to load the 4.0 assemblies. My first idea was …


Continue reading