The poor man's feature flag #57
Replies: 14 comments 16 replies
-
It's even possible the stack them: //*TAG0*///*TAG1*/print("1st")
//*TAG0*//*/TAG1*/print("2nd")
/*/TAG0*///*TAG1*/print("3rd")
/*/TAG0*//*/TAG1*/print("4th") The above maps to something like: let TAG0 = true
let TAG1 = true
switch (TAG0, TAG1) {
case (false, false): print("1st")
case (false, true ): print("2nd")
case (true, false): print("3rd")
case (true, true ): print("4th") } |
Beta Was this translation helpful? Give feedback.
-
Caveat: it does not work on |
Beta Was this translation helpful? Give feedback.
-
With some modifications to the original regex, you can write feature blocks too:
Edit: this one cannot be stacked print("Hello")
/*/TAG*//*/
print("Bave")
*///*/TAG*/
//*TAG*/print("New")
/*/TAG*/print("Old")
print("World") print("Hello")
//*TAG*///*
print("Bave")
//*//*TAG*/
/*/TAG*/print("New")
//*TAG*/print("Old")
print("World") |
Beta Was this translation helpful? Give feedback.
-
This regex is a bit tidier but the pattern has one more character:
Edit: this one can be stacked in-line 🥳 print("Hello")
/*/TAG*//*/*
print("Bave")
//**/*/TAG*/
//*TAG*/print("New")
/*/TAG*/print("Old")
print("World") print("Hello")
//*TAG*///**
print("Bave")
//**//*TAG*/
/*/TAG*/print("New")
//*TAG*/print("Old")
print("World") |
Beta Was this translation helpful? Give feedback.
-
Oh, I like this regex and pattern:
Edit: this one cannot be stacked print("Hello")
/*/*/TAG*/
print("Bave")
*//*/TAG*/
//*TAG*//*
print("Cowardly")
//*TAG*/*/
//*TAG*/print("New")
/*/TAG*/print("Old")
print("World") print("Hello")
//*TAG*//*
print("Bave")
//*TAG*/*/
/*/*/TAG*/
print("Cowardly")
*//*/TAG*/
/*/TAG*/print("New")
//*TAG*/print("Old")
print("World") |
Beta Was this translation helpful? Give feedback.
-
Here's a cool one that forms both stackable lines and feature blocks:
print("Hello")
//*TAG0*///*TAG1*/ print("1th")
/*/TAG0*//*/TAG1*/ print("2nd")
//*TAG0*///*TAG1*/ print("3rd")
//*TAG0*//*/TAG1*/ print("4th")
/**//*/TAG0*/
/*/*//*TAG1*/
print("Cruel")
//*TAG1*/*/*/
/**//*/TAG1*/
print("Clown")
/*/TAG1*//**/
/*/TAG0*//**/
/*/*//*TAG0*/
/*/*//*TAG1*/
print("Sweet")
//*TAG1*/*/*/
/**//*/TAG1*/
print("Brave")
/*/TAG1*//**/
//*TAG0*/*/*/
print("World") print("Hello")
//*TAG0*///*TAG1*/ print("1th")
//*TAG0*//*/TAG1*/ print("2nd")
/*/TAG0*///*TAG1*/ print("3rd")
/*/TAG0*//*/TAG1*/ print("4th")
/*/*//*TAG0*/
/*/*//*TAG1*/
print("Cruel")
//*TAG1*/*/*/
/**//*/TAG1*/
print("Clown")
/*/TAG1*//**/
//*TAG0*/*/*/
/**//*/TAG0*/
/*/*//*TAG1*/
print("Sweet")
//*TAG1*/*/*/
/**//*/TAG1*/
print("Brave")
/*/TAG1*//**/
/*/TAG0*//**/
print("World") |
Beta Was this translation helpful? Give feedback.
-
OK, this one's genius: // FIND...: \/([/*!])([/*!])(FLAG)([*!])\/
// REPLACE: \/$2$1$3$4\/
//
// LINE...: //*FLAG*/ or /*/FLAG*/
// HEAD...: //*FLAG!/ or /*/FLAG!/
// TAIL...: //!FLAG*/ or /!/FLAG*/ Edit: I suppose /*/TAG0*///*TAG1*/ print("1th")
/*/TAG0*//*/TAG1*/ print("2nd")
//*TAG0*///*TAG1*/ print("3rd")
//*TAG0*//*/TAG1*/ print("4th")
//*TAG0!/
/*/TAG1!/
print("1st")
/!/TAG1*/
//*TAG1!/
print("2nd")
//!TAG1*/
//!TAG0*/
/*/TAG0!/
/*/TAG1!/
print("3rd")
/!/TAG1*/
//*TAG1!/
print("4th")
//!TAG1*/
/!/TAG0*/ //*TAG0*///*TAG1*/ print("1th")
//*TAG0*//*/TAG1*/ print("2nd")
/*/TAG0*///*TAG1*/ print("3rd")
/*/TAG0*//*/TAG1*/ print("4th")
/*/TAG0!/
/*/TAG1!/
print("1st")
/!/TAG1*/
//*TAG1!/
print("2nd")
//!TAG1*/
/!/TAG0*/
//*TAG0!/
/*/TAG1!/
print("3rd")
/!/TAG1*/
//*TAG1!/
print("4th")
//!TAG1*/
//!TAG0*/ |
Beta Was this translation helpful? Give feedback.
-
Look at me. I am the compiler now 👀 |
Beta Was this translation helpful? Give feedback.
-
you can do it simpler inside the Swift parts. Add compiler definitions to SPM target like In the case of CocoaPods you can even set it up as an environment variable while calling the |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
We had a discussion on Swift forums before about the |
Beta Was this translation helpful? Give feedback.
-
so for the SBI feature in the code, it can be like #if swift(>=5.8) && SBI
extension NBKDoubleWidth {
// StaticBigInt code
}
#else
extension NBKDoubleWidth {
// Int64 code
}
#endif and in // swift-tools-version: 5.7
//=----------------------------------------------------------------------------=
// This source file is part of the Numberick open source project.
//
// Copyright (c) 2023 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=
import PackageDescription
//*============================================================================*
// MARK: * Numberick
//*============================================================================*
// CHANGE THIS VARIABLE BY REGEX TO true
let featureStaticBigInt = false
// it's a var, not a let, because we will modify it
var package = Package(
name: "Numberick",
platforms: [
.iOS(.v14),
.macCatalyst(.v14),
.macOS(.v11),
.tvOS(.v14),
.watchOS(.v7),
],
products: [
//=--------------------------------------=
// NBK
//=--------------------------------------=
.library(
name: "Numberick",
targets: ["Numberick"]),
//=--------------------------------------=
// NBK x Core Kit
//=--------------------------------------=
.library(
name: "NBKCoreKit",
targets: ["NBKCoreKit"]),
//=--------------------------------------=
// NBK x Double Width Kit
//=--------------------------------------=
.library(
name: "NBKDoubleWidthKit",
targets: ["NBKDoubleWidthKit"]),
],
targets: [
//=--------------------------------------=
// NBK
//=--------------------------------------=
.target(
name: "Numberick",
dependencies: ["NBKCoreKit", "NBKDoubleWidthKit"]),
//=--------------------------------------=
// NBK x Core Kit
//=--------------------------------------=
.target(
name: "NBKCoreKit",
dependencies: []),
.testTarget(
name: "NBKCoreKitBenchmarks",
dependencies: ["NBKCoreKit"]),
.testTarget(
name: "NBKCoreKitTests",
dependencies: ["NBKCoreKit"]),
//=--------------------------------------=
// NBK x Double Width Kit
//=--------------------------------------=
.target(
name: "NBKDoubleWidthKit",
dependencies: ["NBKCoreKit"]),
.testTarget(
name: "NBKDoubleWidthKitBenchmarks",
dependencies: ["NBKDoubleWidthKit"]),
.testTarget(
name: "NBKDoubleWidthKitTests",
dependencies: ["NBKDoubleWidthKit"]),
]
)
if featureStaticBigInt {
package.platforms = [.iOS("16.4"), .macCatalyst("16.4"), .macOS("13.3"), .tvOS("16.4"), .watchOS("9.4")]
package.targets.first{ $0.name == "NBKDoubleWidthKit" }!.swiftSettings = [.define("SBI")]
} |
Beta Was this translation helpful? Give feedback.
-
I have one idea. You can try it (I don't have right now access to a machine with swift 5.8) // swift-tools-version: 5.7
//=----------------------------------------------------------------------------=
// This source file is part of the Numberick open source project.
//
// Copyright (c) 2023 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=
import PackageDescription
//*============================================================================*
// MARK: * Numberick
//*============================================================================*
var package = Package(
name: "Numberick",
platforms: [
.iOS(.v14),
.macCatalyst(.v14),
.macOS(.v11),
.tvOS(.v14),
.watchOS(.v7),
],
products: [
//=--------------------------------------=
// NBK
//=--------------------------------------=
.library(
name: "Numberick",
targets: ["Numberick"]),
//=--------------------------------------=
// NBK x Core Kit
//=--------------------------------------=
.library(
name: "NBKCoreKit",
targets: ["NBKCoreKit"]),
//=--------------------------------------=
// NBK x Double Width Kit
//=--------------------------------------=
.library(
name: "NBKDoubleWidthKit",
targets: ["NBKDoubleWidthKit"]),
],
targets: [
//=--------------------------------------=
// NBK
//=--------------------------------------=
.target(
name: "Numberick",
dependencies: ["NBKCoreKit", "NBKDoubleWidthKit"]),
//=--------------------------------------=
// NBK x Core Kit
//=--------------------------------------=
.target(
name: "NBKCoreKit",
dependencies: []),
.testTarget(
name: "NBKCoreKitBenchmarks",
dependencies: ["NBKCoreKit"]),
.testTarget(
name: "NBKCoreKitTests",
dependencies: ["NBKCoreKit"]),
//=--------------------------------------=
// NBK x Double Width Kit
//=--------------------------------------=
.target(
name: "NBKDoubleWidthKit",
dependencies: ["NBKCoreKit"]),
.testTarget(
name: "NBKDoubleWidthKitBenchmarks",
dependencies: ["NBKDoubleWidthKit"]),
.testTarget(
name: "NBKDoubleWidthKitTests",
dependencies: ["NBKDoubleWidthKit"]),
]
)
if #available(iOS 16.4, macCatalyst 16.4, macOS 13.3,
tvOS 16.4, watchOS 9.4, *)
{
package.targets.first{$0.name == "NBKDoubleWidthKit"}!.swiftSettings = [.define("SBI")]
} |
Beta Was this translation helpful? Give feedback.
-
updated CI scripts in the https://github.com/ypopovych/Numberick/tree/pods-swift57 branch. How it works:
|
Beta Was this translation helpful? Give feedback.
-
I don't know where to put this, but I invented a poor man's feature flag while working on (#55):
By replacing the regex
/([*/])([*/])(TAG)\*/
with/$2$1$3*/
you can toggle lines on and off:TL;DR: Here's the best one yet:
Beta Was this translation helpful? Give feedback.
All reactions