Latest sites

  • 2017-11-26: New Drupal 8 site at Rue du Commerce, architected and tech-led by OSInet, just went throught Black Friday week with flying colors thanks to RabbitMQ
  • 2017-05-26: New headless Drupal 8 / Symfony 3 site at FranceTV Sport, architected and tech-led by OSInet, with RabbitMQ
  • 2017-02-20: New Drupal 8 site galaxy (+/- 70 sites) for Agences Régionales de Santé architected and tech-led by OSInet, delivered by Klee
  • 2015-08-21: 50% less server load with MongoDB on the Drupal 7 site factory at France Télévisions
  • 2015-07-15: Our first Drupal 8 production site at France Télévisions is live
  • 2014-08-18: 400% speedup in 3 weeks for : who said Drupal back-offices had to be slow ?
  • 2014-02-07: Sotchi Olympics traffic not a problem for , which I rearchitected on Drupal 7 in 2013
  • 2011-09-14: Completed migration of FranceInfo.FR from SPIP to Drupal
  • 2011-07-13: The new social network features of Le Figaro are now powered by an OSInet-designed MongoDB implementation

Quick news

  • 2014-03-27: MongoDB Watchdog module ported to Drupal 8 at the Szeged Dev Days.
  • 2014-01-26: My post on the Symfony web profiler in Silex selected in Week of Symfony. w00t !
  • 2013-10-18: My first commit went into MongoDB today. And, guess what ? It's in JavaScript
  • 2013-09-20 to 29: Working on Drupal 8 EntityAPI at the extended code sprints during and around DrupalCon Prague
  • 2012-08-19: Working on Drupal 8 EntityAPI at Drupalcon Munich
  • 2012-06-15: Working on Drupal 8 EntityAPI at DrupalDevDays Barcelona
  • 2012-03-23: Working on the future Drupal Document Oriented Storage at DrupalCon Denver. D8 or later ? Bets are on Later

Golang fun : adding methods to primitive types

Go syntax has some funny peculiarities which are not immediately obvious, like methods on primitive types. Let's have a look.

Primitive wrapper types

Anyone with a few hours of Go is familiar with the basic object notation, like this example:

package main

import "fmt"

type Int struct {
  value int

// Imagine this is an actually complex and useful function instead.
func (i Int) Add(j Int) Int {
  return Int{i.value + j.value}

func main() {
  i := Int{5}
  j := Int{6}

Try it on

Looking for simplicity

The problem, of course, is that not only is this useless, it is also ugly, because we are wrapping a primitive type in a useless struct. What we probably would like would be something like this, like one can do in Ruby, where all types are objects which can receive methods:

package main

func (i int) Add(j int) int {
  return i + j

func main() {}

But if we try this, the Go compiler complains, as you can see on

prog.go:3: cannot define new methods on non-local type int

A solution

Now, Go has the ability to assign types to other types:

type Int int

Unlike in some languages where types are first-class objects, this is not aliasing: the two types will be different when compared, but offer the same capabilities. And this brings us the expected capability:

package main

import "fmt"

type Int int

func (i Int) Add(j Int) Int {
  return i + j

func main() {
  i := Int(5)
  j := Int(6)
  fmt.Println(i.Add(j) + 12)

Run it on

There we have it : we added a method on a type which is a copy of a primitive type. No struct boxing, no unboxing in the method.

Why it matters

The real beauty of this is actually not the ability to attach the method, although this is definitely satisfying from a conceptual point of view, bringing consistency with an approach opposite to that or Ruby, it is rather the code in the Add() method itself, and the last line in main() are actually the beauty of the thing : since our local Int type is actually just a name for the intrinsic int, it still support the builtin operators like naked ints: no explicit casts needed to use the aliased variable in an expression.

Is it possible to define an operator override ?

Is it possible to define a struct method to override "+" itself? To have Int(5) + Int(6) for example?

Operators cannot be overridden

I think I already saw this asked on #golang-nuts more than once : Go1.x does not allow this, and it seems unlikely to appear in 2.x if the current language direction remains stable : see explicitly rejecting this.