Home > database >  How do I explicitly declare the type of the variable as object in Haxe?
How do I explicitly declare the type of the variable as object in Haxe?

Time:02-17

If I have a variable called str that is a String, I can do the following

var str:String = "value";

However, I don't know what kind of type I need to use for a generic object like this

var myObj:???? = {
  key1: value1,
  key2: value2
}

I know I don't need to declare it but I still want to know what the proper text is in place of ????

Type.typeof() says it's a TObject but I can't use that.

CodePudding user response:

it depends on key types but you might want something like this:

    var myObj:{key1:String, key2:String} = {
      key1: "foo",
      key2: "bar"
    };

CodePudding user response:

If you plan on reusing this structure you should look at defining a Class or Typedef. Otherwise, you don't necessarily have to provide a type. You could just do the following and let the compiler assign its own type:

var myObj = {
      key1: "foo",
      key2: "bar"
    };

If using a typedef you would do like:

typedef FooBar = {
  var foo : String;
  var bar : String;
}
var myObj:FooBar = {
  foo: "foo",
  bar: "bar"
};
  • Related