-
Notifications
You must be signed in to change notification settings - Fork 458
Implement Additional Feedback on PR for SR-11580 #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,24 +11,20 @@ | |
//===----------------------------------------------------------------------===// | ||
|
||
public extension FloatLiteralExprSyntax { | ||
var floatingValue: Double { | ||
return potentialFloatingValue! | ||
} | ||
|
||
fileprivate var potentialFloatingValue: Double? { | ||
var floatLiteralValue: Double? { | ||
let floatingDigitsWithoutUnderscores = floatingDigits.text.filter { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless there is other validation of The rules for where underscores are allowed (where, how many consecutively, etc.) aren't easily found and, even when found, may be inaccurate. For instance, according to PEP 515 (which added underscore separators to numeric literals in Python and surveyed the rules in other languages), Swift documentation says that any number of underscores are allowed between digits, but in fact they are allowed in Swift at the end of literals after the last digit as well. This will likely require either experimentation or a careful reading of the Swift compiler's code. If you undertake this work, please do document it somewhere for posterity. Similar issues may apply to integer literals too, although to a lesser degree because the syntax is less complex. Even if there turn out to be no such issues, it will be good to undertake the exercise of proving that and documenting it. |
||
$0 != "_" | ||
} | ||
return Double(floatingDigitsWithoutUnderscores) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the As an example, There are other examples listed here that will likely require some experimentation to ensure are still applicable and as described. It is likely that I have missed some of the differences too (for instance, I never did list the differences in behavior when a float literal is too large to be represented by the desired type versus a string). Again, similar issues may apply to integer literals, and even if there turn out to be no such issues, it would be important to prove and document that. |
||
} | ||
} | ||
|
||
public extension IntegerLiteralExprSyntax { | ||
var integerValue: Int { | ||
return potentialIntegerValue! | ||
var isValid: Bool { | ||
floatLiteralValue != nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest removal of It may be, however, that in your experimentation regarding the issues above, you end up having a sufficiently complete implementation validating Swift's syntactic rules surrounding literals that you can implement The same comment applies to the other |
||
} | ||
} | ||
|
||
fileprivate var potentialIntegerValue: Int? { | ||
public extension IntegerLiteralExprSyntax { | ||
var integerLiteralValue: Int? { | ||
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, so here's my one comment on API design. I think users will want to know what value is represented by their numeric literal expression for types other than the default Therefore, we can use a (generic) method instead of a property to allow the user to pass a variable indicating the desired type: func integerLiteralValue<T>(ofType _: T.Type) -> T
where T: ExpressibleByIntegerLiteral { ... } Users could then write: func integerLiteralValue<T>(ofType _: T.Type = IntegerLiteralType) -> T
where T: ExpressibleByIntegerLiteral { ... } Now, the use site simply differs from what you have currently in this PR by a pair of parentheses. Pretty good. However, if you were to try to implement this API, you'd find that you actually need to constrain However, you'd run into another problem when it comes to integer literals, because if you want to pass the |
||
let text = digits.text | ||
let (prefixLength, radix) = IntegerLiteralExprSyntax.prefixLengthAndRadix(text: text) | ||
let digitsStartIndex = text.index(text.startIndex, offsetBy: prefixLength) | ||
|
@@ -65,16 +61,8 @@ public extension IntegerLiteralExprSyntax { | |
return (decimalPrefix.count, decimalRadix) | ||
} | ||
} | ||
} | ||
|
||
public extension IntegerLiteralExprSyntax { | ||
var isValid: Bool { | ||
potentialIntegerValue != nil | ||
} | ||
} | ||
|
||
public extension FloatLiteralExprSyntax { | ||
var isValid: Bool { | ||
potentialFloatingValue != nil | ||
integerLiteralValue != nil | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One benefit of this PR is that this property is no longer needed.