Defining tuple methods -


This is a swap function for two-element tuple:

  FN Swap & lt; A, B & gt; (OBJ: (A, B)) - & gt; (B, A) {Let (A, B) = Ob; (B, a)}   

example usage:

  main () {obj = (10i, 20i); Println! ("{}", Swap (obj)); }   

Is there a way to define swap as a method on two-element toplevel? To wit. So that it can be called in such a way:

  (10i, 20i) .swap ()    

Yes, there is simply defining a new property and applying it immediately, something like this:

  attribute swap & lt; U & gt; {FN Swap (Self) - & gt; U; } Impl & lt; A, B & gt; Swap & lt; (B, A)> For (A, B) {# [inline] FN swap (self) - & gt; (B, A) {Give (A, B) = Self; (B, A)}} FN main () {T = (1 U, 2 U); Println! ("{}", T.swap ()); }   

Note that in order to use this method, you need to import the swap property in each module where you want to call the method.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -