clutcher / bh

Issue tracker for Better Highlights Intellij IDEA plugin
6 stars 0 forks source link

Kotlin Typealiases #101

Closed mgroth0 closed 3 months ago

mgroth0 commented 3 months ago

I am not sure if links to kotlin typealiases are officially supported, but for me it seems they do not work.

clutcher commented 3 months ago

@mgroth0 I'm not familiar with Kotlin and what are typealiases. Can you give me an example of kotlin file and how you are referencing it with wikilink?

mgroth0 commented 3 months ago

Sure.

To summarize, a typealias is basically what it sounds like. It is a definition that allows you to alias another type.

Say I define Color

package my.example

class Color(val red: Float, val green: Float, val blue: Float)

Now say I want I have another class with the same simple name on my classpath. For example, java.awt.Color. This creates some naming conflict issues.

Kotlin introduced typealiases which offers a way to avoid some of these naming conflict issues:

package my.example

typealias AwtColor = java.awt.Color

In almost all scenarios, a typealias can be used exactly as a regular class name.

package my.other.example

import my.example.Color
import my.example.AwtColor

fun assertSame(myColor: Color, awtColor: AwtColor): Boolean {
    // implementation not included
}

And so I might reference it in a wikilink as [[my.example.AwtColor]]

You might ask why I can't just link the original class like [[java.awt.Color]]. This is because I sometimes use a typealias to convey meaning, sort of like an annotation.

In kotlin, void is Unit and Unit is a regular object.

So I have made some typealiases for Unit to convey special meanings. For example:


typealias DoNothing = Unit
typealias WIP = Unit

They get compiled to the same byte code, but serve as a reminder for myself about my intentions.

But the most important thing is that a kotlin typealias is treated as a normal type for almost all purposes, especially for type resolution by the compiler. Given this, a kotlin developer would expect that if wikilinks could point to types they would also support pointing to type aliases.

clutcher commented 3 months ago

@mgroth0 Thanks for extensive explanation. Will take a look on it.

clutcher commented 3 months ago

Released in 2024.1.13.

mgroth0 commented 3 months ago

It's working great. Thanks!