Unable to create a GArray in Gjs - gnome

I try to create a GArray but I have always the same error :
const GLib = imports.gi.GLib;
var garray = new GLib.Array(true, true, 1); // Unable to construct
boxed type Array since it has no zero-args , can only
wrap an existing one
var garray = new GLib.Array(); // Unable to construct boxed type Array
since it has no zero-args , can only wrap an existing one
I need a GArray to use it in the GnomeKeyring.item_create_sync method :
GnomeKeyring.item_create_sync(
"login",
4,
"name",
new GLib.Array(true, true, 1)
"pass",
true);
I tried other type of array instead of GArray, here are the errors
[] -> Object 0xb5d120d0 is not a subclass of (null), it's a Array
new Array() -> Object 0xb5e12138 is not a subclass of (null), it's a Array
{} -> Object 0xb5d0b1b0 is not a subclass of (null), it's a Object
5 -> Unhandled GType GArray unpacking GArgument from Number
"5" -> Expected type interface for Argument 'attributes' but got type 'string'
If anyone has a solution for GArray or for GnomeKeyring.item_create_sync

Read /usr/share/gir-1.0/GnomeKeyring-1.0.gir and search for function name="item_create_sync". You have all required parameters there and it seems to me that you need a plain list. GnomeObjectIntrospection will translate it to the GArray when needed.

The GNOME Shell's retrospection will convert between JS and GNOME types on its own accord where possible and required. If you create an array in the JS space and pass it to a function that requires a GArray, the Shell will convert if you pass the array as argument.
The only objects that aren't converted are those whose classes exist in one space, but not the other. Like the GNOME File Objects, as JS has no native filehandlers.
You can use the .toString() method on filestreams to get their contents as a string, or call the reading methods to get the binary values as an array of integers.

Related

Hazelcast Predicate in clause

