You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I write a CLI Tool and use some FilePath structs, but I think the current way to concatenated FilePaths is a way less readable & 'swifty' than it could be.
I remember the C++ 17 Filesystem Path Library which supports a overloading for the '/=' - operator to append a path to another.
The Swift FilePath Struct doesn't support any operator overloadings.
I think this possibility can smooth up the C++ Interoperability and make the syntax more readable.
The Problem in Code
import System
// Defined in another File for project configurationlethomePath:FilePath="/ProjectHome"letuserDirectory:FilePath="SomeUser"// The current ways// solution 1letuserPath1=FilePath("\(homePath)/\(userDirectory)")// solution 2letuserPath2= homePath.appending(userDirectory.components)// solution 3 (if userDirectory is a String)letuserPath3= homePath.appending(userDirectory)
Proposed solution
I wrote an extension to overload the '/' - operator. This also uses the 'appending' Function, but hides the superfluous syntax. So the path appending can be red like a Unix path.
Motivation
I write a CLI Tool and use some FilePath structs, but I think the current way to concatenated FilePaths is a way less readable & 'swifty' than it could be.
I remember the C++ 17 Filesystem Path Library which supports a overloading for the '/=' - operator to append a path to another.
The Swift FilePath Struct doesn't support any operator overloadings.
I think this possibility can smooth up the C++ Interoperability and make the syntax more readable.
The Problem in Code
Proposed solution
I wrote an extension to overload the '/' - operator. This also uses the 'appending' Function, but hides the superfluous syntax. So the path appending can be red like a Unix path.
The Extension
Now I can write the path appending in this way:
Solution
Alternatives considered
An other approach would be the takeover of the C++ like '/='-operator overloading.
This isn't my preferred solution, because I think it isn't easier to read and this approach required a mutable FilePath struct.
Additional information
The URL could maybe also be extended in this way. Other Path-Based structs, classes also, but I don't know if it continues.
The text was updated successfully, but these errors were encountered: