Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/p-x9/aliasmacro
๐ญ A Swift macros for defining aliases for types, functions, variables, etc.
https://github.com/p-x9/aliasmacro
macro spm swiftmacros swiftpackage swiftpackagemanager
Last synced: 24 days ago
JSON representation
๐ญ A Swift macros for defining aliases for types, functions, variables, etc.
- Host: GitHub
- URL: https://github.com/p-x9/aliasmacro
- Owner: p-x9
- License: mit
- Created: 2023-06-16T19:57:55.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-06T02:22:46.000Z (7 months ago)
- Last Synced: 2024-10-11T22:50:28.455Z (about 1 month ago)
- Topics: macro, spm, swiftmacros, swiftpackage, swiftpackagemanager
- Language: Swift
- Homepage:
- Size: 83 KB
- Stars: 30
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AliasMacro
Swift Macro for defining aliases for types, functions, or variables.
[![Github issues](https://img.shields.io/github/issues/p-x9/AliasMacro)](https://github.com/p-x9/AliasMacro/issues)
[![Github forks](https://img.shields.io/github/forks/p-x9/AliasMacro)](https://github.com/p-x9/AliasMacro/network/members)
[![Github stars](https://img.shields.io/github/stars/p-x9/AliasMacro)](https://github.com/p-x9/AliasMacro/stargazers)
[![Github top language](https://img.shields.io/github/languages/top/p-x9/AliasMacro)](https://github.com/p-x9/AliasMacro/)## Usage
A macro that can be attached to a type, function or variable.
### Variable
You can define an alias for a variable as follows
```swift
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())`.```swift
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 ":".
```swift
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.
```swift
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.
```swift
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 = .expertstatic func ultimate(level: Int) -> Self {
.master(level)
}
}
```### associatedtype
```swift
protocol APIRequest {
@Alias("Reply")
associatedtype Response
}
```โโโ
```swift
protocol APIRequest {
associatedtype Responsetypealias 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](https://github.com/apple/swift-evolution/blob/main/proposals/0389-attached-macros.md#restrictions-on-arbitrary-names)```swift
@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
```swift
@Alias("hello")
@Alias("ใใใซใกใฏ")
var text: String = "Hello"
```### Specify Access Modifier of Alias
You can specify an alias access modifier as follows.
```swift
@Alias("hello", access: .public)
private var text: String = "Hello"
```If set "inherit", it will inherit from the original definition.
```swift
@Alias("hello", access: .inherit)
private var text: String = "Hello"
```## License
AliasMacro is released under the MIT License. See [LICENSE](./LICENSE)