Entries by tag: java

I want higher-order functions, dammit!
[info]demoness101
Even delegates would help.

I'm having to write like 10 instances of this code:

  refine LookupVariable eq ClassDecl.superFieldsIterator() {
      return superIteratorWrapper(new Lambda<ClassDecl, Iterator>() {
          public Iterator apply(ClassDecl decl) {
              return decl.fieldsIterator();
          }
      });
  }
Tags:
  • Add to Memories

ARGH JAVA RANT
[info]demoness101
This is f***ing ridiculous.  Due to type erasure, you can't create an array of a generic type T without having an object of type T and using reflection to make an array of the same type.  But if you create an Object array, you can't cast it to a T array either, since an Object array that will always contain T's is not the same as a T array (as it shouldn't be, but dammit, the type system should realize this operation is safe!).

So I thought, ok, I'll pass in a function that will create an T[] of arbitrary size. Except then I have to create yet another damn interface, and implement the stupid thing.  This is nothing new, but what really irritated me was that I just now remembered that faking higher order functions in freaking C++ is easier than this--you just overload the function call operator.  Alteratively (and a better approach) is simply to assume the type T passed in will have a member function named, say, "apply", and then the typechecker will make sure this is true of T once the template is instanitated, so you can fake structural subtyping.  Don't get me wrong, I fully (and painfully) understand the issues with C++ templates, but seriously, we're in a bad situation when C++ is beating Java.

So I thought, oh yeah, I have a wrapper lying around that is intended to wrap constructors.  It basically has only one function, makeObject, with the type alpha->beta.  Except I can't use it, as is. Know why?  The argument type I need is "int", and "int" is not a type that can be used to instantiate type variables.  So then I have to deal with freaking "Integer". Boxing helps, though marginally.

Btw, have I mentioned that the Fluid library includes a class "ArrayIterator", because in order to make an array something that you can iterate over, you have to WRITE CODE??

Perhaps one argument for having "dynamically typed languages" is that then you can have a screwed up language where you'd have to jump through hoops to get a typechecker to accept it if you had one, but people won't notice because they don't have a typechecker.  Somewhat ironically, Java's ugliness is far more apparent to me now that it has a slightly less primitive type system, since it makes the the language's flaws all the more obvious.

Somebody, quick, write a translator (that you've proven correct) from Java to Scala.  Ok, I'll give you a little more time, since you do have to first formalize the sematics of both Java and Scala... (though I think someone has done this for Java, since they tried to prove its whole disgusting mess as type safe).
Tags: ,
  • Add to Memories

More grossness
[info]demoness101
I just want to create a damn pair.

(actual code follows)
new Pair
[Error: Irreparable invalid markup ('<methodwrapper,>') in entry. Owner must fix manually. Raw contents below.]

I just want to create a damn pair.

(actual code follows)
new Pair<MethodWrapper, List<MethodWrapper>>(result, new ArrayList<MethodWrapper>())

Sigh. In a civilized language, this would be simply "(result, nil)".
Tags: ,
  • Add to Memories

I want higher order functions.
[info]demoness101
Yes, I know I can fake it with anonymous classes, but it's SOOOO ugly, especially if you're using generics...
Tags: ,
  • Add to Memories

Eclipse is wonderful
[info]demoness101
It turns out that Eclipse would have caught the problem I mention here.  Go to Java->Compiler->Errors/Warnings and add a warning (or even an error!) for an empty statement.  Eclipse also has the option of giving a warning if you declare a method as throwing a checked exception that it doesn't actually throw. 

Incidentally, Eclipse's code formatting is very useful--you can have a different color/bold/italic for instance variables and static members.  Now all it needs is a button for "fix Java's stupidness" and everything would be great.
  • Add to Memories

More Java rants
[info]demoness101
In today's edition of the continuing "I hate Java" series, Donna talks about the lack of higher-order functions:

I hate writing the SAME code over and over again.  In the program analysis framework I'm using (Fluid), you can attach an arbitrary (untyped) piece of data to an AST node.  This is convienient.  These data are keyed with a "slot info", and when you create a slot info object, you specify a default value for the slot.  But unless you have immutable values, the only value that makes any sense is "null".  In my case, I'm storing a mutable list, and a specifiying an empty list as the default would result in the same list being returned for all nodes. (Fortunately I realized this before I wrote code to do this.)  What's annoying is that obviously what we want is for the slot info's to take a *function* that will provide the default value. But this is such a pain in the ass to do in Java that of course no one does this sort of thing.  What I'm sick of is time and time again, writing this code:

result = hashtable.get(key); (or "exists" or whatever)
if (result == value doesn't exist)
  add a new item to the hash table
  return this item
else
  return hashtable.get(key)

I'm curious if there's a common idiom for this case in functional programming, or if it's even built-in to the standard libraries?

(Incidentally, I got so sick of how ugly the above code is that in one case I wrote an inner class inside of a function to do get a quick-and-dirty closure).


Note: Please excuse my rants.  But the last time I programmed this much in Java, I didn't know any better and didn't truly understand its mediocrity.  I often had a general sense of "this is kludgy, there has to be a better way!" and I'm very glad I can now articulate the awfulness.  (In fact, sometimes I mistakenly thought it was the nature of the problem that caused the badness!)

My next programming project is going to be in Scala, without a doubt.
Tags: ,
  • Add to Memories

ARGH!!!!
[info]demoness101
WHY OH WHY isn't there a warning in Java if you have something like:

if (condition);
{
}

or worse:
while (condition-that's-initially-true-then-falsified-while-executing-loop);
{
}

This is the second time in 3 days that I've done this.  I need to write an eclipse plugin that detects this and flags it.  If any of you are inclined to help me with this, please let me know.
Tags: ,
  • Add to Memories

Java 5.0 generics
[info]demoness101

I like having typed containers, but JEEZ they could have fixed the syntax!  As far as I can tell, there are NO type abbreviations, and you have to use the fully-parameterized type when creating an object, leading to this code:

  private HashMap< Pair<IRNode, IJavaDeclaredType>, Set<IRNode>> origThrowMap =
        new HashMap<Pair<IRNode, IJavaDeclaredType>, Set<IRNode>>();

And what I want to do isn't all that complicated!  Map a pair to a set!

 

Tags: ,
  • Add to Memories

Argh
[info]demoness101
I wish Java had "fold", and real lambdas!

I suppose I could program in Scala, but I'm unsure how well that would work.
Tags: ,
  • Add to Memories

You are viewing [info]demoness101's journal