4 ms·
I'm new to Kotlin. Can someone explain how this function creates a Money object incorporating the given count of them? This looks like it's ignoring the Number
by zellyn 2y ago
I'm new to Kotlin. Can someone explain how this function creates a Money object incorporating the given count of them?
This looks like it's ignoring the Number and creating a new Money object (of denomination 1?)
> public infix fun Number.money(currency: String): Money = money(currency, defaultRoundingMode, null)
- lolinder 2y agoIt'd be easier to follow if you could provide a link to the GitHub line you're looking at, but most likely the `money` function being called is a second extension method of the Number class, which means that it implicitly gets the Number object as `this` and presumably uses it to build the Money.
- zellyn 2y agohttps://github.com/eriksencosta/money/blob/4bef95a1d2158e3088d832217d46407bad87c7ba/money/src/main/kotlin/com/eriksencosta/money/Number.kt#L37 https://github.com/eriksencosta/money/blob/4bef95a1d2158e308...
- lolinder 2y agoYeah, that's just an infix function that provides some default parameters to this extension method: https://github.com/eriksencosta/money/blob/4bef95a1d2158e3088d832217d46407bad87c7ba/money/src/main/kotlin/com/eriksencosta/money/Number.kt#L79 https://github.com/eriksencosta/money/blob/4bef95a1d2158e308...
- thomascgalvin 2y agoIn Kotlin, an infix method can be called without the `.` operator, and without the parens surrounding arguments. 100 money "USD" is the same as 100.money("USD") `Number.money(currency: String): Money` is an extension method; Kotlin provides syntactic sugar which allows you to "add" methods to existing classes. The above is basically equivalent to: fun money(count: Number, currency: String): Money
- zellyn 2y agoThanks! I allowed myself to be distracted by all the weird infix stuff. I was thinking "but it's not passing itself into the money constructor" but it's actually just delegating to a different-signature version of `money` _on itself_ that will use its value then.
- adamgordonbell 2y agoGreat see that kotlin didn't avoid all Scala's bad ideas.