Vala: parameter hints - automatic check of the zero value of the parameter value-type out

Problem:

I do have problems using the seemingly nice feature advertised on the Vala / Tutorial site regarding the declaration of output parameters.

Here's a quote from: https://live.gnome.org/Vala/Tutorial#Parameter_Directions

Here is an example of implementation of method_1 ():

void method_1(int a, out int b, ref int c) {
    b = a + c;
    c = 3; }

      

By setting a value to out argument "b", Vala will ensure that "b" is nonzero. This way, you can safely pass null as the second argument to method_1 (), if that doesn't interest you.

Looking at the generated C code, it seems clear that unlike the statements above, instead of doing a pre-assignment check, a possible NULL pointer is happily played out.

void method_1 (gint a, gint* b, gint* c) {
    *b = a + (*c);
    *c = 3;
}

      

The code I was expecting would take this form instead:

void method_1 (gint a, gint* b, gint* c) {
    if (b != NULL)
      *b = a + (*c);
    *c = 3;
}

      

The only workaround I've found so far is to drop the entire parameter declaration and go directly with the pointer.

void method_1 (gint a, int* b, ref int c) {
    if (b != null)
      *b = a + c;
    c = 3;
}

      

For aesthetic reasons, I really liked the idea mentioned in the quote above, so I want to ask the following

QUESTIONS:

  • I misinterpreted the description in the above quote and and the value-type declared parameters cannot be automatically checked for null pointers?

  • Or is it probably a bug in the vala compiler, which is fixed in a later version? valac --version

    returns Vala 0.8.1

    , so this is the version I'm using.

  • Perhaps there is another chance to perform the parameter type declaration manually with any language element that I am still missing?

+3


source to share


1 answer


Vala 0.8.1 is your problem. Shaft 0.16 is produced



void method_1 (gint a, gint* b, gint* c) {
  gint _vala_b = 0;
  gint _tmp0_;
  gint _tmp1_;
  _tmp0_ = a;
  _tmp1_ = *c;
  _vala_b = _tmp0_ + _tmp1_;
  *c = 3;
  if (b) {
    *b = _vala_b;
  }
}

      

+5


source







All Articles