What other programming languages have a Smalltalk-like message-passing syntax?
Asked Answered
W

7

24

What languages are there with a message-passing syntax similar to Smalltalk's? Objective-C is the only one I'm familiar with. Specifically, I was wondering if any other language implementations exist which allow for syntax in a form like: [anObject methodWithParam:aParam andParam:anotherParam], having messages that allow for named parameters as part of method definitions.

In general I find that this syntax can be conducive to more consistent method names that more clearly show the methods' intent, and that the price you pay in wordiness is generally worth it. I would love to know if there are any other languages that support this.

Whitehead answered 26/5, 2011 at 21:39 Comment(2)
Eh, not sure how this question is too localized.Killam
+1 for the question - it's a very refreshing change from the usual "ooh, I'm scared, why can't I just use Java like I learned in school" type question.Hyperpyrexia
A
14

Here is a list of languages supporting keyword messages syntax similar to Smalltalk:

Ashburn answered 26/5, 2011 at 23:7 Comment(3)
What about languages that provide a thin layer over some more popular ones to allow this syntax. I'm thinking of things like Bistro (en.wikipedia.org/wiki/Bistro_(programming_language)) for Java. I think this project is pretty old though.Whitehead
News section at Bistro website says last release was 14 October 2010. So this project is not really dead.Ashburn
Objective-J is a layer over JavaScript.Hyperpyrexia
M
5

In addition to the other languages mentioned here, Fancy:

osna = City new: "Osnabrück"
p = Person new: "Christopher" age: 23 city: osna
p println

berlin = City new: "Berlin"
p go_to: berlin
p println
Movable answered 3/6, 2011 at 1:6 Comment(0)
J
3

See e.g. Self.

Also, many languages support optional named parameters, e.g. Python or C# (starting with v4).

Janeljanela answered 26/5, 2011 at 21:46 Comment(1)
Objective-C explicitly does not allowed optional named parameters which, I think, is relevant to OP's question.Demars
M
2

Python and Common Lisp (probably among others) allow for keyword arguments. You can make calls to functions which include the parameter names.

These are not equivalent to Obj-C's method names, because the keyword args ignore position, but they answer your question about readability.*

make_an_omelet(num_eggs=2, cheese=u"Gruyère", mushrooms=True)

(make-a-salad :greens 'boston-lettuce 
              :dressing 'red-wine-vinaigrette 
              :other-ingredients '(hazelnuts dried-apricots))

This is not, of course, message passing, just plain old function calling.


*They have other uses than this, such as specifying default values.

Montelongo answered 26/5, 2011 at 22:11 Comment(2)
Keyword argument based message passing also often allows some of the keywords to be omitted and/or default argument values to be specified.Demars
Well, though I'm not excellent at English very much, but I think word order is very important to make accurate expression in English. And should be also important in readability problem.Stibnite
D
0

Ada supports named parameters.

function Minimum (A, B : Integer) return Integer is
begin
   if A <= B then
      return A;
   else
      return B;
   end if;
end Minimum;

Then call:

Answer := Minimum (A=>100, B=>124);
Destructible answered 27/5, 2011 at 19:20 Comment(1)
Named parameter is not Smalltalk-like syntax, not even close.Stibnite
S
-1

Ruby can send messages to objects in order to call their methods, pretty much like objc does:

class Foo
  def bar a,b
    a + b
  end
end

f = Foo.new
f.send(:bar, a=4, b=5)
>> 9

Indeed, among other things, this makes possible the integration between Cocoa and Ruby in MacRuby

Selfless answered 26/5, 2011 at 21:56 Comment(2)
I'm definitely finding many popular languages that support the optional named parameters. But...and I think I'm having trouble putting a name to the concept of smalltalk-like method names...that type of explicitness seems to be missing.Whitehead
Yah -- named parameters and keyword parameters are not at all like Objective-C's message passing scheme.Demars
I
-1

Erlang do not claim to be object oriented but message passing is a key concept in that language.

Insouciant answered 29/5, 2011 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.