Haxe get function parameter type - reflection

I have haxe code like this:
var fn:String->Int = function(s:String):Int{
return 1;
}
getParameterType(fn,1);//Should return String as it is first parameter of fn
what should function getParameterType code look like?

The Haxe Reflection APIs can be found here: Reflect and Type.
Looking through them, there doesn't seem to be any way to check the types of arguments on functions, probably because this information isn't available at runtime on many of the platforms. Javascript for instance is loosely typed, and the information you are looking for is not included by default.
So you have three options:
Use RTTI (Run Time Type Info). If a class is marked with #:rtti metadata (in Haxe 3, or if it implements haxe.rtti.Infos in Haxe 2), then information about that class, including the types of function parameters, is available in Xml format at run time. You will have to look at the Xml to figure out what the argument is. This will only work for functions which are attached to classes though, it won't work for anonymous functions.
Use macros. This is outside the scope of my answer, but maybe ask on the Haxe mailing list if you need help :) If the argument type is known at compile time, it can be made known to macros, and you can probably save that information back somewhere so it is available at run time.
Figure out another usage that doesn't require you to know the type :)
Of course, if you only need the information at compile-time, not at run-time, you can do: $type(fn) anywhere in your code and when you compile it will let you know the exact type signature of "fn".

Related

Kotlin KFunction reflection - get parameter values

In order to create a simple annotation that logs function calls, I'm trying to grab the following attributes from a function that has said annotation:
Function name
Parameter names
Parameter values
What I have so far uses KCallable as a value, which makes grabbing the name and names from the list of KParameter fairly simple. However, I cannot figure out how to get the values of said parameters to make the log statement more contextual.
Does anyone have ideas on grabbing these parameters values within the annotation? It doesn't need to use KCallable, that just seemed like the most intuitive receiver.
You will need a different approach. Annotations and parameter type are a compile time features while values are a runtime feature.
What you will have to do is use a bytecode processing framework like ASM or google "aspect oriented programming". That allows you to examine the generated bytecode and modify if before the JVM tries to execute it.
The other approach is to write a Kotlin compiler plugin which generates the necessary code (google "Writing Your First Kotlin Compiler Plugin")
This blog post contains an example for Java and Spring using the AOP approach: https://solocoding.dev/blog/eng_spring_centrlize_logging_with_aop
I recommend the compiler plugin because the other approach is much more complicated, brittle and badly documented. Use AOP only if you find a framework which already contains all the features you need.

what are the usage of callStaticObjectMethod callStaticMethod

The qt documentation is poor in QAndroidJniObject object. Could you possibly tell me what the difference between callStaticObjectMethod and callStaticMethod is and when should I use them?
If you have any working example, It would be appreciated if address them in you answers. (like Q notifier)
Thank you
As I've mentioned in a comment, the callStaticMethod is used with primitive types (which are listed here). This function returns the type you requested (ex. jint) not QAndroidJniObject.
And callStaticObjectMethod is used with methods returning an object type (which are listed here). It returns the QAndroidJniObject.
You can find example code in Qt unit test files on their git repository

How to create a closure from String in Dart?

How to use dart-mirror API to create a anonymous closure dynamically?
Like as the interpreter, compile the code during run-time.
var funcstr='bool (String s){ return (s==null); }';
var func=parseStr(funcstr);
// func(s)-> s==null;
var r=func('false');
// r=false;
so, how to do with "parseStr"?
my project:
http://github.com/stevehsu77/surebet
At the moment there is no way to do this. Dart has no eval and no code generation at runtime.
But it is something Gilad Bracha (the language spec lead of Dart) wants to have (https://groups.google.com/a/dartlang.org/forum/#!topic/misc/6O4g7eEHgOU) at least for the development environment.
Also
We’d like to support more powerful reflective features in the future. These would include mirror builders, designed to allow programs to extend and modify themselves, and a mirror-based debugging API as well.
https://www.dartlang.org/articles/reflection-with-mirrors/
So it'll probably be supported some time in the future. But right now it's not possible.
As mentioned above, Dart does not have eval, however it is possible to load new source code in another isolate using spawnUri().
I am not sure if there are any examples of how to use this. Perhaps post a message on the dart discussion group.
Using isolates and spawnUri() is quite a different than using eval, so it may not be the right fit for your project.

How to call calabash xml from a Java program

I try to set up a web based application using spring and xslt. Since i always use xslt in a pipelining style, i would like to use calabash. Is there a possibility to call calabash from Java? I read thru the documentation on http://xmlcalabash.com but there is only a description how to use it from command line. I also tired to find some javadoc on githup but wasn't successful. Obviously, there is the Main class with the main() method and i could supply the command line parameters as a string array...
I wonder if there is a better way to do it.
I looked into this recently too. I took a pragmatic approach where I call Main.run(), and pass in a string array that I generate from a (File)Properties object. It doesn't allow passing in file inputs as streams or sources however, they must reside on the file-system.
Likely there are nicer ways. You could for instance look into http://expath.org/ . There should be sources of that project. The webapp modules (formerly known as servlex?) seems to provide XMLCalabash integration.
HTH!

Determining type of CollectionBase via Reflections (or Microsoft.Cci)

Question:
Is there a static way to reliably determine the type contained by a type derived from CollectionBase, using Reflection or Microsoft.Cci?
Background:
I am working on a code generator that copies types, makes customized versions of those types, and converters between. It walks the types in the source assembly via Microsoft.Cci. It prints out source code using textual templates. It does a lot of conversion and customization, and tosses out code that I don't care about.
In my resulting code, I intend to replace List<T> everywhere that a CollectionBase, IEnumerable<T>, or T[] was previously used. I want to use List<T> because I am pretty sure I can serialize it without extra work, which is important for my application. T is concrete in every case. I am trying not to copy CollectionBase classes because I'd have to copy over the custom implementation, and I'd like to avoid having to do that in my code generator.
The only part I'm having a problem with is determining T for List<T> when replacing a custom CollectionBase.
What I've done so far:
I have briefly looked at the MSDN docs and samples for CollectionBase, and they mention creating a custom Add method on your derived type. I don't think this is in any way enforced, so I'm not sure I can rely on that. An implementor could name it something else, or worse, have a collection that supports multiple types, with Object as their only common ancestor.
Alternatives I have considered:
Maybe the default serialization does some tricks that I can take advantage of. Is there a default serialization for CollectionBase collections, or do you generally have to implement it yourself? If you have to do it yourself, is there some reliable metadata I could look at in order to determine the types? If it supports default serialization, does it rely on the runtime types of the items in the collection?
I could make a mapping in my code generator of known CollectionBase types, mapped to their corresponding T for List<T>. If a given CollectionBase type that I encounter isn't in the list, throw an exception. This is probably what I'll go with if I there isn't a reliable alternative.
I'm still not sure enough about what you want to do to give advice. Still, do your CollectionBase-derived classes all implement Add(T)? If so, you could look for an Add method with single parameter of type other than object, and use that type for T.

Resources