1
$\begingroup$

For typed languages, what advantages/disadvantages are there to specifying the parameter type after the parameter name?

For languages like C and Java, the type comes first:

void myfunc(TypeA param1, TypeB param2);

For other languages like Scala, Rust, etc, the type comes after the parameter name:

fn myfunc(param1: TypeA, param2: TypeB) {}

What advantages are there to this approach? I could see in languages where specifying a type is optional (or inferred) and can be left off, but at least for Rust types have to be specified in a function's parameter list.

$\endgroup$
  • 2
    $\begingroup$ I don't think this question should have been closed as primarily opinion-based. The short answer is that there is no fundamental reason why it makes a difference, it's just a historical accident. But that's an answer, not a reason to close. And there's actually a longer answer, which is that the colon-type syntax allows complex type expressions after the colon (e.g. A * List<B>), whereas C-like syntax is not purely prefix and does not easily allow forming type expressions. $\endgroup$ – Gilles 'SO- stop being evil' May 27 '19 at 19:01
4
$\begingroup$

It's completely arbitrary.

In a given programming language, one would hope that the same convention is used throughout, so either int f(int x) or f(x: int): int but not int f(x: int) or f(int x): int; but even that is arbitrary.

$\endgroup$
  • 1
    $\begingroup$ Objective-C has for methods an (optional but almost always used) name for the caller, followed by (type)parameter - but uses C conventions for plain functions. $\endgroup$ – gnasher729 May 27 '19 at 21:10
  • 1
    $\begingroup$ They probably had good intentions... $\endgroup$ – Yuval Filmus May 27 '19 at 21:12
  • 1
    $\begingroup$ The syntax for "blocks" (similar to closures) in Objective C has its own website named "f***ingblocksyntax.com" :-) $\endgroup$ – gnasher729 May 27 '19 at 22:00
3
$\begingroup$

No, in C it’s not type followed by parameter name. It is a declaration, where the parameter name can be found in many positions.

int f (int (*p)());
$\endgroup$
3
$\begingroup$

This is largely an arbitrary choice. One language did it some way, a long time ago, and then many but not all other languages copied a familiar syntax. That being said, the choice is not completely arbitrary. Putting the type before or after the variable name is largely cosmetic, but there are more differences than this between the C-style syntax and the Pascal-style syntax used in languages like Rust and Scala.

In early languages where variable declarations had a type, the syntax of a declaration was of the form Type Name where Type is a reserved word and Name is a name chosen by the programmer. Here the order is not arbitrary: first you say what kind of thing you're declaring, then you give it a name. Compilers at the time were very primitive and needed to know early on what they were parsing; writing INTEGER X let the compiler know immediately that it had to work on an integer declaration, whereas X INTEGER would have required the compiler to first remember that it had seen X and only after that it would have known that it was supposed to declare an integer variable. Languages of the 1950s and 1960s such as ALGOL and FORTRAN tended to use this syntax. Interestingly, at the time, in programming language descriptions, “type” had a more general meaning that it has today: today it has a specific technical meaning that's more or less synonym with “datatype”, but at the time it had its ordinary English meaning, which could cover things like integer and real, but also things like label and statement.

C extended this syntax in a way that allowed defining compound types. In C, the syntax of a variable declaration mimics the syntax of its use. For example, int x declares an integer called x, and int *p declares a pointer-to-integer called p, and the expression *p has the type int. This syntax is not purely prefix because accessors can be suffixes: for example a[1] is an int if a is an array of int values, so the syntax to declare an array of integers is int a[].

C became a very popular language, so many languages after it retained a similar syntax for several things including variable declarations and types.

The colon syntax goes back to at least Pascal in 1971. This is the syntax used by computer scientists at least since the early 1970s. I suspect that it started in theory papers and was adopted by Pascal and later by other languages, but for all I know it may have originated with Pascal. Why colon to denote that a value belongs to a type? discusses the origin of $x : T$ in computer science. Modern languages that draw from modern (or not-so-modern-anymore) theory tend to use the syntax that's familiar to computer scientists, including ML, Scala, Swift, Rust, and many others. Haskell is somewhat unusual in using a double colon :: where others use a colon :.

The order $x : T$ vs $T : x$ is arbitrary: these days, compilers don't mind, and for humans, it's just a matter of habit. However, having a punctuation sign between the variable name and the type is not arbitrary. Most languages that use this syntax also allow the left-hand side to be an expression, to form a type annotation. More importantly, the right-hand side is a type expression, not just a type name. Recall that C supports declarations of variables with compound types, but the syntax is somewhat awkward, with the variable name ending up somewhere in the middle. C's syntax does not work with compound types that are made of more than one type part. For example, in ML, x : bool * int * string means that x is a triple consisting of a boolean, an integer and a string. Haskell writes this x :: (Bool, Integer, String). Both the ML and the Haskell way allow building complex types. The C approach fails: where do you put the variable name?

Languages such as Java and C# deviate from the C syntax by putting the whole type information before the variable name: int[] a, (bool, int, string) x, List<int> v. With no separator, it's impractical to allow both sides to have an arbitrary length, but these languages do support type expressions, which can be used to declare a variable.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.