Hi,
Some more ideas. Would very much appreciate some feedback.
Thanks

----------------- atomics and volatile ------------------------

type T {
    stop volatile bool
    i atomic int
}
func T.ops() {
    i++
    ++i
    i = 3 ? 1;
    i--
    i += 1
    i -= 2
    v := i
    v = i++
}
func T.Stop() {
    stop = true
}

Translates to

public final class T {
    volatile boolean stop;
    AtomicInteger i = new AtomicInteger();

    final void incr() {
        i.getAndIncrement();
        i.incrementAndGet();
        i.compareAndSet(3, 1);
        i.getAndDecrement();
        i.addAndGet(1);
        i.addAndGet(-2);
        int v = i.get();
        v = i.getAndIncrement();
    }
    public final void Stop() {
        stop = true;
    }
}

---------------- importing Java types --------------------

// imported types can be given a local alias
// so in a way string is an alias for String
import java.util.ArrayList list

func foo() {
    v := new list<string>()
    v.add(“hello”)
    v.add(“world”)
    for x:v {
        print(x)
    }
}

translates to:

import java.util.ArrayList
public final class Statics {
    static void foo() {
        ArrayList<String> v = new ArrayList<String>();

        v.add(“hello”);
        v.add(“world”);
        for (String x:v) {
            System.out.println(x);
        }
    }
}

--------------------- Discarding exceptions ----------------------

// Support a syntax for discarding exceptions
func foo() {
    try Thread.sleep(2)    // silently consumes InterruptedException
}

translates to:

public final class Statics {
    static void foo() {
        try {
            Thread.sleep(2)
        } catch (Exception e) {
            // ignored
        }
    }
}

------------------------ Exceptions ----------------------------
import java.io.FileReader
// a default try/catch/finally block
// this might be too wierd !!!
func foo()  {
    f := new FileReader(“testfile”)
} catch e {
    e.printStackTrace()
} finally {
    try f.close()
}

translates to

import java.io.FileReader
public final class Statics {
    static void foo() {
        FileReader f = null;
        try {
            f = new FileReader(“testfile”);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (f != null) {
                try {
                    f.close();
                }
                catch (Exception e) {
                }
            }
        }
    }
}

-- 
You received this message because you are subscribed to the Google Groups "JVM 
Languages" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/jvm-languages?hl=en.

Reply via email to