Custom Curly Bracket Methods

102 views Asked by At

In java some standard library methods (Maybe they're not actually methods?) have the following format:

keyword(condition or statements) {
//write your code here
}

This includes the if-statement, for-loop, while-loop do-while-loop etc.

for(initialValue = n; conditionForLoopToContinue; incrementOrDecrement) {
//write your code
}

Also you can start anonymus threads like so:

new Thread() {
//write your code here
}.start();

What I want to know is Can we create our own methods (or whatever you they're actually called) that have this curly bracket format?

So , for example, I would write an 'until' method that goes like this:

int a = 0;
until(a == 10) {
a++;
}

where until(a == 10) would be equivalent to while(a != 10).

Of course, the example above wouldn't allow us to do anything new (we can just use a while-loop), but the purpose of this question is to find out whether we can write 'custom curly bracket methods'.

Also, if you guys are a aware of any language that has this feature or one similar to it, please let me know.

Thanks for any help in advance!

3

There are 3 answers

1
aioobe On BEST ANSWER

You can't implement your own keywords. You can of course create anonymous subclasses of your own classes, i.e. you can do

new YourOwnClass() {
    // write your code here
}.launch();

if you like.

With Java 8, you get a bit further towards the curly brace syntax that you're asking for. Here's my attempt to mimic your util method using lambdas:

public class Scratch {

    static int a;

    public static void until(Supplier<Boolean> condition, Runnable code) {
        while (!condition.get())
            code.run();
    }

    public static void main(String[] args) {
        a = 0;
        until(() -> a == 10, () -> {
            System.out.println(a);
            a++;
        });
    }
}

Output:

0
1
2
3
4
5
6
7
8
9

Note that in this slightly contrived example there are some limitations. a for instance needs to be a field or a constant variable due to the closure.

0
geert3 On

What you are doing in fact is extending the language, i.e. inventing a new "reserved word" and saying that this reserved word must be followed by a boolean expression, and a statement (block).

The fact alone that you need a new reserved word can cause a lot of problems e.g. people may already use today the word until in the context of e.g. a variable. Your new feature would break that code.

Also you would need to tell the runtime environment what the effect of your new statement is.

I don't know languages where you can simply do that. Like @aioobe said, lambdas may be something that comes close.

1
OldCurmudgeon On

Not as elegant as aioobe's:

abstract class until<T> {
    // An attempt at an `until` construct.

    // The value.
    final T value;
    // The test.
    final Function<T, Boolean> test;

    public until(T v, Function<T, Boolean> test) {
        this.value = v;
        this.test = test;
    }

    public void loop() {
        while (!test.apply(value)) {
            step();
        }
    }

    abstract void step();
}

public void test() {
    AtomicInteger a = new AtomicInteger();
    new until<AtomicInteger>(a, x -> x.get() == 10) {

        @Override
        void step() {
            a.getAndIncrement();
        }

    }.loop();
    System.out.println("a=" + a);
}

Probably could use some improvement.

As far as other languages go.

In C - if I recall correctly - you can do:

#define until(e) while(!(e))

and in BCPL there was a full set of conditionals WHILE, UNTIL, IF and UNLESS among some others.