Take the 2-minute tour ×

Possible Duplicate:
Is there a language out there in which parameters are placed inside method name?

Does a language exist with such a syntax? If not, what are some of the limitations/disadvantages to this syntax in case I want to write a language that supported it?

Some examples:

sort(array, fromIndex, toIndex);



Method signature would like this:

sort(SomeType[] arr)from(int begin)to(int end){


Because there might be some confusion, I'd like to clarify... I meant this question as a general idea like this (not specific to sorting and possibly using keywords like from and to):

In JAVA(like language):

void myfancymethod(int arg1, String arg2){

myfancymethod(1, "foo");

In imaginary language:

void my(int arg1)fancy(String arg2)method{

share|improve this question
Downside: explosion of the number of keywords, complicated corner cases. Even VBA is less verbose than what you are proposing. SCIP authors believe that the number of reserved keywords should be no more than the number of toes and fingers. Upside: I cannot think of any. –  Job Nov 18 '11 at 21:38
@Job: I do not understand what you mean about the keywords. –  c_maker Nov 18 '11 at 21:46
That second one is horrible. I would hate having to code in that language. –  Darren Young Nov 18 '11 at 21:50
This question has definitely been asked before on an SE site, but I can’t seem to find it. –  Jon Purdy Nov 18 '11 at 22:04
@Job: Take a look at Agda, which handles it quite gracefully: if_then_else_ can be used as if x then y else z, and an infix 10 if_then_else_ declaration ensures that … else a + b is parsed as … else (a + b) and not (… else a) + b. Simple. –  Jon Purdy Nov 18 '11 at 22:07
show 2 more comments

marked as duplicate by c_maker, Jörg W Mittag, Jon Purdy, Winston Ewert, back2dos Nov 19 '11 at 21:32


6 Answers

up vote 18 down vote accepted

Smalltalk and derived languages (including Objective C) use a syntax like this for method calls.

In Objective C, for instance, one can work with an NSMutableArray container with something like:

id a = [[NSMutableArray alloc] initWithCapacity: 3]

[a insertObject: @"Bob" atIndex: 0] 
[a insertObject: @"Fred" atIndex: 1]
[a insertObject: @"Mary" atIndex: 2]    

[a replaceObjectAtIndex: 3 withObject: @"Hello, World!"]
share|improve this answer
add comment

If you create a fluent interface, you can write it more or less like this:


But method overload resolution will give you simpler syntax:

array.Sort(fromIndex, toIndex);

...and if you name things sensibly, it's still self-describing.

If you still need the descriptive verbiage, some languages (like C# 4.0) allow you to specify the names for each method parameter:

array.Sort(from:= fromIndex, to:= toIndex);
share|improve this answer
It seems you need to write a whole lot of code to make a fluent interface. –  c_maker Nov 18 '11 at 22:23
All that's really required is return this in your method bodies (in languages such as C# and Java). –  Robert Harvey Nov 18 '11 at 22:32
That means though that instead of a single method supporting the syntax mentioned in the post, you need an actual class, set all your parameters and call 'go'. –  c_maker Nov 18 '11 at 22:36
The Sort() method is the "go" method. But you're right; in class-based languages, your method needs to go somewhere, even if it's in a static utility class. We could talk about lambda expressions, but that's probably beyond the scope of your question. –  Robert Harvey Nov 18 '11 at 22:39
In Scheme or Lisp, it would simply be (sort array from to), or something similar. Of course, you still need to build up a function, but polymorphism works there as well, so you can still simply say (sort array) if you want to. As you can see, I favor a "functional" approach (your first example) over the fluent interface which you more or less described in your second example. –  Robert Harvey Nov 18 '11 at 22:42
show 2 more comments

In Agda an identifier containing underscores can be used infix. Consider the declaration of if:

if true  then x else y = x
if false then x else y = y
share|improve this answer
add comment

I'm not aware of any language with syntax quite like that, although QBasic and Visual Basic had some built-in statements with custom syntax, like "LINE (10,10)-(80,95)".

I like your idea as a general-purpose feature. I think "sort(array from index1 to index2)" would look slightly more attractive and be easier to type, and presumably you're talking about a single function call here, which is not as clear if several sets of parenthesis are required. Parsing might be challenging: "from" and "to" basically represent commas in the syntax I just suggested, but the parser wouldn't know that for certain until the function name is resolved. However, it could be parsed as a sequence of expressions: (array), (from), (index1), (to), (index2) where (from) and (to) are replaced with commas during semantic analysis.

Instead of defining "sort" in a special way, you could also envision this as a way of representing named parameters:

sort (SomeType[] arr, int from, int to) { ... } // declaration
sort (arr from: index1 to: index2); // possible call syntax 1
sort (arr, from index1, to index2); // possible call syntax 2
sort (arr from index1 to index2); // possible call syntax 3
share|improve this answer
add comment

You could write your example in Tcl like this if you wrote your own sort function:

sort array from $index1 to $index2
share|improve this answer
But it's more common to prefix the extra “keywords” with a minus and make them optional with sensible defaults (sort array -from $idx1 -to $idx2). Many built-in commands fit this model rather well (lsort and lsearch being the classic examples). –  Donal Fellows Nov 19 '11 at 8:55
(Weirdly, there's no -from or -to option to lsort; guess nobody ever had a real requirement for it.) –  Donal Fellows Nov 19 '11 at 8:56
yes, common, but not strictly necessary. –  Bryan Oakley Nov 19 '11 at 14:17
add comment

TeX has this kind of functionality: you can define for example:

\def\say #1 to #2.{#2 said: ``#1.''}

And use it:

\say Hello to Mike.

To get:

Mike said: ``Hello.''

This allows for some nice syntactic tricks.

share|improve this answer
add comment
