Scala Trait self-types or “How do I tell my trait where it’s going to be mixed in?”

A feature of Scala whose syntax I tend to forget are self-types, with which you can tell a trait where it is going to be mixed in, just like this:

class Foo {
    def x = 5
}

trait Bar { self: Foo =>
    def y = x + 10
}

The self: Foo => part tells the trait that it is going to be mixed into a class of type Foo and that it can therefore use all methods from this type. This is why the Bar trait is then able to call the x method from the Foo class:

scala> (new Foo with Bar).y
res4: Int = 15

Whereas the following will fail miserably:

scala> (new Object with Bar).y
<console>:10: error: illegal inheritance;
  self-type java.lang.Object with Bar does not conform to Bar’s selftype Bar with Foo
          (new Object with Bar).y

Dark Pastels with Groovy support for IntelliJ IDEA 11

I recently ran across the awesone Dark Pastels color scheme for IntelliJ IDEA by Ted Wise:

(image taken from here)

Unfortunately it didn’t support Groovy and had some minor other flaws (in my opinion) which I have corrected. The Groovy highlighting looks like this:

Installation

Download the coding-journa-dark-pastels.jar and import it using the File -> Import Settings dialog from the IntelliJ menu.

Suggestions and Bugs

Have a suggestion or found something odd? Leave a comment and I’ll get back to you :)

Using Groovy classes from within Java when building with Gradle

I’ve been experimenting a bit on Terasology, an open-source game inspired by Minecraft. They’re using Groovy and Java in their project, and their build tool is gradle.

Unfortunately it wasn’t as straight forward as I had hoped to use a Groovy class from within Java, because by default, Gradle seems to first invoke the Java compiler and afterwards the Groovy compiler, which leaves the former with missing symbols (the classes from Groovy that haven’t been compiled yet).

Apparently, to enable “joint compilation”, you have to force the Groovy compiler to compile both Java and Groovy sources (as stated here), which can be done by adding the following snippet to your “build.gradle”:

sourceSets {
    main {
        java { srcDirs = [] }    // no source dirs for the java compiler
        groovy { srcDir ‘src’ }  // compile everything in src/ with groovy
   }
}

Automatically creating footnotes when referencing Multimarkdown glossary entries

Multimarkdown has a feature to add glossary entries when compiling to LaTeX, and it looks like this:

[^glossaryentry]: glossary: term
    The actual definition belongs on a new line, and can continue on
    just as other footnotes.

And to reference a glossary entry created that way, you can use:

This is some text. [^glossaryentry]

Which, in LaTeX, renders to

This is some text. \glsadd{term}

Unfortunately, \glsadd does not add any indication in the text where it is invoked, and there is not (easy) way to change the Multimarkdown behavior to use, \gls instead of \glsadd, for example.

The workaround I devised for this problem is that I overwrote the \glsadd command.

First: Caveats

  1. In my case the created footnote markers (the small numbers added inline) linked to the first page of the document instead of linking to the actual footnote, and I wasn’t able to find out why. I “fixed” this problem by disabling links for footnotes entirely by using the hyperfootnotes=false for the hyperref package, as explained here.

  2. If you want to use LaTeX commands or math mode in your glossary entries you will need to specify the following option for the “glossaries” package:

\usepackage[
    ...,
    sanitize={description=false}
]{glossaries
}

The Code

% Rewrite glsadd so that it also creates a footnote
% We do this so that the glossary references created by Multimarkdown
% (which uses \glsadd) also generate a footnote (didn’t want to hack mmd)

% This copies the old \glsadd command to \Oldglsadd, so that we can invoke it in our new command
% http://www-h.eng.cam.ac.uk/help/tpl/textprocessing/extending_latex.html#Commands
\let\Oldglsadd\glsadd

