Why can I not return a reference to a dictionary value? - ref

public class PropertyManager
{
private Dictionary<ElementPropertyKey, string> _values = new Dictionary<ElementPropertyKey, string>();
private string[] _values2 = new string[1];
private List<string> _values3 = new List<string>();
public PropertyManager()
{
_values[new ElementPropertyKey(5, 10, "Property1")] = "Value1";
_values2[0] = "Value2";
_values3.Add("Value3");
}
public ref string GetPropertyValue(ElementPropertyKey key)
{
return ref _values[key]; //Does not compile. Error: An expression cannot be used in this context because it may not be returned by reference.
}
public ref string GetPropertyValue2(ElementPropertyKey key)
{
return ref _values2[0]; //Compiles
}
public ref string GetPropertyValue3(ElementPropertyKey key)
{
return ref _values3[0]; //Does not compile. Error: An expression cannot be used in this context because it may not be returned by reference.
}
}
In the above example GetPropertyValue2 compiles, but GetPropertyValue and GetPropertyValue3 do not.
What is wrong with returning a value from a dictionary or list as reference, while it does work for an array?

I'd like to add my answer to the 'pot', maybe it makes things a bit more clear.
So, why doesn't that work for lists and dictionaries? Well, if you have a piece of code like this:
static string Test()
{
Dictionary<int, string> s = new Dictionary<int, string>();
return s[0];
}
This (in debug mode) translates to this IL code:
IL_0000: nop
IL_0001: newobj instance void class [mscorlib]System.Collections.Generic.Dictionary`2<int32, string>::.ctor()
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldc.i4.0
IL_0009: callvirt instance !1 class [mscorlib]System.Collections.Generic.Dictionary`2<int32, string>::get_Item(!0)
IL_000e: stloc.1
IL_000f: br.s IL_0011
IL_0011: ldloc.1
IL_0012: ret
This, in turn means that what you do with one line of code (return s[0]) is actually a three-step process: calling the method, storing the return value in a local variable and then returning the value that is stored in that local variable. And, as pointed out by the links the others have provided, returning a local variable by reference is not possible (unless the local variable is a ref local variable, but as pointed out by the others again, since Diciotionary<TKey,TValue> and List<T> does not have a by-reference return API, this is not possible either).
And now, why does it work for the array? If you look at how array-indexing is handled more closely (i.e. on IL-code level), you can see that there is no method call for array indexing. Instead, a special opcode is added to the code called ldelem (or some variant of that). A code like this:
static string Test()
{
string[] s = new string[2];
return s[0];
}
translates to this in IL:
IL_0000: nop
IL_0001: ldc.i4.2
IL_0002: newarr [mscorlib]System.String
IL_0007: stloc.0
IL_0008: ldloc.0
IL_0009: ldc.i4.0
IL_000a: ldelem.ref
IL_000b: stloc.1
IL_000c: br.s IL_000e
IL_000e: ldloc.1
IL_000f: ret
Of course this looks like the same as it was for the dictionary, but I think the key difference is that the indexer here generates an IL-native call, not a property (i.e. method) call. And if you look at all the possible ldelem variants on MSDN here, you can see that there is one called ldelema which can load the address of the element directly to the heap. And indeed, if you write a piece of code like this:
static ref string Test()
{
string[] s = new string[2];
return ref s[0];
}
This translates to the following IL code, utilizing the direct-reference loading ldelema opcode:
IL_0000: nop
IL_0001: ldc.i4.2
IL_0002: newarr [mscorlib]System.String
IL_0007: stloc.0
IL_0008: ldloc.0
IL_0009: ldc.i4.0
IL_000a: ldelema [mscorlib]System.String
IL_000f: stloc.1
IL_0010: br.s IL_0012
IL_0012: ldloc.1
IL_0013: ret
So basically, array indexers are different and under the hood, arrays have support for loading an element by reference to the evaluation stack via native IL calls. Since the Dictionary<TKey,TValue> and other collections implement indexers as properties, which result in method calls, they can only do this if the method called explicitly specifies ref returns.

Related

Convert variable reference name to its value in Dart

I'm trying to access the variable reference by its name given in a String type.
class A {
B b = B();
}
class B {
String c = "apple";
}
void main() {
A a = A();
print(a.b.c); // prints apple
/// now if I get this ref as a string eg:
final ref = "a.b.c";
/// want to access the same variable with its reference
print(<ref>); <------
}
Need this to create a dynamic functionality.
Already tried with the creation of the toJson() method. but we have to access more nested classes and update all the classes with toJson method.
please help if there is any other solution.

