Question / request re: Var


#1

Hello all.

I’m finding the ValueTree very useful in rewriting a better version of my program, using it especially for undo/redo and state recall. With the individual elements, the vars, I presume one of the primary uses is to replace the basic member elements of objects into the ValueTree. Once this has been done, the values need to be retrieved from the ValueTree for use by the object. To do this with a number you have to convert the var to a String using toString(), then into whatever number you want using, for example, getIntValue(). Is it possible to add toInt(), toFloat() etc to var so you can reduce the method calls and shorten the amount of code?

So that:

valueTree.getProperty (“property”).toString().getIntValue();

becomes

valueTree.getProperty (“property”).toInt();

Thank you.


#2

You can do e.g.

valueTree.getProperty("stringProperty").operator String()
valueTree.getProperty("intProperty").operator int()

#3

Ah, thank you!


#4

I second that proposal. Especially since c++11 is widely adopted and one tends to initialize variables with auto.


#5

isn’t it the same?

 (int)valueTree.getProperty("intProperty")

#6

Correct modern syntax is:

static_cast<int> (valueTree.getProperty ("foo"))
static_cast<String> (valueTree.getProperty ("foo"))

Never use C-style casts where classes are involved.

But often you don’t need the cast if e.g. you’re passing it into a function that takes an int or String.


#7

True. But often you also want to assign a variable.


#8

Sure, so you’d write:

String s = valueTree.getProperty ("foo");

Still no need for a cast.


#9

I didn’t consider that a property would deduce according to the type to which it was passed, thank you!