The Xojo ASSIGNS keyword and Swift

In Xojo you can create a method like this

Function myMethod(foo as Integer, assigns bar as String) {
dosomething(foo,bar)
End Function

myMethod(42)="Dave"

Does anyone know of a way to replicate that in Swift?
So far I can’t find anything even close

Best so far is

Function myMethod(foo as Integer, bar as String) {
dosomething(foo,bar)
End Function

myMethod(42,"Dave")

But that precludes me creating overloads as now the signatures can’t be different

I assume this is not what you’re looking for?
https://docs.swift.org/swift-book/LanguageGuide/Extensions.html

No, I need to be able to replicate the exact same functionalty that Xojo provides
the problem is I can’t find anything that allows me to put a value to a “function” using the “=” sign. The closest is a computed variable, but you cannot pass other parameters

wouldnt you override the = operator ?
https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html

I can’t envision a methodolgoy that would allow that.
Xojo seems to allow two methods for passing values to a function, parameter and/or assignment, I’m trying to figure a way to “fake” the assignment method, and still allow overloads to be viable.

Current train of thought is something along this line (not that I really like this idea)

BASIC

FUNCTION foo(bar as Integer, assigns S as string) 
stuff....
END FUNCTION
FUNCTION foo(bar as Integer) AS String // overload
return "xyz"
END FUNCTION

SUB TEST() 
   foo(32)="abc"
END SUB

SWIFT

func foo(bar : Int) {
    var s : String
   var assign : String { set ( s=NewValue} } get { return "" } }
}
func foo(bar: int) -> String {
  return "xyz"
}

func test() {
   foo(32).assign="xyz"
}

which actually doesn’t work :frowning:

Overriding the = operator for the type would seemingly let you do this
I dont know the specifics as I’m not nearly as Swift literate as you BUT in C++ you can do this as well and make several = functions
The pages I linked to have examples of some (written as extensions)

extension Vector2D {
    static func += (left: inout Vector2D, right: Vector2D) {
        left = left + right
    }
}

and you’d use this just like you would any += operator in Swift

= can also be overridden like this (as can most operators)

Norm… I am not sure how overriding the “=” operator, gives a FUNCTION the ability to accept an assignment value?

Show me a C++ example that replicates my above post if you can… please?

I’m not sure there is any language that permits something that is syntactically equivalent

Most of the time when you see the = operator overloaded its basically performs what “operator_convert” would do when you assign from one type to another

operator = is about as close as I could think of

Problem is a “function” isn’t a datatype, so operators don’t apply… Not sure how Xojo defines it, but seems it is unique.

My problem, I used it alot, and it allowed overloads

sub myFunction(foo as string, bar as string, assigns xyz as integer)
function myFunction(foo as string,bar as string) as integer

yeah it does seem to be quite unique

In C++ you make a function, which returns an object, which then has an assign operator to do take the value.

Thanks… could you throw together a simple example?

in C++ I got this working example:

#include
#include

typedef void (^IntPropertySetter)(int value);
typedef int (^IntPropertyGetter)();

class IntPropertyAssign
{
IntPropertySetter setter;
IntPropertyGetter getter;
public:

IntPropertyAssign(IntPropertyGetter g, IntPropertySetter s) { setter = s; getter = g; }
IntPropertyAssign(const IntPropertyAssign &other) : setter(other.setter), getter(other.getter) { }

IntPropertyAssign& operator= (int v) { setter(v); return *this; }
operator int () { return getter(); };
};

class MyTest{

int values[10];

public:

MyTest()
{
memset(&values, 0, sizeof(values));
}

IntPropertyAssign value(int index)
{
IntPropertyGetter g = ^{ return values[index]; };
IntPropertySetter s = ^(int v){ values[index] = v; };
IntPropertyAssign v(g,s);
return v;
}

};

int main(int argc, const char * argv) {

MyTest t;

t.value(2) = 3;

int v = t.value(2);

std::cout << "value: " << v;

return 0;
}

maybe it helps you.

And I made a blog entry:

Xojo like properties with parameters in C++

Thanks… I will see if I can make heads or tails of it