-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab14d16
commit 9726bfa
Showing
6 changed files
with
429 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"pins" : [ | ||
{ | ||
"identity" : "swift-syntax", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-syntax.git", | ||
"state" : { | ||
"revision" : "64889f0c732f210a935a0ad7cda38f77f876262d", | ||
"version" : "509.1.1" | ||
} | ||
} | ||
], | ||
"version" : 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,26 @@ | ||
// The Swift Programming Language | ||
// https://docs.swift.org/swift-book | ||
|
||
/// A macro that produces both a value and a string containing the | ||
/// source code that generated the value. For example, | ||
/// A macro that adds a `copy()` method into a type bearing the annotation. For example: | ||
/// | ||
/// #stringify(x + y) | ||
/// @Copyable | ||
/// struct User { | ||
/// let name: String | ||
/// let age: Int | ||
/// } | ||
/// | ||
/// produces a tuple `(x + y, "x + y")`. | ||
@freestanding(expression) | ||
public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "CopyableMacroMacros", type: "StringifyMacro") | ||
/// updates the struct as follows: | ||
/// | ||
/// struct User { | ||
/// let name: String | ||
/// let age: Int | ||
/// | ||
/// func copy(name: String? = nil, age: Int? = nil) -> Self { | ||
/// .init( | ||
/// name: name ?? self.name | ||
/// age: age ?? self.age | ||
/// ) | ||
/// } | ||
/// } | ||
@attached(member, names: named(copy)) | ||
public macro Copyable() = #externalMacro(module: "CopyableMacroMacros", type: "CopyableMacroMacro") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,30 @@ | ||
import CopyableMacro | ||
|
||
let a = 17 | ||
let b = 25 | ||
@Copyable | ||
final class Sample { | ||
var x: Int | ||
let y: Double | ||
|
||
let (result, code) = #stringify(a + b) | ||
init(x: Int, y: Double) { | ||
self.x = x | ||
self.y = y | ||
} | ||
} | ||
|
||
print("The value \(result) was produced by the code \"\(code)\"") | ||
let sample = Sample(x: 1, y: 2) | ||
|
||
@Copyable | ||
struct StructSample { | ||
var firstProp: Bool | ||
let secondProp: String | ||
//let t: String? = nil | ||
var fourth: String { | ||
secondProp | ||
} | ||
let fifth: [Int] | ||
|
||
} | ||
|
||
let structSample = StructSample(firstProp: true, secondProp: "secondProp", fifth: [0, 1]) | ||
|
||
print(sample) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.