A type alias is a short, descriptive name for a type whose real name is longer or less descriptive than the alias. Such type members can help clarify code.
Questions tagged [type-alias]
319 questions
42
votes
2 answers
C++20 bit_cast vs reinterpret_cast
According to the last meeting of the ISO C++ Committee, bit-cast will be introduced in C++20 standard.
I know that reinterpret_cast is not suitable for this job due to type aliasing rules but my question is why did they choose not to extend the…
![](../../users/profiles/9295224.webp)
bogdan tudose
- 894
- 6
- 20
30
votes
1 answer
Returning constrained generics from functions and methods
I would like to create a function that returns an object that conforms to a protocol, but the protocol uses a typealias. Given the following toy example:
protocol HasAwesomeness {
typealias ReturnType
func hasAwesomeness() ->…
![](../../users/profiles/492847.webp)
edelaney05
- 6,640
- 6
- 38
- 63
21
votes
1 answer
Does Rust have an equivalent of C's typedef?
C offers the keyword typedef which lets you alias another type:
typedef unsigned int uint;
This basically makes uint an alias for unsigned int. This also works with more complex types and structures too. Does Rust have a similar language feature?…
![](../../users/profiles/6360938.webp)
typos
- 3,422
- 7
- 26
- 47
17
votes
5 answers
Is it bad form to make new types/datas for clarity?
I would like to know if it is bad form to do something like this:
data Alignment = LeftAl | CenterAl | RightAl
type Delimiter = Char
type Width = Int
setW :: Width -> Alignment -> Delimiter -> String -> String
Rather than something like…
![](../../users/profiles/11672176.webp)
BryceTheGrand
- 657
- 1
- 9
17
votes
2 answers
In Swift, how to extend a typealias?
I have a typealias:
typealias BeaconId = [String: NSObject]
I'd like to extend it by doing something like:
extension BeaconId {}
But this throws a compile error:
Constrained extension must be declared on the unspecialized generic type…
![](../../users/profiles/1672161.webp)
abc123
- 7,069
- 7
- 40
- 71
14
votes
2 answers
Aliasing with Webpack 4 and awesome-typescript loader not working
I'm currently having issue getting aliasing to work properly. From my understanding, to get aliasing to work properly with webpack you have to:
Versions
"typescript": "2.8.3",
"webpack": "4.16.2",
"webpack-cli": "3.1.0",
…
![](../../users/profiles/391465.webp)
darewreck
- 2,375
- 4
- 33
- 61
14
votes
4 answers
Kotlin type-safe typealiases
I use typealiases in my Kotlin code a lot, but I wonder if I can enforce type-safety on them.
typealias Latitude = Double
typealias Longitude = Double
fun someFun(lat: Latitude, lon: Longitude) {...}
val lat: Latitude = 12.34
val lon: Longitude =…
![](../../users/profiles/2660039.webp)
ekaerovets
- 1,038
- 10
- 17
14
votes
1 answer
Does Rust have an idiomatic equivalent to F# typedefs?
I'm re-writing existing code of mine in Rust 1.6 and I've found it very convenient in the source language to label a type by typedef. For example, in my card game I have a rank value in F# defined as:
type Rank = uint8
![](../../users/profiles/2736686.webp)
Erik Uggeldahl
- 828
- 9
- 22
14
votes
3 answers
Using sizeof on a typedef instead of a local variable
Like in this example (in C):
typedef int type;
int main()
{
char type;
printf("sizeof(type) == %zu\n", sizeof(type)); // Outputs 1
}
The output is always the size of the local variable type.
When C++ removed the need to write struct before…
![](../../users/profiles/3787875.webp)
wefwefa3
- 3,542
- 2
- 24
- 50
13
votes
4 answers
Is it standard practice to use type aliases to indicate parameter semantics?
Items in tuples don't have names, which means that you often don't have a clear way to document the meanings of each item.
For instance, in this discriminated union:
type NetworkEvent =
| Message of string * string * string
| ...
I want to make it…
![](../../users/profiles/388626.webp)
Rei Miyasaka
- 6,796
- 5
- 37
- 65
13
votes
1 answer
Is it possible to create a template alias?
Consider the following code:
template< template< typename ... > class ... Ts >
struct unite
{
template< typename ... T >
struct type
: Ts< T ... > ...
{ };
};
// This does not work as ::type does not name a type, but a…
![](../../users/profiles/2073257.webp)
Daniel Frey
- 52,317
- 11
- 110
- 168
12
votes
1 answer
Unexpected behavior with template type alias in VS2015
The following code compiles with VS15 Community and prints out "Hello".
#include
#include
template
using void_template_alias_t = void;
template
using Func = std::function
![](../../users/profiles/1624629.webp)
DaBrain
- 2,889
- 2
- 17
- 28
11
votes
1 answer
How to access a Java static method from Scala given a type alias for that class it resides in
Given the type-alias type Cal = java.util.Calendar how can the static getInstance method be accessed? I tried the following in Scala REPL:
scala> type Cal = java.util.Calendar
defined type alias Cal
scala> Cal.getInstance
:8: error: not…
![](../../users/profiles/881945.webp)
Tim Friske
- 1,897
- 1
- 15
- 27
11
votes
1 answer
Define a typealias for the whole project
In which file can I define a typealias that works in the whole project, etc.
typealias S = String
![](../../users/profiles/4475130.webp)
Tomas Gudmundsson
- 183
- 1
- 10
10
votes
1 answer
Overhead of `using`
For my problem, I can use the using directive in two ways. They basically boil down to these options:
template
struct A {
private:
// Define our types
using WrapperType = Wrapper;
public:
U *operator()(U *g) const {
…
![](../../users/profiles/3309610.webp)
bremen_matt
- 5,606
- 3
- 33
- 70