Maybe with Introspection?

I have an idea for a class that might be quite handy… but it needs to be able to
a) retrieve a variables datatype and value given the NAME
b) the variable must be “any” legal XOJO variable in the current scope



where if the named variable did not exist in the current scope (or was an invalid variable name), it would return some predetermined values (but not an error)

can Introspection do this?
Does someone have a snippet?

Introspection can not do this because it has no idea about the callers scope. You can iterate through created objects, but that will not tell you scope nor will iterate through intrinsic types declared in any given method.

Maybe give your end goal? There could be another way?

end goal… create a function that returns a string… based on the concept in SWIFT

s="Variable #1 is \\(variable_name) Variable #2 is \\(var2_name)"

instead of current

s="Variable #1 is "+variable_name+" Variable #2 is "+str(var2_name)"

I think you’re going to be out of luck for something like that in Xojo. Best I can think of is something like (which you have to roll your own, just given use ideas):

s=Expand("Variable #1 is ", var1, " variable #2 is ", var2)

// or

const kTemplate = "Variable #1 is @Var1 Variable #2 is @Var2"

s=kTemplate.Map("@Var1" : var1, "@Var2" : var2)

// or

const kTemplate = "Variable #1 is $(1), Variable #2 is $(2)"

s = kTemplate.Expand(var1, var2)

which obviously is more work than simple string expansion and incurs method overhead costs. I’m sure others have ideas too.

Of the above, I really like the third. Define your strings as constants, then in various languages you can reorder the variable expansion even.

But… to your original question, I think you’re out of luck. Maybe a Xojo Engineer can chime in.

As far as I know introspection doesn’t have access to things like locals etc in the current lexical scope
Swift & Obj-C have a runtime that provides a lot of things that make this sort of thing possible
The Java VM provides a lot of this in Java and other languages running on the Java VM

Xojo is more like running C++ where this sort of thing isn’t available from the C/ C++ runtimes

Thats too bad… that one feature is perhaps (my opinion) one of the most useful “differences” I’ve found in SWIFT, and I was hoping to somehow bring that to XOJO as well.

oh I can think of a ton that’d be cool but without something to mimic a great portion of the Obj-C runtime its probably unlikely
Things like Class By Name, Responds To Selector and many others require that runtime or something like it in order to get the kind of dynamism Obj-C and Swift have

I’ve seen this before but can’t find reference… how 'bout something like

s = concat("Variable #1 is \\(d) and Variable #2 is \\(i)", myDouble, myInt)

yeah you could probably write something that took a format string + param array of variants
it would not be identical but …

I think there’s an old version of PrintF from joe strouts stringutils that might be close or at least be a decent starting point

Also in my M_String module.

Thanks… but was hoping to do it in the same manner, not a “sorta like”

Nope - sorta like is all you’ll be abel to get
Introspection has no idea about the current lexical scope, local variables, etc