Skip to content
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

StaticString until StaticBigInt #62

Closed
oscbyspro opened this issue Aug 17, 2023 · 9 comments
Closed

StaticString until StaticBigInt #62

oscbyspro opened this issue Aug 17, 2023 · 9 comments
Labels
addition oh, so shiny! availability in an SDK near you subtraction huh? where did it go?
Milestone

Comments

@oscbyspro
Copy link
Owner

oscbyspro commented Aug 17, 2023

Lowering the minimum platform requirements (#61) means that StaticBigInt be disabled by default. I'll add an ExpressibleByStringLiteral conformance to NBKDoubleWidth in its place. It will work like init(_:radix:) except it will also support the 3 most common radix prefixes: 0b, 0o and 0x. So, ideally, the change is reduced to adding quotation marks.

@oscbyspro oscbyspro added addition oh, so shiny! subtraction huh? where did it go? labels Aug 17, 2023
@oscbyspro oscbyspro added this to the v0.10.0 milestone Aug 17, 2023
@oscbyspro oscbyspro changed the title ExpressibleByStringLiteral until StaticBigInt StringLiteral until StaticBigInt Aug 17, 2023
@oscbyspro oscbyspro added the availability in an SDK near you label Aug 17, 2023
@oscbyspro
Copy link
Owner Author

oscbyspro commented Aug 17, 2023

I suppose one somewhat surprising thing is that conforming to ExpressibleByStringLiteral causes UInt256("123") to call init(stringLiteral:) as opposed to init?(_ description:) from LosslessStringConvertible, because there's a special case for literals. So UInt256(" 23") crashes instead of returning nil. Dunno, if that's a problem. It probably isn't given that literals are known at compile time.

@ypopovych
Copy link
Contributor

ypopovych commented Aug 17, 2023

one remark - enable String Literal for SBI too. Right now it will be disabled when the SBI flag is set. They can both be present.

  • +SBI: StaticBigInt + String
  • -SBI: Int64 + String

@oscbyspro
Copy link
Owner Author

oscbyspro commented Aug 17, 2023

You're right that these methods can coexist, but should they when one is clearly better? I suppose I can alleviate some migration pain by conditionally deprecating it. Or, do you have a use case in mind that prefers init(stringLiteral:) undeprecated? I'm thinking something like:

#if swift(>=5.8) && SBI
@available(swift, deprecated: 5.8, message: "Use an integer literal instead.")
#endif
@inlinable public init(stringLiteral source: StringLiteralType) { ... }

@ypopovych
Copy link
Contributor

ypopovych commented Aug 17, 2023

I think it can be used with some APIs which return strings with prefixes even if we have StaticBigInt. Maybe JSON-based API with big integers in it as hex values. I don't think it should be deprecated.

It's better than radix methods because it checks the prefix of the string.

@oscbyspro
Copy link
Owner Author

oscbyspro commented Aug 17, 2023

In that case, wouldn't it be better to have a dedicated method? Perhaps with some format parameters.

#if swift(>=5.8) && SBI
@available(swift, deprecated: 5.8, message: "Use an integer literal instead.")
#endif
@inlinable public init(stringLiteral: StringLiteralType) {
    self.init(fromStringLiteralButBetter: stringLiteral, optionalRadixOrSomething: 10)
}

@ypopovych
Copy link
Contributor

the method is good too. But I think we shouldn't force developers. If someone wants to use string literals - ok. Maybe they want to support older OSes without ifdefs even when they have StaticBigInt

@oscbyspro
Copy link
Owner Author

I've been thinking. I'll keep depreciating init(stringLiteral:) from 5.8. I'm open to a radix decoding initializer, but init(stringLiteral:) isn't it. The reason is twofold: A) the signature is non-descriptive. B) if this method should be a member of [U]Int256, then it should also be a member of [U]Int[8-64]. I own neither [U]Int[8-64] nor ExpressibleByStringLiteral, and conforming unowned types to unowned protocols is nasty.

@oscbyspro oscbyspro changed the title StringLiteral until StaticBigInt StaticString until StaticBigInt Aug 18, 2023
@oscbyspro
Copy link
Owner Author

I changed NBKDoubleWidth/StringLiteralType to StaticString from String.

@oscbyspro
Copy link
Owner Author

I'll remove the deprecation because there isn't an automatic way of enabling StaticBigInt via availability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition oh, so shiny! availability in an SDK near you subtraction huh? where did it go?
Projects
None yet
Development

No branches or pull requests

2 participants