A Unique Identifier in Kotlin

In my typescript class, I have a member that accepts any as the name:

interface ControlTagType {
    type?: String | null;

    [name: string]: any
}

class ControlTag {
    tagSource: String | null = null;

    tag: ControlTagType | null = null;
}

export { ControlTag };

With this setup, I can use the class in vuejs like this:

controlTag.push({ tagSource: 'USER', tag: { type: 'X', TAG_1: 'TAG_X' } });
controlTag.push({ tagSource: 'AUTO', tag: { type: 'Y', TAG_2: 'TAG_Y' } });

This solution worked for another problem I had.

However, I'm struggling to achieve the same generic member functionality in kotlin:

I have an enum class that I utilize during serialization:

  @JsonValue
  fun value(): X {
    return X( this.name : this.name)
  }

like so:

data class X(val [name: string]: any)

I can do it this way, but it's complex and needs to be repeated for each class:

 @JsonValue
  fun value(): Any? {
    when (this.name) {
      XX_XX.name -> {
        return object {
          var XX_XX: String = name
        }
      }
      YY_YY.name -> {
        return object {
          var YY_YY: String = name
        }
      }
      ZZ_ZZ.name -> {
        return object {
          var ZZ_ZZ: String = name
        }
      }
      else -> return null
    }
  }

Furthermore, in this implementation, the parameter name is converted to lowercase:

{"type":"XX","xx_XX":"XX_XX"}

Answer №1

Index types do not exist in Kotlin.

However, you can utilize various implementations based on interfaces instead of using val[name: string]: any

interface ControlTagType {
  val type: String?
}

data class ControlTag(
    val tagSource: String? = null,
    val tag: ControlTagType? = null
)

data class FirstControlTagType(
    override val type: String? = null,
    val firstOne: String? = null
) : ControlTagType

data class SecondControlTagType(
    override val type: String? = null,
    val secondOne: String? = null
) : ControlTagType

Kotlin is designed to support multiple languages and constructions like val[name: string]: any, making it more dynamic compared to languages such as Java.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method to run a MongoDB query that is stored as a string in a Java program

I'm fairly new to working with the MongoDB Java driver and I have a question regarding executing queries stored as strings. Is this the recommended method, or are there better alternatives? I came across the following code snippet in a stackoverflow ...

Tips for connecting data to an HTML page with Angular 2

My code is throwing an error message while I'm debugging: Error: Uncaught (in promise): TypeError: Unable to get property 'standard' of undefined or null reference Here is the relevant part of my code: student.component.ts: this._studentSe ...

The getStaticProps() function in NextJS has not been invoked

Currently, I am working on a basic website and my goal is to retrieve data from an API and showcase it on my component. However, I've encountered an issue where the getStaticProps() method doesn't seem to be triggering. Below is the code snippet ...

The Angular template loads and renders even before the dynamic data is fetched

I'm encountering a frustrating issue where the page loads before the data is retrieved. When I log the names in $(document).ready(), everything appears correct without any errors in the console. However, the displayed html remains empty and only shows ...

`Dealing with Java Servlet Exception in Kendo UI Environment`

I am facing an issue with displaying date in my Kendo UI grid. The data is coming from a Java servlet, and I have set the status code to 500 whenever an error occurs. Although I can see the error on the console, I am unable to handle it in JavaScript. My g ...

What is the best way to retrieve a null value from an ArrayList in an Android application

Here is an example of a JSON structure: { "questions": [{ "Creator_User_ID": "140110", "Advisor_User_ID": null, "Question_Video_Title": "media", "playing_time": null, ...

I must provide a condition as a parameter

Essentially, I am in need of passing a condition as an argument to a method and continuously checking that condition until it changes. Here is an example of a method that presses a specific keyboard key until a condition is satisfied. For example, let&ap ...

Visual Studio Code is unable to identify the TypeScript module located within the `node_modules` directory

After cloning the tslint repository, running npm install and grunt as instructed in the README, I opened the folder in Visual Studio Code (0.9.1). Upon inspecting a .ts file such as src/rules/typedefRule.ts, TypeScript errors appeared regarding the require ...

Retrieving the name of the current page in ionViewCanEnter

While working with Ionic 2, I am currently facing a challenge in identifying the name of the page that triggered the navigation (PUSHER) before entering the destination page (PUSHEE). Within the PUSHEE page, I have an ionViewCanEnter function where I need ...

Error encountered while bundling CDK Synth with Node.js function - Kindly ensure to update your lock file by running `npm install` before proceeding further

As I attempt to utilize AWS CDK for creating a Lambda function, I am facing a challenging error: "npm ERR! npm ci can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file ...

Parsing a Yaml file to extract a Json string

I am currently working on parsing a yaml file that includes a field with a JSON structure. My goal is to extract this field as a String in my Java class. Here is the structure of my Java class: class A{ int field1; boolean field2; String field3; } ...

What is the best way to pause an animation in Kotlin?

Looking for assistance in delaying an animation before every repetition with a duration x. Can anyone help? This is the animation code snippet: val animations = arrayOf(-140f).map { translation -> ObjectAnimator.ofFloat(button, "translationX", tra ...

The call stack size has been exceeded in Next.js, resulting in a RangeError

Currently attempting to deploy my project on vercel.com but encountering an error specifically with 3 pages that have no internal errors. An error occurred while prerendering the page "/applications". For more information, visit: https://nextjs.org/docs/me ...

Leveraging a shared library in Typescript

How can I efficiently share code between different codebases that are all written in TypeScript and constantly being developed? I am seeking a straightforward solution. Some of the methods I have attempted include: 1 Utilizing import statements with path ...

Detection of multiple exceptions is not functioning properly in an obscured build and is not catching the exceptions

When utilizing a catch block with multiple exceptions, it seems to function correctly in an unobfuscated build but fails to catch exceptions in an obfuscated build. I have incorporated the proguard-maven-plugin in my project. try { ... } catch (Servle ...

What is the best approach for managing _app.js props when transitioning from a page router to an app router?

Recently, in the latest version of next.js 13.4, the app router has been upgraded to stable. This update prompted me to transition my existing page router to utilize the app router. In _app.jsx file, it is expected to receive Component and pageProps as pr ...

Issue with Angular Material date picker: Date Parsing UTC causing dates to display as one day earlier

After exploring numerous threads related to this issue and spending several days trying to find a solution, I may have stumbled upon a potential fix. However, the workaround feels too messy for my liking. Similar to other users, I am encountering an issue ...

Using TypeScript to implement Angular Draggable functionality within an ng-template

Sorry if this question has been asked before, but I couldn't find any information. I am trying to create a Bootstrap Modal popup with a form inside and I want it to be draggable. I have tried using a simple button to display an ng-template on click, b ...

Innovative Functions of HTML5 LocalStorage for JavaScript and TypeScript Operations

Step-by-Step Guide: Determine if your browser supports the use of localStorage Check if localStorage has any stored items Find out how much space is available in your localStorage Get the maximum storage capacity of localStorage View the amount of space ...

Transitioning to PDF Embed Window with Selenium

Is there a way to check if a new window pops up when the 'Generate PDF' link is clicked? I'm not concerned with the PDF content, I just want to make sure a new window appears when the link is clicked. I attempted to use the window handles c ...