angelolloqui / SwiftKotlin

A tool to convert Swift code to Kotlin.
MIT License
2.93k stars 199 forks source link

Convert basic operators #43

Open angelolloqui opened 6 years ago

angelolloqui commented 6 years ago

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 commented 5 years ago

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)
torlangballe commented 5 years ago

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?

angelolloqui commented 5 years ago

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

torlangballe commented 5 years ago

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 notifications@github.com 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.