SwiftKotlin icon indicating copy to clipboard operation
SwiftKotlin copied to clipboard

Convert basic operators

Open angelolloqui opened this issue 7 years ago • 4 comments

Swift allows for custom operators. In Kotlin, operators are restricted to: https://kotlinlang.org/docs/reference/operator-overloading.html

Transform the operators that have a Kotlin counterpart. Example:

public func + (left: Price, right: Price) -> Price {
    return Price(amount: left.amount + right.amount, currency: left.currency)
}
public func - (left: Price, right: Price) -> Price {
    return Price(amount: left.amount - right.amount, currency: left.currency)
}

Translates to:

operator fun Price.plus(price: Price) =
        Price(amount = this.amount + price.amount, currency = this.currency)

operator fun Price.minus(price: Price) =
        Price(amount = this.amount - price.amount, currency = this.currency)

angelolloqui avatar Nov 16 '17 09:11 angelolloqui

Continuing conversation from #89, I believe this conversion can be added by:

  1. Changing the swift func into a method extension on the first argument type
  2. Remove the first argument and replace tokens with its name by copies with this
  3. Add the operator keyword
  4. Map basic operators to their names (+ -> plus, - -> minus,... https://kotlinlang.org/docs/reference/operator-overloading.html)

angelolloqui avatar Feb 08 '19 11:02 angelolloqui

This would be great. Since they were outside of class/struct in swift I just went for a simple hack, but this would be clean and perfect. Maybe you do it?

torlangballe avatar Feb 08 '19 11:02 torlangballe

Yeah, it is in the scope of changes I want to make, but to be honest, it has been open for more than 1 year already. I am not finding all the time I would like to implement all the issues (and the list is long). So yes, I hope I will eventually implement it myself but I can not promise when.

If this is important for you, please let me know so I will try to prioritize over others

angelolloqui avatar Feb 08 '19 11:02 angelolloqui

I can work my way through all other changes first. Can always do a pull request and see if I can do it how you want too if it gets to that, but no hurry yet.

tor

On 8 Feb 2019, at 12:52, Angel G. Olloqui [email protected] wrote:

Yeah, it is in the scope of changes I want to make, but to be honest, it has been open for more than 1 year already. I am not finding all the time I would like to implement all the issues (and the list is long). So yes, I hope I will eventually implement it myself but I can not promise when.

If this is important for you, please let me know so I will try to prioritize over others

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/angelolloqui/SwiftKotlin/issues/43#issuecomment-461779406, or mute the thread https://github.com/notifications/unsubscribe-auth/AB-irn0eSYgbstGC7uq9SlXP81naynb6ks5vLWT1gaJpZM4QgO2d.

torlangballe avatar Feb 08 '19 11:02 torlangballe