After 10 years of Java, I finally found something that could be my daily driver

I’m really enjoying my Kotlin experience so far. Already moved a couple of big projects to Kotlin as a learning exercise.

Over the years I also did some work with C# and JS (node – I’m backend only, UI makes me nauseous), but it never felt right.

Maybe Kotlin is my future, who knows..

submitted by /u/RevertCommit
[link] [comments]

Continue Reading After 10 years of Java, I finally found something that could be my daily driver

Potentially terrible idea to use extension functions inside a class as “pseudo-private”.

Sometimes I like to just fiddle around and think about what I can do, rather than what I should do. 😉

Here’s the motivating example. Let’s say I’m implementing some class. Inside that class I need to perform some operations on

String

s. These are pretty specialized operations that are really only relevant to this class’s functionality. Here’s a skeleton of an example of what I would sometimes do:


class Foo() { fun doSomething() { // some logic val x = parseBookFile(); if (x.countPages() > 1000) { throw Exception("tl;dr") } // more logic } } private fun String.countPages() = TODO() // domain specific term doesn't belong public

But, now you can’t test that

countPages

function by itself. You just have to test

Foo.doSomething

more thoroughly. If you do this too much you get combinatorial explosion in your test cases.

What if we did this instead?


class Foo() { fun doSomething() { // some logic val x = parseBookFile(); if (x.countPages() > 1000) { throw Exception("tl;dr") } // more logic } fun String.countPages() = TODO() // domain specific term doesn't belong public }

Now the function

String.countPages

isn’t technically private, but outsiders can’t really use it unless they work kind of hard to get at it. The only way to access

String.countPages

is while inside class

Foo

‘s context. In other words, you’re either editing

Foo

‘s definition directly or you’re writing an extension function for

Foo

:


fun doAThing() { "abc".countPages() // compile error } fun Foo.doSomethingElse() { "abc".countPages() // here you can access it! }

I haven’t actually used this anywhere yet, but I feel like it’s a nice middle ground between other options. Those other options being:

  • Keep the function private. Now you can’t test it.
  • Make the function public/internal. Now it “pollutes” the namespace when I’m almost positive that others should not use this function.
  • Make the function internal but make this whole class/functionality into a module, so you can test it, but still keep it private from users of your new “library”. This is clearly overkill for many cases.
  • Kotlin implements package-private. :p

The functionality itself doesn’t have to be an extension function, either, necessarily:


class Foo() { fun doSomething() { [snip[ } fun Foo.add(i: Int, j: Int): Int = TODO() }

This will have the same effect of only allowing

add

to be accessed from an extension function on

Foo

. This form does give a compiler warning that the receiver isn’t used, though.

So, I don’t know. I just thought it was pretty cute and that I might share it. 😀

EDIT: Taken to its logical conclusion, I guess we can end up with something like:


class Foo() { fun doSomething() = with(FooContext) { // some logic val x = parseBookFile(); if (x.countPages() > 1000) { throw Exception("tl;dr") } // more logic } } object FooContext { fun String.countPages() = TODO() // domain specific term doesn't belong public }

submitted by /u/ragnese
[link] [comments]

Continue Reading Potentially terrible idea to use extension functions inside a class as “pseudo-private”.

I just discovered this is valid Kotlin

And Java too!


val x = 1 - - - - - - - - - - - 2 val y = 3 + + + + + + + + + + + 4 val z = 5 + - + - + - + - + - + 6

I actually figured this out while working on my own lexer – it’s basically so that numeric literals with a leading + or – can be disambiguated from arithmetic operations, which requires information more easily acquired later during parsing. Otherwise, this would be difficult to lex correctly:


val w = 3+4

Also fun to note:


val x = 1-----------2 // Lexer detects decrement ops, fails val y = 3+++++++++++4 // Lexer detects increment ops, fails val z = 5+-+-+-+-+-+6 // This works!

So now you can confuse coworkers and increase job security with more obscure and difficult parses. Enjoy!

EDIT: I just discovered this as well:


val x = 3 - +(2 + 4) // Totally valid

Kotlin I believe makes ‘unaryPlus’ an explicit operation, but Java was a surprise to me. Even putting non-constant expressions in the above will work just fine.

submitted by /u/frizzil
[link] [comments]

Continue Reading I just discovered this is valid Kotlin

Arkanoid

Well I have to do a arkanoid game, and I dont know how to do the bricks list… Bricks that a ball that is moving needs to destroy. I need to create a list of bricks so when the ball collides with bricks the bricks are “destroyed” Thanks for the help

submitted by /u/gddo2020
[link] [comments]

Continue Reading Arkanoid

Should I use type inference?

Very simple question here.

Should I do this?


val re1 = txtRe1.text.toString().toIntOrNull() ?: 0

Or this?


val re1: Int = txtRe1.text.toString().toIntOrNull() ?: 0

Or for example:


val spinner: Spinner = findViewById(R.id.spinner)

or


val spinner = findViewById(R.id.spinner)

Thanks.

submitted by /u/HelloWorldzik
[link] [comments]

Continue Reading Should I use type inference?

A little help abut Desktop apps?

Hey guys,

so I’m pretty new to Kotlin, though I know the basics and fundamentals pretty well, and I was interested in building Desktop applications. Currently I’m building my project with TornadoFx, but I have no actual clue about how to compile it to a .exe or .dmg, for example. I was looking around the web and I found all these articles about Kotlin/Native, Compose and KMM, and now I’m a bit confused over all of them.

So can someone explain to me a bit about what are those environments? Can I actually build something compatible with Windows or Mac with Kotlin? Can I stick with TornadoFx and Kotlin (both of which I’m finding myself really comfortable with)? I’d like to have just to have a bit of guidelines about what I should look to learn and to invest some time on.

Thank you so much in advance to everyone!

submitted by /u/NicolaM1994
[link] [comments]

Continue Reading A little help abut Desktop apps?

End of content

No more pages to load