Can anyone tell me how to 'in' clause with Hazelcast predicate. I want to use the following,
Predicates.in('column',value);
I want to pass the value as an ArrayList of values. But it is throwing error as it expects object that implements comparable. Is there any workaround for this?
Predicates.in takes arguments of types (String, Comparable...).
So for the column name you pass a String as you have done.
Comparable... means you can pass the individual values in a comma-separated list, but you can also pass them in an array. An ArrayList won't automatically be converted, but you can do it as follows:
ArrayList<String> values = new ArrayList<>(Arrays.asList("one", "two", "three");
Predicates.in("column", values.toArray(new String[]));
The (new String[]) argument is just to ensure you get back an array of Strings, otherwise you'll get an array of Objects.

How to store any class object in an hash map

I have a hash map which acts as store of objects - where key is class name & value is object
store = {} of String => Type
store["Animal"]= Animal.new
store["Book"]= Book.new
store["Car"]= Car.new
Here class is unknown to me, which means i can't use union type. Please tell me how to solve this problem ?
I am trying to create a wrapper around hash map for storage of objects, which will save object & return object by key.
Update 1
Classes will be passed as a parameter - like this
add_in_store(Animal)
add_in_store(Person)
Update 2
Crystal play link of what i am trying to do - https://play.crystal-lang.org/#/r/8lwx
Solution of problem doesn't have to be in same way as what i m doing. It can be with any other approach like using proc or macro etc.
Hash is a generic type, to wrap it in a way where you cannot or don't want to restrict the possible types of the generic arguments, you should make your wrapper itself generic.
class Store(V)
#store = {} of String => V
def add(name, value : V)
#store[name] = value
end
end
Then the consumer of your wrapper has to give the possible types.

what's the meaning of "type tags" in nodejs

assert.deepStrictEqual(actual, expected[, message]) in nodejs's docs:
Type tags of objects should be the same.
what's the meaning of "type tags"
const date = new Date();
const object = {};
const fakeDate = {};
Object.setPrototypeOf(fakeDate, Date.prototype);
// Different type tags:
assert.deepStrictEqual(date, fakeDate);
typeof date and typeof fakeDate ,The results are all object, but different type tags
Type tags in Javascript, are referred to the word returned by typeof
For example for primitive values:
typeof({}) // returns 'object', this is the type tag
For non-primitive:
Object.getPrototypeOf(new Date) // returns 'Date {}' this is the type tag
If typeof is used with Date it will returns object which is right, because that would be the type tag for the primitive value, this is why using Object.getPrototypeOf is more accurate.
In the firsts JS implementations, the type tag were stored the first 1–3 bits and the the remaining 29–31, contained the actual data.
What the NodeJS docs says, it's that the result of Object.getPrototypeOf function when comparing two objects has to be the same to be considered as equal.
This is an old question that has been already answered, but I thought I might add that, although:
In the first implementation of JavaScript, JavaScript values were
represented as a type tag and a value. The type tag for objects was 0.
null was represented as the NULL pointer (0x00 in most platforms).
Consequently, null had 0 as type tag, hence the typeof return value
"object".
Type tags can also refer to an object's toStringTag.
The Symbol.toStringTag well-known symbol is a string valued property
that is used in the creation of the default string description of an
object. It is accessed internally by the Object.prototype.toString()
method.
The value associated to the well-know Symbol.toStringTag is used as the default string description of an object. Since every built-in object has a toStringTag value (except for null prototypes), it can be used to detect an object's class.
There's a package called typetags that has more information about it: typetags.org

Runtime meta programming in ceylon

I get unexpected results from the following code snippet using the eclipse ide:
class Example(String s = "init") {
shared String a() => "Func 1";
shared String b = "Attr 1";
shared String c(Integer i) { return "Func 2"; }
}
shared
void run() {
// 1.
print("getAttributes: " + `Example`.getAttributes().string);
print("getMethods: " + `Example`.getMethods().string);
// prints: []
// i.e. doesnt print any attribute or method
// 2.
value e = Example()
type(e); // error
e.type(); // error, should be replaced by the ide with the above version.
}
ad 1.) I get as a result:
getAttributes: []
getMethods: []
where I expected lists containing attributes or methods.
ad 2.) The doc says:
"The type() function will return the closed type of the given instance, which can only be a ClassModel since only classes can be instantiated.
..."
But I cant find the type() function and others related to meta programming, i.e. I dont get a tooltip, but instead I get a runtime (!) error:
Exception in thread "main" com.redhat.ceylon.compiler.java.language.UnresolvedCompilationError: method or attribute does not exist: 'type' in type 'Example'
So, where is the equivalent to the backticks `Example`.... as a function ?
`Example`.getAttributes() returns an empty list because getAttributes takes three type arguments: Container, Get, Set. When called as getAttributes(), the typechecker tries to infer them, but since there’s no information (no arguments with the appropriate type), the inferred type argument is Nothing. Since Nothing is not a container of any of the class’ members, the resulting list is empty. Use getAttributes<>() instead to use the default type arguments, or specify them explicitly (e. g. getAttributes<Example>()). Same thing with getMethods(). Try online
The type function is in ceylon.language.meta and needs to be imported: import ceylon.language.meta { type }. Once you do that (and remove the e.type() line), the compilation error will go away.
If you directly want a meta object of the function, you can write `Example.a`.

Casting to types that are know only at Runtime by their string names. C#

I've got a problem here. (C#)
There's a collection in another assembly (I cannot change it) that takes a string as parameter and returns an object.
Like:
object Value = ThatCollection.GetValue("ParameterName");
The problem is, for each parameter string, it returns a DIFFERENT type as object.
What I want is to cast those objects to their respective types, knowing the types only at runtime by their string names.
I need to do some operations with those returned values.
And for that I need to cast them properly in order to access their members and so.
Limitations:
I cannot use "dynamic" since my code needs to be done in an older framework: 3.5 (because of interop issues).
I need to do operations with MANY returned values of different types (no common interfaces nor base classes, except "object", of course)
All I have is a table (containing string values) correlating the parameter names with their returned types.
Yes, I could transform that table into a biiig "switch" statement, not very nice, don't want that.
Any hints??
You want to look into reflection, something like the following should work to cast an object to type T. Set up a simple cast method:
public static T CastToType<T>(object o)
{
return (T)o;
}
Invoke this using reflection:
Type t = Type.GetType(stringName)
MethodInfo castTypeMethod = this.GetType().GetMethod("CastToType").MakeGenericMethod(t);
object castedObject = castTypeMethod .Invoke(null, new object[] { obj });

Resources