Swift Macro for defining aliases for types, functions, or variables.
A macro that can be attached to a type, function or variable.
You can define an alias for a variable as follows
class SomeClass {
@Alias("title")
var text: String = "hello"
}
/* โโโโโ */
let someClass = SomeClass()
print(text) // => "hello"
print(title) // => "hello"
You can define an alias for a function as follows.
In this way, you can call both hello("aaa", at: Date())
and ใใใซใกใฏ("aaa", at: Date())
.
class SomeClass {
@Alias("ใใใซใกใฏ")
func hello(_ text: String, at date: Date) {
/* --- */
print(text)
}
}
/* โโโโโ */
let someClass = SomeClass()
someClass.hello("aaa", at: Date()) // => "aaa"
someClass.ใใใซใกใฏ("aaa", at: Date()) // => "aaa"
Argument labels can also be customized by separating them with ":".
class SomeClass {
@Alias("ใใใซใกใฏ::ใใค")
func hello(_ text: String, at date: Date) {
/* --- */
print(text)
}
@Alias("ใใใซใกใฏ2:_:_") // Omit argument labels
func hello2(_ text: String, at date: Date) {
/* --- */
print(text)
}
@Alias("ใใใซใกใฏ3::ๅฎ") // The first argument label is inherited. The second is customized
func hello3(_ text: String, at date: Date, to: String) {
/* --- */
print(text)
}
}
/* โโโโโ */
let someClass = SomeClass()
someClass.hello("aaa", at: Date()) // => "aaa"
someClass.ใใใซใกใฏ("aaa", ใใค: Date()) // => "aaa"
someClass.hello2("aaa", at: Date()) // => "aaa"
someClass.ใใใซใกใฏ2("aaa", Date()) // => "aaa"
someClass.hello3("aaa", at: Date(), to: "you") // => "aaa"
someClass.ใใใซใกใฏ3("aaa", at: Date(), ๅฎ: "ใใชใ") // => "aaa"
You can define alias for enum case.
For example, suppose we define the following.
enum Difficulty {
@Alias("beginner")
case easy
@Alias("normal")
case medium
@Alias("challenge")
case hard
@Alias("extreme")
case expert
@Alias("ultimate")
case master(level: Int)
}
At this time, the macro is expanded as follows.
enum Difficulty {
case easy
case medium
case hard
case expert
case master(level: Int)
static let beginner: Self = .easy
static let normal: Self = .medium
static let challenge: Self = .hard
static let extreme: Self = .expert
static func ultimate(level: Int) -> Self {
.master(level)
}
}
protocol APIRequest {
@Alias("Reply")
associatedtype Response
}
โโโ
protocol APIRequest {
associatedtype Response
typealias Reply = Response
}
For example, the ViewController
can also be referenced as a VC
by writing the following.
(If this macro is used for type, it is defined internally simply using typealias
.)
Warning
PeerMacro
with arbitrary specified innames
cannot be used in global scope. Restrictions on arbitrary names
@Alias("VC")
class ViewContoroller: UIViewController {
/* --- */
}
/* โโโโโ */
print(ViewContoroller.self) // => "ViewController"
print(VC.self) // => "ViewController"
Multiple aliases can be defined by adding multiple macros as follows
@Alias("hello")
@Alias("ใใใซใกใฏ")
var text: String = "Hello"
You can specify an alias access modifier as follows.
@Alias("hello", access: .public)
private var text: String = "Hello"
If set "inherit", it will inherit from the original definition.
@Alias("hello", access: .inherit)
private var text: String = "Hello"
AliasMacro is released under the MIT License. See LICENSE