/AliasMacro

๐ŸŽญ A Swift macros for defining aliases for types, functions, variables, etc.

Primary LanguageSwiftMIT LicenseMIT

AliasMacro

Swift Macro for defining aliases for types, functions, or variables.

Github issues Github forks Github stars Github top language

Usage

A macro that can be attached to a type, function or variable.

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"

Function/Method

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"

Customize Argument Label

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"

Enum Case

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)
    }
}

associatedtype

protocol APIRequest {
    @Alias("Reply")
    associatedtype Response
}

โ†“โ†“โ†“

protocol APIRequest {
    associatedtype Response

    typealias Reply = Response
}

Class/Struct/Enum/Actor

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 in names 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

Multiple aliases can be defined by adding multiple macros as follows

@Alias("hello")
@Alias("ใ“ใ‚“ใซใกใฏ")
var text: String = "Hello"

Specify Access Modifier of Alias

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"

License

AliasMacro is released under the MIT License. See LICENSE