access arguments similar to this.variable

void main() {
doStuff("foo");
}
void doStuff(String myString) {
String myString = "bar";
print(myString); //prints "bar"
}
The above executed in the dartpad yields "bar", the local variable is used instead of the parameter in the last line. If instead of an argument myString were an instance variable of a class, we could access the original myString with this.myString to print "foo". Is there an analogous way to access an argument to a function after a homonymous local variable has been declared?
No.
For local variables, you are expected to be able to rename any local variable which shadows the variable you want to access, so there is no scope override.
For instance variables, you can use this.x, for static variables you can use ClassName.x, for an imported top-level name, you can import the library with a fresh prefix name and access it as prefix.x, and for public top-level variable of the current library, you can, if all else fails, import that library with a prefix too and use:
// library foo.dart
import "foo.dart" as toplevel; // <- this library!
// ...
int foo = 42;
// ...
int method(int foo) {
return toplevel.foo + foo; // Wohoo
For local variables, you just have to rename the one that's shadowing the variable you want. Since nobody can see local variables from outside the method, no-one will ever know.

How can I use `class` as Map's key?

I use Piece class as Map's key.
But when this code ran, error occured Uncaught exception:
C.JSNull_methods.$indexSet is not a function.
class Piece {
int type;
Piece(this.type);
}
void main() {
Map<Piece, int> hand;
hand[Piece(5)] = 5;
if (hand.containsKey(Piece(5))) {
print("contains");
}
print('${hand[Piece(5)]}');
}
In dart-lang, how can I use class as Map's key?
First, the error you got has nothing to do with using types as keys but are before you never initialize the hand variable. So you need to do this:
Map<Piece, int> hand = {};
Now, you will not get the exception but your code will properly not work as expected since hand.containsKey(Piece(5)) will return false and print('${hand[Piece(5)]}') will return null.
This is because the map Map<Piece, int> are not using the Type as key but instead objects of the type Piece. So if we take your code here:
Map<Piece, int> hand = {};
hand[Piece(5)] = 5;
if (hand.containsKey(Piece(5))) {
print("contains");
}
print('${hand[Piece(5)]}');
You are here creating a new object instance of the Piece type each type you are writing "Piece(5)". Since each of this objects will be a separate instance of a Piece then you will not receive the value 5 you have saved because the value 5 has been saved for a different object than you are requesting.
There are multiple solutions for that and I don't know which one are the best for you. But the simple solution in this case is to either only creating one instance of Piece and reuse that:
void main() {
Map<Piece, int> hand = {};
final piece = Piece(5);
hand[piece] = 5;
if (hand.containsKey(piece)) {
print("contains");
}
print('${hand[piece]}');
}
Or make a const constructor for your Piece class so instances with the same arguments are made into the same object. This solution requires that the int type are final since you cannot edit a const constructed object (since it is constant):
class Piece {
final int type;
const Piece(this.type);
}
void main() {
Map<Piece, int> hand = {};
hand[const Piece(5)] = 5;
if (hand.containsKey(const Piece(5))) {
print("contains");
}
print('${hand[const Piece(5)]}');
}
Note that you need to prefix you object instantiation with const like "const Piece(5)" each time you want a instance where you are sure it will returns the same object for the same arguments.

Get pointer to a struct from a Dart_NativeArguments struct in C

I'm trying to wrap a C library using Dart. I call into a C function from dart and pass in the arguments through a Dart_NativeArguments struct in C:
void _sayHello(Dart_NativeArguments arguments) {
string from;
Dart_Handle seed_object = HandleError(Dart_GetNativeArgument(arguments, 0));
if (Dart_IsString(seed_object)) {
const char* seed;
HandleError(Dart_StringToCString(seed_object, &seed));
from = seed;
}
num = (int)Dart_GetNativeArgument(arguments, 1);
Dart_SetReturnValue(arguments, HandleError(Dart_NewStringFromCString(sayHello(from, num).c_str())));
}
In Dart, I call the function and pass in the necessary arguments
String sayHello(String from) native "sayHello";
main() {
print(sayHello("Dart"));
}
I was wondering how I could pass in pointers (to a struct I made) instead of just strings and ints as arguments. There are functions in Dart to convert Dart_Handles into Strings and ints but not pointers. What is the internal structure of the Dart_Handle and how would I go about converting it back to a pointer? For example:
Dart code:
String sayHello(info from) native "sayHello";
class info
{
String message;
int num;
}
main() {
info tester = new info();
tester.message = "Dart";
tester.num = 2;
print(sayHello(tester));
}
C Code:
void sayHello(Dart_NativeArguments arguments) {
/*What do I do here to get back a pointe to the struct/class I passed
in as an argument in Dart?*/
}
Your Dart_NativeArguments will consist of just one item, which will be an instance - the instance of the class info that you created with new info(). You can test whether it's an instance with bool Dart_IsInstance(Dart_Handle object). So what you have is an handle to an instance of info. This allows you to access its instance fields (message and num) to get and set them, using Dart_GetField and Dart_SetField.
Dart_Handle instance = Dart_GetNativeArgument(arguments, 0);
Dart_Handle message_handle = Dart_GetField(retobj, NewString("message"));
char* message;
Dart_StringToCString(message_handle, &message);
Dart_Handle number_handle = Dart_GetField(retobj, NewString("num"));
int64_t number;
Dart_IntegerToInt64(number_handle, &number);
// message contains the string, number contains the number
// use them, copy them etc
I know this is just an example, but it might be easier to redefine sayHello to take 2 arguments (a string and an int) rather than passing an object instance. There isn't a way to access the fields of a class in one step, you need to access them individually. Consider these two versions of the Dart code, one passing an object instance and one just the values. The second version is simpler at the Dart and C side (no GetField steps). The first version is more powerful, though, because you could update the fields using SetField, which you couldn't in the second.
class Info {
String message;
int num;
Info(this.message, this.num);
}
version1() {
sayHelloV1(new Info('Dart', 2));
}
version2() {
sayHelloV2('Dart', 2);
}
If your C API requires you to pass in a struct you will have to create that in your C code by copying the values you extract using Dart_IntegerToInt64etc into it, then pass the pointer to your C struct to the API.
If your API is very precise about the packing/padding of the data into the struct, you could use Dart typed_data to pack the Dart types into a ByteData and pass the underlying byte array.

What does Cannot create delegate without target for instance method or closure mean

I am using vala.
This is the source code that gives that compile time bug :
private Gee.HashMap<string,VoidFunc> fill_actions()
{
var actions = new Gee.HashMap<string,VoidFunc>();
MainWindow win = window;
actions["t"] = () => _puts(win.title);
return actions;
}
First I tried to access this.window directly but that gave another error so I tried this with a local scope variable.
Error when doing directly this.window :
This access invalid outside of instance methods
It sounds like VoidFunc is declared with [CCode (has_target = false)]. What that means is that no context information is passed to it, and AFAIK that is the only way delegates work as generic type arguments. The reason for this is limitations in C, so assuming VoidFunc looks like this:
[CCode (has_target = false)]
public delegate void VoidFunc ();
What you'll get in C is something like this:
typedef void (*VoidFunc)();
As opposed to something like this if you didn't have the [CCode (has_target = false)]:
typedef void (*VoidFunc)(gpointer user_data);
When you pass around callbacks in C you generally do so with between one and three arguments. Something with all three would look like this:
void foo (VoidFunc void_func, gpointer user_data, GDestroyNotify notify);
The first parameter is the actual function. The second parameter is the value to pass as user_data to the callback, and is what Vala uses to pass context information to the callback (which is what allows it to act as an instance method, or even a closure). The third parameter is used to specify a function to free user_data when it is no longer needed.
What [CCode (has_target = false)] means is that the delegate doesn't have a user_data argument, and therefore cannot be used as a closure or instance method.
The reason this is necessary with a generic argument is that generics look something like this at the C level:
void foo_bar (gpointer data, GDestroyNotify notify);
The first parameter is the data that you want to use as a generic value, the second is actually only added if the generic argument is owned (as it is in the case of the set methods in Gee), and is called with user_data as an argument when user_data is no longer needed.
As you can see, when trying to use a delegate as a generic, there is nowhere to put the user_data argument, which is why Vala only allows delegates without targets to be generic arguments.
The solution is basically to wrap the delegate in a class:
public delegate void VoidFunc ();
public class YourClass {
private class VoidFuncData {
public VoidFunc func;
public VoidFuncData (owned VoidFunc func) {
this.func = (owned) func;
}
}
private Gee.HashMap<string,VoidFuncData> fill_actions() {
var actions = new Gee.HashMap<string,VoidFuncData>();
string win = "win";
actions["t"] = new VoidFuncData (() => GLib.debug (win));
return actions;
}
}

Resources