Alex Rivera | Logout

Any programming language with "strange" function call?

Asked 2010-07-22T10:43:23.893
9

I was wondering, is there any programming language where you can have function calls like this:

function_name(parameter1)function_name_continued(parameter2);

or

function_name(param1)function_continued(param2)...function_continued(paramN);

For example you could have this function call:

int dist = distanceFrom(cityA)to(cityB);

if you have defined distanceFromto function like this:

int distanceFrom(city A)to(city B)
{
   // find distance between city A and city B
   // ...
   return distance;
}

As far as I know, in C, Java and SML programming languages, this cannot be done.

Are you aware of any programming language that let's you define and call functions in this way?

Edit
Report

4 Answers

2

In Python, you can explicitly pass the name of the arguments you're calling the function with, which lets you pass them in a different order or skip optional arguments:

>>> l = [3,5,1,2,4]
>>> print l.sort.__doc__
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1
>>> l.sort (reverse=True)
>>> l
[5, 4, 3, 2, 1]

This looks a lot like what the Objective C syntax is doing, tagging each argument to a function with its name.

answered 2010-07-22T11:00:25.363
2

(see my very favourite personal effort - the final C++ approach at the end of this answer)

Language One

Objective-C but the calling syntax is [object message] so would look like:

int dist = [cities distanceFrom:cityA  to:cityB];

if you have defined distanceFromto function like this, within a cities object:

- (int)distanceFrom:(City *)cityA to:(City *)cityB 
  {
     // find distance between city A and city B
     // ...
     return distance;
  }

Language Two

I also suspect you could achieve something very close to this in the IO Language but I'm only just looking at it. You may also want to read about it in comparison to other languages in Seven Languages in Seven Weeks which has a free excerpt about IO.

Language Three

There's an idiom ("chaining") in C++ where you return temporary objects or the current object that is used to replace keyword arguments, according to The Design and Evolution of C++ and looks like this:

int dist = distanceFrom(cityA).to(cityB);

if you have defined distanceFrom function like this, with a little helper object. Note that inline functions make this kind of thing compile to very efficient code.

class DistanceCalculator
{
public:
    DistanceCalculator(City* from) : fromCity(from) {}

    int to(City * toCity) 
    {
         // find distance between fromCity and toCity
         // ...
         return distance;
    }

private:
    City* fromCity;
};


inline D
answered 2010-07-22T11:06:13.867
1

You can do this in C, albeit unsafely:

struct Arg_s
    {
    int from;
    int to;
    };

int distance_f(struct Arg_s args)
    {
    return args.to - args.from;
    }

#define distance(...) distance_f( ((struct Arg_s){__VA_ARGS__}) )
#define from_ .from =
#define to_ .to =

uses compound literals and designated initializers.

printf("5 to 7 = %i\n",distance(from_ 5, to_ 7));
// 5 to 7 = 2
answered 2010-08-02T01:45:58.090
0

You can use a member function for this.

cityA.distance_to(cityB);

That's valid code in C++, C(with a little tweaking), C#, Java. Using method chains, you can do:

cityA.something(cityB).something(cityC).something(cityD).something(cityE);
answered 2010-07-22T10:58:11.030

Your Answer