mercredi 11 novembre 2020

ERROR reflect.ValueOf(val).IsZero undefined (type reflect.Value has no field or method IsZero

I am new in Golang.

I want to use gorm in my code but when I run go run *.go, I see this Error, unfortunately.

/var/www/html/src/gorm.io/gorm/utils/utils.go:46:30: reflect.ValueOf(val).IsZero undefined (type reflect.Value has no field or method IsZero)

this is my coe :

package main

import (
    "gorm.io/gorm"
    "gorm.io/driver/sqlite"
)

type Product struct {
    gorm.Model
    Code  string
    Price uint
}

func main() {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
    panic("failed to connect database")
}

// Migrate the schema
db.AutoMigrate(&Product{})

// Create
db.Create(&Product{Code: "D42", Price: 100})

}




mardi 10 novembre 2020

Get parent Enumeration of Value typetag

I have a Type instance that refers to the Value of a specific Enumeration.

Is there any way to get the Type or Symbol of the parent enum? In other words, if I have typeOf[SomeEnumeration.Value] how can I obtain typeOf[SomeEnumeration]?

I know this information exists at runtime because I can see it in the debug console with valueType.pre.sym, but I can't come up with a public API for accessing it in code.

My first thought was valueType.typeSymbol.owner, but this just gives the Symbol for the base class Enumeration, not the specific enumeration instance that I want.





how to normalize a `scala.reflect.api.Types.Type`

How to implement the function normalize(type: Type): Type such that:

A =:= B if and only if normalize(A) == normalize(B) and normalize(A).hashCode == normalize(B).hashCode.

There is a deprecated method called normalize in the TypeApi, but it does not the same.

In my particular case I only need to normalize types that represent a class or a trait (tpe.typeSymbol.isClass == true).





Get generic delegate return type

I got a c# method which receives a generic Delegate as a parameter and after invocation checks the type of the result:

public async Task<TResult> InvokeAction<TResult>(Delegate action, object[] actionArgs = null)
{
...
    var result = action.DynamicInvoke(actionArgs);
    if (result is Task<TResult> task) return await task;
}

Is there a way I can check in advanced if the return type of the delegate parameter is indeed TResult without the need to invoke it first? This without changing the parameter the Func<TResult>





Compare Dart @override Metadata Reflectee to Instance of _Override

I am trying to come up with an example of how we could search for the @override metadata annotation using reflection in Dart.

In the examples I used to learn the dart:mirrors library and reflection, they were always searching for custom made annotations.

Here is an example where they search for a custom "Todo" annotation

When searching for custom made annotations, they would simply compare the metadata's reflectee to the class data type to check for a match.

In Dart's documentation linked below, you can see an example implementation of an _Override instance.

Here is Dart documentation on the override constant

This lead to me to try:

if(meta.reflectee is _Override) {
    print('Found!);
}

But the "_Override" cannot be resolved and suggest no imports to access such a data instance.

I am able to toString the reflectee for comparison but I feel like it is a dirty solution:

if (meta.reflectee.toString() == "Instance of '_Override'") {
    print('Found!');
}

When using the @override annotation, I am struggling to find a way to compare the metadata's reflectee to the instance type of _Override.

Here is the Dog class:

class Dog extends Animal {
  Dog(String name) : super(name);

  @override
  void makeNoise() {
    print('Bark, bark!');
  }
}

Here is my reflection search code:

 Dog dog = Dog('Harper');

  InstanceMirror instanceMirror = reflect(dog);
  ClassMirror classMirror = instanceMirror.type;

  classMirror.instanceMembers.forEach((_, member) {

    print(member.owner.simpleName);
    print(member.simpleName);
    print(member.isRegularMethod);

    member.metadata.forEach((meta) {
      print(meta.reflectee);
      if (meta.reflectee is _Override) {
        print('Found!');
      }
      
    });
  });

Finally, here is my output when the instanceMembers.forEach loop gets to the method I am interested in:

Symbol("Dog")
Symbol("makeNoise")
true
Instance of '_Override'




lundi 9 novembre 2020

I was wondering if it is possible to have function return a reference to itself

I was wondering if it is possible to have a function return a reference to itself specifically without using an object to facility the reflection. (don't ask why.) This kind of works but no dice because I'm not asking about a bound method I'm asking about function.

>>> class test:
...     def ex(self):
...             return(self.ex)
...
>>> t = test()
>>> a = t.ex()
>>> b = t.ex
>>> a==b
True
>>> a()
<bound method test.ex of <__main__.test object at 0x033189F0>>
>>> b()
<bound method test.ex of <__main__.test object at 0x033189F0>>

I also tried with a def but obviously I had nothing to return...

>>> def test():
...     return(self)
...
>>> test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in test
NameError: name 'self' is not defined

Finally I typed the word lambda into the terminal and then just said 'yeah, that's not happening... lulz'.





Is it possible to have a method that returns a reference to itself in python? [closed]

I was wondering if it is possible to have method return a reference to itself. (don't ask why.) I've tried to use a class for self and get reflection there but no dice...

>>> class test:
...     def ex(self):
...             return(self.ex)
...
>>> t = test()
>>> a = t.ex()
>>> b = t.ex
>>> a==b
True
>>> a()
<bound method test.ex of <__main__.test object at 0x033189F0>>
>>> b()
<bound method test.ex of <__main__.test object at 0x033189F0>>
>>> a(t)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ex() takes 1 positional argument but 2 were given
>>> b(t)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ex() takes 1 positional argument but 2 were given

And I also tried with just a def but obviously I had nothing to return...

>>> def test():
...     return(self)
...
>>> test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in test
NameError: name 'self' is not defined

Finally I typed the word lambda into the terminal and then just said 'yeah, that won't help either huh... lulz'.