What is the origin of the parmname: value syntax for passing paramters?

80 Views Asked by At

I learned groovy last summer and discovered the syntax for constructors that you can specify the parameter name with a value and initialize that value. For instance:

Team team = new Team(name: "Bengals")

At the end of the year, I upgraded IntelliJ and this syntax started showing up in my java functions with overloaded functions with different parameter lists. (This is not implemented very well. It's confusing when I try to change the values. I've figured it out, but poorly implemented.)

team.chant = (msg: "Who dey?");

Which is interesting, because I'm learning Swift 3 and it uses the same syntax.

It seems unlikely that Swift 3 was influenced by Groovy, so I'm curious. What language is putting pressure on these languages to support the parmname: value syntax?

It seems to me that Swift is based on what little I know about Smalltalk. Does Smalltalk support this parameter syntax?

1

There are 1 best solutions below

0
On

I don't know Swift nor Groovy, so I can only respond for the Smalltalk part.

Smalltalk does not support that syntax, but has keyword messages that read very much like that.

For instance,

team := Team new name: 'Bengals'.
team chant msg: 'who dey?'

is valid in Smalltalk.

It is not that the names of params are in the command, but the command itself is #name:, which matches the name of some instance variable but is a method to set its value... So as I said, it reads the same but the meaning is very different.