Saturday, May 30, 2009

Java Riddle

I change the value of a constant in one file, and it causes a compile error in a different file. How is this possible?

Sunday, May 17, 2009

It's all C's fault

C's type casting syntax creates a lot of trouble for parsers.

(a)-b

What does the above code mean? are you casting negative b to type a? or are you subtracting b from a? It's ambiguous and depends on whether a is a type or a value.

Well, actually that's not the whole story. In Java, the only two ambiguous binary/unary operators are + and - which are only applicable as unary operators to primitive types. So any instance of (___)-b could be disambiguated by whether or not the ___ was one of the 7 primitive types. This is because you can't cast a primitive to anything but another primitive. In C++ this is not the case.

C++ has operator overloading, but barely dodges the bullet by enforcing the prototyping rule. To paraphrase, "every identifier must be defined prior to its usage." including types and where "prior" refers to position in source code. So if the C++ parser is traveling forward, it will have an accurate bank of identified types whenever it needs to disambiguate code such as (a)-b. All it needs to do is look up what category a belongs to--type, variable, function, etc,--and it will be able to make the right decision from there.

C++ can't cleanly separate parsing from semantic analysis, but C++ sucks anyway so who cares.

Wednesday, May 13, 2009

Java's "protected" access fine print

In Java, protected means that subclasses have access, even if the classes are in different packages. This is actually not always the case. Right from Java's language spec:

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.


The following code produces a compile error.


package p1;

public class C1
{
protected void foo()
{
}
}

...

package p2;

import p1.C1;

public class C2 extends C1
{
private C1 c1;
private void bar()
{
c1.foo(); // compile error
}
}


I have yet to understand why this is the case. I'm guessing it has something to do with virtual methods and identifier resolution tokens.

Sunday, May 3, 2009

Name that regex

What do these regular expressions match?

  1. /0x[0-9A-Fa-f]+/

  2. /\-?\d+/

  3. /\/{2}.*?\n|\/\*.*?\*\//

  4. /<!\-{2}.*?\-{2}>/

  5. /^\d{5}(\-\d{4})?$/

  6. /[A-Za-z_][A-Za-z0-9_]*/

Tuesday, April 28, 2009

Electric Traps and a High-Tech Marker

You are lost deep in an enormous maze.

The maze is made of rooms, automatic sliding doors, and fluorescent-lit hallways. The maze is so large and uniform-looking, and your memory is so bad, that you aren't able to remember where you've been or how you got wherever you happen to be. Rooms are connected by hallways with doors at both ends. Hallways can have stairs and ladders to other stories of the maze. There is exactly one exit.

Halfway through every hallway, the walls, floor, and ceiling are lined by strange metal. Every time you step on the metal, the hairs on your arms stand up and you hear a high-pitched squealing coming from the walls. The squealing continues until you're off the metal on one side or another. If you come back to a hallway and step on the same metal a second time, you hear the squealing again, but even higher-pitched. If you step on the metal in any hallway a third time, you get electrocuted and killed. There is no way to get through a hallway without stepping on the metal.

You find in your pocket a high-tech marker that can only write on the metal walls in the hallways, and only when they're squealing. You have no other way to leave markings in the maze.

How do you find your way out alive?

Sunday, April 26, 2009

Consistent Arbitrary Colors

Mibbit gives usernames colors, i think, depending on their users' client. XChat gives everyone but me the same color. I don't like either of these choices because many usernames get the same color, and I therefore can't use color to tell who said something without reading their username.

I'd prefer it if everyone had a different color (to a reasonable extent), but still have the same color the next time they log in. Short of users assigning colors to anything, how could an IRC client accomplish this?