0

This is not directly related to programming but after some time you need to brush up your generic skill with google searches. This is confusing and irritating. Do you know why the kotlin creators decided to be inconsistent with their square brackets like this?

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
ntos
  • 189
  • 10
  • Please see [this](https://meta.stackoverflow.com/q/323334/5133585) and clarify your question. – Sweeper Jan 14 '23 at 13:52
  • Note that Java does it the same way. Might just be a _"we do it the same way to not confuse our users"_ thing. In which case your question would be why Java did it like that - maybe it is easier for the parser? Ultimately, you will have to ask one of their designers. – Zabuzard Jan 14 '23 at 13:52
  • @Zabuzard ...and then OP is going to ask, "well why did Java do it that way?" and the chain of "why"s would continue infinitely :) – Sweeper Jan 14 '23 at 13:55
  • Duplicate of [this question](/q/9744269/10134209)? It goes at least as far back as C++ (started in 1979, though I don't know when templates were added). A quick web search hasn't shown up that syntax in ML, CLU, BETA, Ada, or Eiffel (which are the earlier languages I found that have generics/templates/type params). [This answer](/a/9744677/10134209) seems to indicate that they were invented for C++. – gidds Jan 14 '23 at 18:29

1 Answers1

0

In Kotlin, the angle brackets (< and >) are used to denote generic types. The placement of the angle brackets in Kotlin's syntax is consistent with the way generics are represented in the Java programming language, from which Kotlin draws much of its inspiration.

When used with a class, the angle brackets are placed after the class name, like this: MyClass<T>. This syntax is used to indicate that the class is a generic class and that it can be parameterized with a type.

When used with a function, the angle brackets are placed before the function name, like this: fun <T> myFunction(). This syntax is used to indicate that the function is a generic function, and that it can be parameterized with a type.

The placement of the angle brackets before the function name is intended to make it clear that the function is a generic function, and that the type parameter is part of the function's signature, rather than part of the function's return type or parameter types.

It's worth noting that the syntax for generics in Kotlin is consistent with Java, and it's designed to be familiar to developers who are already familiar with Java's generics. It also makes it easy for Java developers to learn and use Kotlin.

AlexT
  • 2,524
  • 1
  • 11
  • 23
Arbaz Pirwani
  • 935
  • 7
  • 22