1

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!

jww
  • 97,681
  • 90
  • 411
  • 885
Dziugas
  • 1,500
  • 1
  • 12
  • 28
  • Sure you can own methods and format it like that, but your example of `until` is not an own method, it is a try to create a new keyword. – Tom Dec 11 '14 at 10:03
  • if, while and so on are build in the language and not actual functions. Your example with Thread() is a new (anonymus) object. – Mirco Dec 11 '14 at 10:03

3 Answers3

2

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.

aioobe
  • 413,195
  • 112
  • 811
  • 826
0

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.

geert3
  • 7,086
  • 1
  • 33
  • 49
0

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.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • This has the restriction that your condition depends on a single variable. (Btw, you can use Predicate instead) – aioobe Dec 11 '14 at 12:08