One of the datatypes the is used in Xojo is the “PAIR”
the common way to initiate a pair is
var p as pair = "A" : "B"
using the “:”
Well the pair datatype does not exist in Swift (typealias is close, but no cigar). so here is a bit of code the replicates this as close as possible. Unfortunatly the use of “:” is claimed as a “key” word in Swift so I had to come up with something else
infix operator ><
func ><(lhs:Any,rhs:Any) -> pair { return pair(lhs,rhs) }
class pair {
var left : Any? = nil
var right : Any? = nil
init(_ _left:Any?,_ _right:Any?) {
left = _left
right = _right
}
}
here we use “><” in Swift in place of the “:” used by Xojo, and because Swift handles “any” a bit different than the Xojo “variant” we need to unwrap our results
here is a small test function that shows how it might work
func test() {
let xyz = "A" >< "B"
// could also be declared as 'let xyz=pair("A","B")
print(xyz.left!)
print(xyz.right!)
xyz.right="C"
print(xyz.right!)
}
I thought Swift was meant to be easier to read and write… I mean I get what’s going on, but wow, lots of magic incantations happening here. I guess nearly all programming languages have certain complexities, and seem like a dogs dinner until you understand 'em.
I assume “Any” can mean any data type or object type?
But both the macro and function take strings? I assume these are more like NSString instead of char?
How do specify memory management? Are the objects counted in, copied in or just not counted at all?
Very easy to read and write… What magic? Its a simple class, almost idendical to a Xojo class
in Xojo this would be something like
class pair
var left : variant
var right : variant
constructor(_left : String,_ right:Variant)
left = _left
right = _right
end contstructor
end class
Macro? no, it INFIX defines a custom operator (since “:” is reserved in Swift)… something Xojo cannot do at all… Underlying NSString? yes, but Swift has an actual String datatype.
Memory Managment? Object Counting? Not the concern of the developer anymore. Not that those functons are even warranted in this simple class.
and I did make a typo… Anywhere you see STRING , should be Any