News for hackers. Stuff that matters.
Re:Kotlin Money by occz on Tuesday October 08, 2024 @10:07AM (#41777532)
Cool stuff!

The use of infix functions reads a bit weird to me.

If I were to design an API like this in Kotlin, I think I would have gone for regular extensions for many cases and perhaps extension properties, think as such:

    val fiveBucks = 5.usd
    val fiveBucks = 5.money("USD")
    val tenPercent = 10.percent
How come you went for "increaseBy" and "decreaseBy" instead of overloading `plus` and `minus`? Just curious, preference is a valid answer.
Re:Kotlin Money by sigh_again on Tuesday October 08, 2024 @11:38AM (#41778555)
Nothing is preventing you from using it this way ? Infix functions are just syntactic sugar, some prefer it, some don't, but there's zero downsides to it (aside from your coworkers abusing it.) 5 money "USD" is literally the exact same thing as 5.money("USD"), and Int.usd = this.money("USD")

+ and - are already overloaded (see val subtotal = price + shipping), increase/decreaseBy are for operating over percentages (and could be written as subtotal * (1 - discount), which is much less clear). As the other comment say, it has an actual, real life meaning that people understand clearly. Your price increased by 10 percent. the By convention is also already present in the Kotlin stdlib, although it's more for grouping operations, numeric operations are taking the Of suffix now (sumBy has been deprecated in favor of sumOf, increaseBy could become increaseOf without any loss of clarity)

Re:Kotlin Money by nwatson on Tuesday October 08, 2024 @10:54AM (#41778015)
`decreaseBy` is a multiplication and subtraction combined, map naturally to commerce domain, and is more complex than plain addition / subtraction.
Re:Kotlin Money by refulgentis on Tuesday October 08, 2024 @11:56AM (#41778780)
This does a better job of showing an uneasy feeling I have about Kotlin than anything I could say.

- The infix is weird and footgun-y.

- Extension methods on int/double serving as constructors smells funny.

- Using infix operators as constructors but not using infix operators for addition/subtraction smells funny.

In general, at least in a corporate environment switching off Java for Android, I found Kotlin a distracting step sideways.

Code reviews tended to involve a lot of bikeshedding over how to make it Kotlin-y, and there's a sort of "why not?" approach to language features that creates much room for the bikeshedding.

It left me feeling like we were unconciously choosing to have the same arguments C++ programmers had in 1990, all over again. Except it was even more destructive, because those arguments were centered, and conflated with "proper" coding in the fancy new language.

I'm not against new and shiny: I was the first to use Kotlin in the org., and I dove right into Swift. There's something alarming with this transition.

I'm heartened by starting to see some debate in Android dev communities about whether Kotlin/Compose were a bridge to nowhere that shouldn't have been a focus for years.

Re:Kotlin Money by t-writescode on Tuesday October 08, 2024 @12:56PM (#41779371)
I have used Kotlin professionally for a couple-few years; and, in my experience, infix operations are a choice and you don't have to use them. - in fact, I don't think any bit of code in anything I've used uses infix operations, this is my first time seeing them in Kotlin in the wild.

For example, I haven't used them even once.

That said, I, personally, have done almost no Android programming in Kotlin, so perhaps I'm missing the main environment where such things are done? For me, Kotlin has mostly been an aggressive amount of syntactic sugar to make Java a nice experience?

And most of the DSL-style code that I write uses function pointers as the last parameter, so I can do doTheThing(parameters) { anonymous function } and that's about it?

I'm sorry that the organization you work with has aggressive bikeshedding. Perhaps this is something your organization, in general, could have a conversation about?

Re:Kotlin Money by refulgentis on Tuesday October 08, 2024 @02:03PM (#41780051)
I agree, you nailed it. Operators are something that feels like using the new shiny but are repeating well-trodden mistakes that are well-understand in languages ranging in age from C++ to Swift.
Re:Kotlin Money by t-writescode on Tuesday October 08, 2024 @07:28PM (#41782883)
I was referring to in-fix, like can be found in Kotlin and Ruby, not necessarily operator overloads. I've found pretty usable operator overloading, in appropriate contexts, like adding two lists meaning append.
Re:Kotlin Money by mrcrumb1 on Tuesday October 08, 2024 @01:14PM (#41779524)
You can bikeshed in any language. Kotlin introduced a lot of optional syntactic sugar so that Java devs could choose their level of comfort with the language features. I don't really see this as a problem unless your devs are prone to this sort of bikeshedding (see: your use of "smells funny"). I've used kotlin for Android since right before it was officially supported and there has been almost no downside other than the occasional hiccup with Java/Kotlin nullability interop
Re:Kotlin Money by refulgentis on Tuesday October 08, 2024 @01:32PM (#41779728)
Hear hear, agree whole heartedly.

Minor nit: bike shedding refers to, in the original, arguing about the color of the paint on the shed.

It follows that not all discussion is bike shedding.

In this instance, the references to C++ allude to a common best practice for programmers of avoiding custom operators, which goes far beyond an aesthetic, i.e. style, i.e. color of paint on the bike shed difference. There are engineering consequences. This also applies across languages, I'm familiar with it only from trodding the same path you are, through Swift.

Bike shedding bike shedding is indeed possible, so I won't suggest an umabiguous definition. :)

The bike shedding reference in OP is to the different flavors, but equivalent, syntactic sugar that you mention. This uses the new shiny. But this creates toxic baggage, because among other things, because to a naive implementer, there is no solid engineering reason to e.g. avoid custom operators, it's just a scarred C++ graybeard enforcing their opinion :)

Re:Kotlin Money by wiseowise on Wednesday October 09, 2024 @06:08AM (#41786168)
No, not in every language like in Kotlin.

I’ve never seen this amount of bikeshedding like in Kotlin. It’s as if whole identity of Kotlin developer is based on “do it differently from Java” which is super ironic for people who use JVM language.

Re:Kotlin Money by beeboobaa3 on Wednesday October 09, 2024 @07:59AM (#41786916)
> so that Java devs could choose their level of comfort with the language features

and thus complicating teamwork because there is no "right" way

kotlin is great for the lone cowboy. not so much for teams.

Re:Kotlin Money by eriksencosta on Friday October 11, 2024 @02:05AM (#41806659)
In my previous job, we had a mid-sized team of ~34 software engineers and Kotlin worked like a charm. We set common standards and practices early on and it paid off. The lone wolves will exist regardless of the programming language. I've seen them in different flavors: Ruby, Elixir, PHP, JS, Python, Perl, Java, Kotlin, and etecetera throughout my career. It's a matter of team dynamics.

Anyway, I'm not a Kotlin die-hard but I found it quite fun to code in the language. IMHO, it has a gentle learning curve and the community has plenty of great libraries (e.g., Ktor, Koin).

Nevertheless, I think I leaned too much on using the syntactic sugar of the language when writing the docs and the introductory article. But by no means users are bound to this way of coding.

Re:Kotlin Money by zoogeny on Tuesday October 08, 2024 @01:26PM (#41779654)
I hear what you are saying but there is the other side. Perhaps the opinion/feeling you are having is related to getting older.

We've learned a lot about what works and what doesn't in programming languages. Goto considered harmful and all that kind of stuff. If you want to get a Lisp/Haskell fanatic really going point out how so many of the features in those languages have finally made their way into mainstream languages (lambdas, etc.).

What we don't often consider are all of the language features that didn't make it.

This process didn't stop sometime in the past. It is happening right now. That feeling of unease may not be an indication of the quality of the features you are considering. It may largely be uncertainty about what features will or will not stand the test of time.

Perhaps as we get older, we want languages that have all of the good stuff we've learned from the past and none of the experimental stuff that we aren't too sure about yet. That might be because we are starting to notice that we won't have enough time left in our remaining days to sort all the new toys into the good/bad bin.

Re:Kotlin Money by refulgentis on Tuesday October 08, 2024 @01:45PM (#41779894)
Stirring call to action for creativity, but it is unclear how a comment that boils down to "new things might be good" applies in this context.

as OP refers to, custom operators are considered harmful in languages ranging from C++ to Swift. It's a great contemporary example of goto.

Re:Kotlin Money by zoogeny on Tuesday October 08, 2024 @02:44PM (#41780438)
I suppose the more poetic: "don't speak too soon, for the wheel's still in spin" is another way to state it. It isn't "new things might be good" it is more a reminder that your self-described feeling of unease may say more about you than it does about the language feature under question.

I've seen some horrors in my time due to custom operators. I've also suffered through some horrors when they are missing and would clearly be appropriate. They seem appropriate for linear algebra libraries for example. Not everyone agrees, obviously.

As an aside, I have been watching a lot of YouTube videos of people programming C. I notice that goto shows up very frequently, especially in modern C. Usually it is a label at the end of a function to manage resource cleanup before returning, almost like a manual `defer`. Sometimes it is to break out of nested loops. I mean, I've always taken Dijkstra's point to be that long jumping out of a procedural boundary was the real problem, not the keyword "goto". And built-in language features that obviate the need to use goto even for the reasonable use cases are my preference.

I feel the same about custom infix operators. I too share an unease about them. But I have to admit they feel right sometimes. I admit I don't have enough information on their use in this kind of case to know if it is one of the good ones or one of the bad ones.

Re:Kotlin Money by Clamchop on Tuesday October 08, 2024 @03:32PM (#41780936)
> The infix is weird and footgun-y.

If it's a foot gun, then I'm not seeing how the gun is loaded?

> Extension methods on int/double serving as constructors smells funny.

It doesn't feel meaningfully different from the static factory pattern in traditional Java, where 10.percent() would be something like MoneyUtils.createPercent() or Percent.create() with several overloads. Under the hood, that is essentially what is happening. Only downside I can see is that it muddies what truly belongs to the class, but that's true of any extension function and Kotlin is intended to be used with IDEA's introspection anyway.

The Kotlin standard library has lots of extension functions that construct new objects.

It also reminds me of Ruby, which perhaps you're also not a fan of and that's OK.

Re:Kotlin Money by ulrikrasmussen on Tuesday October 08, 2024 @11:27PM (#41784229)
I think that's more an effect of the language being new to you, and the company coding culture not having developed around it yet, than a problem with the language. Kotlin has warts, sure, but we have been using it for seven years now, and while we did have some bikeshedding in the beginning, it is virtually absent from our code reviews now. The language didn't change (well, it got even more features), but we did.
Re:Kotlin Money by gus_massa on Tuesday October 08, 2024 @05:56PM (#41782177)
> overloading `plus` and `minus`

Perhaps I'm misunderstanding your idea, but what about

  val total = price + 10.percent * 2
If the price is 10, then the total is 12 or 22?
Re:Kotlin Money by callmeal on Tuesday October 08, 2024 @06:21PM (#41782391)
>val total = price + 10.percent * 2

If the price is 10, I would expect total to be 10,2.

(price + 10.percent) * 2 would be 22.

I don't see any way to get 12.

Re:Kotlin Money by ternaryoperator on Tuesday October 08, 2024 @06:41PM (#41782525)
price + (10.percent * 2) looks like 12: If 10.percent = 1, 1*2 = 2. 10 + 2 = 12.

Under the usual rules of operator precedence would be the expected answer (multiplication having higher precedence than addition).

Re:Kotlin Money by bloated5048 on Tuesday October 08, 2024 @11:16PM (#41784169)
How does computer know it's 10% of 10? Unless specified, 10% is 0.1 Or am I an idiot and missing something here
Re:Kotlin Money by gus_massa on Wednesday October 09, 2024 @07:29AM (#41786684)
If you oveload "+" to mean whatever-you-want, it's possible. Using bad python [1], it's something like:

  def sum(x,y):
    if is_number(x) and is_number(y):
      return x + y
    elif is_number(x) and is_percent(y):
      return x * (1 + y.value/100)
    else:
      raise fatal_error
I don't claim it's a good idea, but it's possible if the language allows operator overload.

[1] Sorry, my main current language is Racket, but I thought that python-like is easier to read for most people, in spite people that likes python can find like 10 error in 7 LOC.

Re:Kotlin Money by kelnos on Tuesday October 08, 2024 @09:43PM (#41783652)
How do you get 10.2? I can only think of two (reasonable) ways to think about this:

    (price + 10.percent) * 2 = (10 + 1) * 2 = 11 * 2 = 22
and

    price + (10.percent * 2) = 10 + 20.percent = 10 + 2 = 12
Personally I would assume the latter, given standard operator precedence, where multiplication precedes addition.
Re:Kotlin Money by gus_massa on Wednesday October 09, 2024 @07:39AM (#41786763)
To get the 10.2 you should use the standard tricks to use % in math. For example in a two equations system [1].

  x + y = 100
  90% x + 80% y = 84
you translate it to

  x + y = 100
  90/100 x + 80/100 y = 84
and then you use the Gauss method (or whatever) to solve it.

So in a math class, when you see 20% you translate it to .2, and 10+20% means 10.2. But in a calculator and in the OP project the % button is magic and and 10+20% means 12.

[1] You have to buy two item that in total cost $100, but today there is a 10% discount in one and a 20% discount in the other so you pay only $84. ¿How much is each item?

Re:Kotlin Money by calfuris on Wednesday October 09, 2024 @04:19PM (#41792185)
Percentages are incommensurable with most things, including other percentages in the general case. They are percentages _of something_ and in general the only way you can do addition, subtraction, or comparison is to identify the referent and multiply it out first (the special case is when the other value involved is another percentage with the same referent). So in math class, when you see 20%, you translate it to ".2 _times something_", which is not a value that can be added to 10. You have to figure out what to multiply it by first. In the case of 10+20%, it would be reasonable to assume 20% of 10, which is how you get 12. It would also be reasonable to ask "20% of what?" 10.2 is 10 + 20% of 1, which requires an explanation of how that 1 got involved.
Re:Kotlin Money by occz on Wednesday October 09, 2024 @03:43AM (#41785469)
I suppose that depends on how you implement `times` for the `Percent`-class. One way would be to say that 10% * 2 = 20%, making this expression evaluate to 12, in accordance with Kotlin operator precedence [1].

It's possible to avoid making this decision altogether by not implementing `times` for `Percent`, making the user have to be more explicit about what they actually want to happen - for example, requiring them to add parenthesis to arrive at 22:

    val total = (price + 10.percent) * 2
Or to arrive at 11:

    val total = price + (10 * 2).percent
[1] https://kotlinlang.org/docs/reference/grammar.html#expressio...