% Create the new \glsadd command
\renewcommand{\glsadd}[1]{%
    \footnote{\textbf{\gls{#1}}: \glsentrydesc{#1}}     % Create the footnote
    \Oldglsadd{#1}%                                     % Call the old glsadd to add the reference
}

If you insert this snippet into your LaTeX preamble, all you Multimarkdown generated glossary entries will also appear as footnotes on the page where they are referenced.

Hope this saves you some time, because it took me a solid hour to figure this out…

Enabling HTTPS in Apache results in 403 Forbidden in Internet Explorer

After enabling HTTPS for a certain VHOST (within MAMP, but that is probably not relevant), Internet Explorer 8 started denying to display any HTTPS enabled page, showing a 403 Forbidden error instead. This problem occurred because of the following setting in my Apache configuration:

# Don’t accept connections from non-SNI clients disabled
SSLStrictSNIVHostCheck on

Apparently IE8 doesn’t support Server Name Indication (I’m using version 8.0.6001.18702), even though it should

Long story short, disabling the option solved my problem. This was on my local development system, so I’m not worried about the security implications, but I’d like to know them anyway, so if anybody knows, please leave comment :)

C/C++: Fill variables using a single value

A more or less interesting C/C++ problem occured to me this day:

How do I initialize an integer with one part without using shifts. For example you have 0xFF and you want 0xFFFFFF.

Normally you would do something like this

int x = 0xFF << 16 | 0xFF << 8 | 0xFF

But how does it work without this runtime computation? And does it help you increase your performance? Of course not, but we'll see.

One could use arrays:

#define FILL(x,t) *(t*)(const unsigned char[]){x,x,x,0}

int main() {
    int foo = FILL(0xFF, int);

    return 0;
}

This would convert to the following assembly (64bit code):

.main
    …
    subq    $16, %rsp       ! reserve space for rbp and foo
    movl    $._6, %eax      ! load address of ._6 (our static array)
    movl    (%rax), %eax    ! dereference rax and save value in eax
    movl    %eax, -4(%rbp)  ! save value of eax in `foo`
    …

._6:
    .byte    -1
    .byte    -1
    .byte    -1
    .byte    0

If you're wondering why there is -1 and not 255 at the .byte directive, the number stands for 0b11111111 whereas -2 would stand for 0b11111110 and so on.

Comparsion to naive solution

The naive solution:

int foo = 0xFF << 16 | 0xFF << 8 | 0xFF

Compiles to the following assembly (64bit again):

.main:
    …
    subq    $16, %rsp
    movl    $16777215, -4(%rbp)
    movl    -4(%rbp), %eax
    …

The compiler optimizes the expression so it is a number which will be put into our variable foo. This is way better than loading an address and dereferencing it.

Conclusion

Use shifts, the compiler will optimize them anyway.

Using the kinect camera with Ubuntu Oneiric Ocelot

This post resembles the major problems (and solutions) which I had, using my previously working OpenNI/NITE setup on Ubuntu/amd64.

If you need to know how to setup your kinect camera, please take a look at this post. Of course, the guide if for previous Ubuntu versions but it worked for me quite well.

Kinect as HID

I noticed that I got a warning, that the USB device could not be initialized.

strace revealed, that the USB device was busy with something.

I remembered from projects before using USB, that the linux kernel happens to assign HID drivers to generic USB devices.

Exactly this was the problem.

The detach-tool from this website solved the problem for me:

$ sudo ./detach 045e 02ae

I got the Vendor/Product ID required from lsusb.

$ lsusb

Bus 002 Device 007: ID 045e:02ae Microsoft Corp. Xbox NUI Camera

Depth image not existant or garbled

This is a funny one. The depth image was not existant or looked like a pile of grey marshmellow clouds.

I got the final hint for this in this discussion.

The problem is that memcpy in the libc was modified to use SSE3 instructions on amd64, which leads sometimes to backwrites (as far as I understood).

The solution was to add

/usr/lib/x86_64-linux-gnu/libc/memcpy-preload.so

to /etc/ld.so.preload (the file didn’t exist until then).

Restoring your Unix System after “rm -rf /”

It happens to all of us, today it happened to me. Conveniently on a production system. Instead of rm -Rf ./* I typed rm -Rf /*, and lots of stuff was gone.

Continue reading

Using Navicat with a MAMP MySQL database on Mac OS X

When trying to connect to a MAMP MySQL database with Navicat, I initially got the following unhelpful error message

Connection Failed
Can’t connect to MySQL server on ’127.0.0.1′ (61)

even though all settings (Hostname, Port, User, Password) seemed to be correct. I am not quite sure why this happens, but it can be solved by pointing Navicat to the MAMP MySQL server socket, by setting

/Applications/MAMP/tmp/mysql/mysql.sock

as the Socket File Path in the “Advanced”-Tab, and you’re done.

Cocoa Controls : Custom Controls for Mac OS X and iOS

If you are building a Mac OS X or an iOS application you may find that the default Cocoa controls are sometimes not exactly what you need. But before writing you roll out your own, you should take a look at CocoaControls.com which lists dozens of iOS and Mac OS X controls that may save you a lot of development time, which can then go into more awesome functionality ;-)