Type aliasing
Type aliasing is a feature in some programming languages that allows the creation to refer to a type using another name. It does not create a new type hence does not increase type safety. It can be used to shorten a long name. Programing languages which allows type aliasing include C++, D, Dart, Elixir, Elm, F#, Go, Hack, Haskell, Julia, Kotlin, Nim, Python, Rust, Scala, Swift and TypeScript.
Example
C++
C++ features type aliasing using the using
keyword.
using Distance = int;
D
D features type aliasing using the alias
keyword.[1]
alias Distance = int;
Dart
Dart features type aliasing using the typedef
keyword.[2]
typedef Distance = int;
Elixir
Elixir features type aliasing using @type
.[3]
@type Distance :: integer
Elm
Elm features type aliasing using type alias
.
type alias Distance = Int
F#
F3 features type aliasing using the type
keyword.
type Distance = int
Go
Go features type aliasing using the type
keyword.
type Distance int
Hack
Hack features type aliasing using the newtype
keyword.[4]
newtype Distance = int;
Haskell
Haskell features type aliasing using the type
keyword.[5]
type Distance = Int;
Julia
Julia features type aliasing.[6]
const Distance = Int
Kotlin
Kotlin features type aliasing using the typealias
keyword.[7]
typealias Distance = Int
Nim
Nim features type aliasing.[8]
type Distance* = int
Python
Python features type aliasing.[9]
Vector = list[float]
Type aliases may be marked with TypeAlias to make it explicit that the statement is a type alias declaration, not a normal variable assignment.
from typing import TypeAlias Vector: TypeAlias = list[float]
Rust
Rust features type aliasing using the type
keyword.[10]
type Point = (u8, u8);
Scala
Scala can create type aliases using opaque types.[11]
object Logarithms: opaque type Logarithm = Double
Swift
Swift features type aliasing using the typealias
keyword.
typealias Distance = Int;
TypeScript
TypeScript features type aliasing using the type
keyword.[12]
type Distance = number;
References
- ↑ "Alias Alias - D Programming Language". https://dlang.org/library/std/meta/alias.html.
- ↑ "Typedefs" (in en). https://dart.dev/language/typedefs.
- ↑ "Typespecs and behaviours" (in en). https://elixir-lang.org/getting-started/typespecs-and-behaviours.html.
- ↑ "Types: Type Aliases". https://docs.hhvm.com/hack/types/type-aliases.
- ↑ "Type synonym - HaskellWiki". https://wiki.haskell.org/Type_synonym.
- ↑ "Types · The Julia Language". https://docs.julialang.org/en/v1/manual/types/#Type-Aliases-1.
- ↑ "Type aliases | Kotlin". https://kotlinlang.org/docs/type-aliases.html.
- ↑ "Nim by Example - Types". https://nim-by-example.github.io/types/.
- ↑ "typing — Support for type hints". Python Software Foundation. https://docs.python.org/3/library/typing.html#type-aliases.
- ↑ "Type aliases - The Rust Reference". https://doc.rust-lang.org/reference/items/type-aliases.html.
- ↑ "Opaque Types". https://docs.scala-lang.org/scala3/book/types-opaque-types.html.
- ↑ "Documentation - Everyday Types" (in en). https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-aliases.
Original source: https://en.wikipedia.org/wiki/Type aliasing.
Read more |