Hello, this is my first attempt at sharing an idea about programming syntax.
I've noticed that quite often we initialized a class or a struct with a single parameter and sometime this can lead to
URLRequest(url: URL(string: string)!)
== STATE OF THE IDEA ==
the idea evolved to something more simple thanks to suyashsrijan
struct Something {
var string: String
reversible(asSomething) init(string: String) {
self.string = string
}
}
and then
let something: Something = myString.asSomething
we could even say that if it default to "asClassname"
reversible init(string: String)
is shorthand for
reversible(asSomething) init(string: String)
and this would basically simply implement:
extension String {
var asSomething: Something {
return Something(string: self)
}
}
Or as an optional if needed
== Original Post ==
What if we could declare an initializer as reversible such as:
struct Something {
var string: String
reversible init(string: String) {
self.string = string
}
}
So that in the end we could create an instance of Something directly from the string object using for example .. between variable.
exemple :
let myString ="this is a string"
let mySomething: Something = myString..something
print(mySomething.string) // "This is a string"
so for example
let request = URLRequest(url: URL(string: String(substring))!)
would become if all had reversible initializers
let request = substring..string..url..request
I have no idea if this is actually doable or really useful but what do you think of it?We could call it the real Yoda syntax