Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does <> (angle brackets) do on class names in swift?

Tags:

In a class declaration what is the use of <> angle brackets and the parameters declared inside in swift? Like as:

public class ClassName<T: Comparable> {   } 
like image 334
Chowdhury Md Rajib Sarwar Avatar asked Dec 18 '15 02:12

Chowdhury Md Rajib Sarwar


People also ask

What does angle brackets mean in Swift?

The angle brackets tell Swift that T is a placeholder type, which will be replaced with an actual type whenever the function is called. In this case, the Swift compiler ensures that any calls to count(of:in:) are passing in an array of the same type as the element argument.

What do angle brackets mean in coding?

What Does Angle Bracket Mean? The angle bracket (< or >), which is also called an “inequality sign” for its use in mathematics, is a kind of sideways caret that can be used to include tags or pieces of code. This ASCII set of characters is common in web design and other types of coding projects.

What do angle brackets mean in typescript?

Generic Classes A generic class has a similar shape to a generic interface. Generic classes have a generic type parameter list in angle brackets ( <> ) following the name of the class.

Which brackets are used for generics?

We use angle brackets ” to specify parameter types in the generic function definition. Then to call the function, we just pass the expecter type as a parameter.


1 Answers

It makes the class generic. The Swift standard library doesn't have a lot of examples of generic classes, but it has some very well-known generic structs and enums:

public struct Array<Element> : CollectionType, MutableCollectionType, _DestructorSafeContainer  public struct Dictionary<Key : Hashable, Value> : CollectionType, DictionaryLiteralConvertible  public enum Optional<Wrapped> : _Reflectable, NilLiteralConvertible 

Read more about generics under “Generics” in The Swift Programming Language.

like image 118
rob mayoff Avatar answered Sep 20 '22 20:09

rob mayoff