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?
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