Do Any Existing Languages Allow Function Arguments In Arbitrary Places In A Function Name?
Asked Answered
F

7

9

When we write (in some language or another):

   lengthOf(n)

We think of it as short for an english 'fill in the blanks' construction, like:

  length of __

But when we write things like these:

  isAnInteger(n)
  appendTo(n,m)

We think of sentences like:

  __ is an integer
  append ___  to  ___

So, it would seem more natural to allow function invocation expressions like:

  (n)isAnInteger
  append(n)to(m)

Where the 'function names' are something like:

  _isAnInteger
  append_to_

Can anyone name existing programming languages which allow this? I know object-oriented languages let the object be one argument at the front, but I am wondering about more flexible syntaxes.

Fulford answered 18/4, 2012 at 3:24 Comment(4)
Objective-C method invocations are often of the form [anObject performOperation:operation withTask:task andInteger:integer], etc. It's not flexible per se, but (IMO) reads more naturally.Oswin
You can define infix functions in Standard ML. For example, you can define a function to so that the caller writes n to m instead of to(n, m). That's not exactly what you're looking for, but it does share some similarities (especially since you can write both append and to in order to call append(n to m) later).Lorielorien
Smalltalk, Objective-C, Agda, BitC, and others. See Is there a language out there in which parameters are placed inside the method name? and Has whitespace in identifiers ever been idiomatic?Excavator
@SedateAlien Objective-C has keyword arguments?Brittain
A
3

Mixfix is the most general form of what is presented above as a Smalltalk syntax feature via Objective-C. Maude's "bubble" parsing is the most clearly documented implementation of this. More generally, google for "mixfix parsing".

Allomerism answered 18/4, 2012 at 20:52 Comment(0)
M
3

As Brian Rice pointed out, mixfix notation in Maude handles this exactly. You've even got the correct syntax.

Your examples

_isAnInteger
append_to_

Would be declared in Maude as

op _IsAnInteger : Int -> Bool
op append_to_ : Int List -> List

And would be called as

5 IsAnInteger
append 5 to L
Millimicron answered 8/6, 2012 at 15:31 Comment(0)
P
1

Yes, such a thing does exist, but not in the exact syntax presented in the question.

In JavaScript it's called chaining and is commonly found in frameworks like jQuery to make it easy to create and modify DOM elements on the fly:

$('<div/>')
 .css('background', '#f00')
 .css('width', '100px')
 .css('height', '100px')
 .hide()
 .appendTo('BODY')
 .fadeIn();

The preceding jQuery snippet creates an DIV element, sets the width, height and background color, hides the DIV, appends it to the document and applies a fade-in effect.

Syntax is important. Given the following example:

(n)isAnInteger
append(n)to(m)

The parentheses obviously are mean to be function argument delimiters. But you also need function name delimiters and object/thing delimiters. A better syntax would be:

n.isAnInteger().append(n).to(m)

Which is very similar the jQuery syntax:

 $('DIV')
  .filter(':visible')
  .appendTo('#new_element');

Which will find DIV elements that are visible and copy them to the element with an id of new_element.

Polis answered 18/4, 2012 at 4:7 Comment(2)
The asker is not asking about a better syntax, they are asking about a specific syntax.Farcy
function chaining is anything but arbitrary.Rosalynrosalynd
A
1

Smalltalk is pretty close. (it is the same syntax as Objective-C, mentioned in another answer)

Message sends in Smalltalk do no use braces, but follow the form doSomething:with:

So you can write:

5 isInteger
aList appendTo: anotherList
aList at: aPosition ifAbsent: anAction

All messages are sent to an object (the receiver) that must be on the left, so you can not write:

append: aList to: anotherList   "illegal syntax"

The syntax doSomething:with: is more natural than using braces. You are less easily confused about which parameter is what. Consider for instance:

   aList.sublist( i, j )
   aList sublistFrom: i to: j

With proper naming of the methods, it's clear the the first argument is the lower index, and the second the upper index, unlike with braces.

Antinucleon answered 18/4, 2012 at 6:49 Comment(0)
Y
1

Grace is a new educational language (still in development), which supports an almost arbitrary order of arguments:

class MyObject {
    method choseBetween (a : Block<None>) and (b : Block<None>) -> None {
        if (Random.nextBoolean)
            then { a.apply } else { b.apply } }
}

which could get called as:

MyObject.new.chooseBetween { print("Yes") } and { print("No") }
Yttria answered 22/4, 2012 at 0:37 Comment(0)
G
0

Objective-C probably comes closest. Example:

- (void)setRangeStart:(int)start end:(int)end;

- (void)importDocumentWithName:(NSString *)name withSpecifiedPreferences:
(Preferences *)prefs beforePage:(int)insertPage;

Called like:

[obj setRangeStart:1 end:42];
[obj importDocumentWithName:@"foo" withSpecifiedPreferences:prefs beforePage:1];

It doesn't allow [obj 5:isAnInteger] though.

Gristmill answered 18/4, 2012 at 5:6 Comment(0)
P
0

Two languages with mixfix operators support come to my mind: Agda and BitC.

Personally answered 28/4, 2012 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.