reflection

How to access parameter list of case class in a dotty macro

南笙酒味 提交于 2020-07-18 06:31:05
问题 I am trying to learn meta-programming in dotty. Specifically compile time code generation. I thought learning by building something would be a good approach. So I decided to make a CSV parser which will parse lines into case classes. I want to use dotty macros to generate decoders trait Decoder[T]{ def decode(str:String):Either[ParseError, T] } object Decoder { inline given stringDec as Decoder[String] = new Decoder[String] { override def decode(str: String): Either[ParseError, String] =

How to load a class from the source code using reflection inside SBT task?

送分小仙女□ 提交于 2020-07-09 09:34:30
问题 I'm using SBT to build my project. I want to analyze some classes from my source code using either Scala or Java reflection during the build process. How do I define an SBT task that loads a single known class or all classes from my source code? import sbt._ val loadedClasses = taskKey[Seq[Class[_]]]("All classes from the source") val classToLoad = settingKey[String]("Scala class name to load") val loadedClass = taskKey[Seq[Class[_]]]("Loaded classToLoad") 回答1: You can use the output of the

Can't use range on slice made with reflect then passed json.Unmarshal

て烟熏妆下的殇ゞ 提交于 2020-07-08 02:31:17
问题 I am getting the following errors from the code below: invalid indirect of typedSlice (type interface {}) cannot range over typedSlice (type interface {}) This is confusing to me because reflect.TypeOf(copy) matches the type of t . func Unmarshal(t reflect.Type) []interface{} { ret := []interface{}{} s := `[{"Name":"The quick..."}]` slice := reflect.Zero(reflect.SliceOf(t)) o := reflect.New(slice.Type()) o.Elem().Set(slice) typedSlice := o.Interface() json.Unmarshal([]byte(s), typedSlice) fmt

How to invoke a class method using performSelector() on AnyClass in Swift?

☆樱花仙子☆ 提交于 2020-07-06 10:41:40
问题 In ObjC you could simply invoke a class method using the class method from NSObject . [Machine performSelector:@selector(calculate:) withObject:num]; But how do you do this in Swift 2.2? @objc(Machine) // put it here, so you can simply copy/paste into Playground class Machine: NSObject { static func calculate(param: NSNumber) -> String { if param.integerValue > 5 { return "42" } return "42" // there is only 1 answer to all the questions :D } } if let aClass = NSClassFromString("Machine") {

How to invoke a class method using performSelector() on AnyClass in Swift?

北慕城南 提交于 2020-07-06 10:41:26
问题 In ObjC you could simply invoke a class method using the class method from NSObject . [Machine performSelector:@selector(calculate:) withObject:num]; But how do you do this in Swift 2.2? @objc(Machine) // put it here, so you can simply copy/paste into Playground class Machine: NSObject { static func calculate(param: NSNumber) -> String { if param.integerValue > 5 { return "42" } return "42" // there is only 1 answer to all the questions :D } } if let aClass = NSClassFromString("Machine") {

How to get struct field names in Rust? [duplicate]

安稳与你 提交于 2020-07-06 08:47:00
问题 This question already has answers here : Is there is a way to get the field names of a struct in a macro? (2 answers) Closed 4 years ago . Is there some equivalent of JS's Object.keys() for Rust's struct? I need something to generate CSV headers (I use rust-csv) from structure field names. struct Export { first_name: String, last_name: String, gender: String, date_of_birth: String, address: String } //... some code let mut wrtr = Writer::from_file("/home/me/export.csv").unwrap().delimiter(b'

kotlin geneirc with vararg parameter

半腔热情 提交于 2020-06-29 04:17:10
问题 Now I am working on some problems about passing the function as parameter in a function with vararg parameter with generic parameter. The below code works for one function as parameter into other function like that funA(funB(parameter):return1):return2 but when i make function like that funA(vararg funB(parameter):return1):return2 it doesn't works. I have tried somethings like Array or KFunction1 ViewModel fun callMultipleAPI( vararg observable: Observable<Any>):LiveData<Boolean>{ .....

C# create excel sheet late bound

不想你离开。 提交于 2020-06-27 15:52:11
问题 Using winforms, C# FW4.5 to open an excel sheet with late bound, like this: objExcel = CreateObject("Excel.Application") Now I want to use the InvokeMember method, but I don't know all the members of excel I can invoke. For example, I know I can call it like this: InvokeMember("Close",... in order to close excel, but where can I find list of all the members I can invoke and what each one of them does? 回答1: Late-bound Using winforms, C# FW4.5 to open an excel sheet with late bound, like this:

Which java.lang.Class method generates the right input for Class.forName()?

有些话、适合烂在心里 提交于 2020-06-27 06:45:14
问题 I would like to write some code like this: Object o = ...; String oTypeName = o.getClass().getName(); //on the other side of the wire: Class<?> oClass = Class.forName(oTypeName); Object oAgain = oClass.newInstance(); However, it's not clear from the javadoc which method I should use to initialize oTypeName , i.e. which method will produce the expected input to Class.forName(): getCanonicalName(): "Returns the canonical name of the underlying class as defined by the Java Language Specification

What is the use of reflection in Java/C# etc [duplicate]

天大地大妈咪最大 提交于 2020-06-25 07:33:28
问题 This question already has answers here : What is reflection and why is it useful? (21 answers) Closed 3 years ago . I was just curious, why should we use reflection in the first place? // Without reflection Foo foo = new Foo(); foo.hello(); // With reflection Class cls = Class.forName("Foo"); Object foo = cls.newInstance(); Method method = cls.getMethod("hello", null); method.invoke(foo, null); We can simply create an object and call the class's method, but why do the same using forName,