1

how to return an array of unowned strings that all point to the same location in memory?

example:

init
    var str = "ABC"
    var unowned_string_array = repeat (str, 5)

def repeat (s: string, n: int): array of string
    // code

and this array will contains 5 elements(same string "ABC"), all point to same location

Naam Famas
  • 124
  • 7
  • I have tried do solve this, but the syntax is tricky here. I have found a relevant mailing list post here: http://osdir.com/ml/vala-list/2009-12/msg00046.html – Jens Mühlenhoff Jul 13 '15 at 15:36

1 Answers1

2

The closest Vala code I could get is:

int main() {
    var str = "ABC";
    var unowned_string_array = repeat (str, 5);
    return 0;
}

public (unowned string)[] repeat (string s, int n) {
    var a = new (unowned string)[n];
    for (var i = 0; i < n; i++)
        // This sadly still duplicates the string,
        // even though a should be an array of unowned strings
        a[i] = s; 
    return a;
}

I'm not sure if the compiler understands the parenthesis here, it may think that I want to declare an unowned array of owned strings here ...

Update: It turns out the problem is that type inference will always create an owned variable (see nemequs comment).

There is even a bug report for this.

So this works just fine (no string duplication in the repeat function):

int main() {
    var str = "ABC";
    (unowned string)[] unowned_string_array = repeat (str, 5);
    return 0;
}

public (unowned string)[] repeat (string s, int n) {
    (unowned string)[] a = new (unowned string)[n];
    for (var i = 0; i < n; i++)
        // This sadly still duplicates the string,
        // even though a should be an array of unowned strings
        a[i] = s;
    return a;
}

Which would be something like this in Genie:

[indent=4]

init
    var str = "ABC"
    unowned_string_array: array of (unowned string) = repeat (str, 5)

def repeat (s: string, n: int): array of (unowned string)
    a: array of (unowned string) = new array of (unowned string)[n]
    for var i = 1 to n
        a[i] = s
    return a

The Genie code has the additional problem of not compiling, due to the parser not being able to deduce what comes after the array of.

This seems to be a similar problem to what I already had with nested generic types.

I have reported this a Genie bug.

Community
  • 1
  • 1
Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
  • 2
    By default all variables are owned—when you use type inferencing you are creating a new *owned* variable, even if the right hand side is unowned. If you do `(unowned string)[] a = new (unowned string)[n];` you can avoid the `g_strdup`. No idea about genie, though, sorry. owned arrays of unowned values are a newish (a couple years old, IIRC) feature in Vala, I wouldn't be surprised if Genie hasn't been updated. – nemequ Jul 13 '15 at 16:58
  • So should we file a bug report for this on the vala compiler? – Jens Mühlenhoff Jul 13 '15 at 22:06
  • It wouldn't hurt. TBH I'm not familiar enough with genie to tell you definitively that there isn't a way to do what you're asking, but it seems likely that would be the syntax… – nemequ Jul 14 '15 at 04:00