Using Gee.Traversable.fold in Vala

I’m trying to use Gee.Traversable.fold to write a simple function that finds the length of the longest string in a set. Here’s an imperative version that works:

uint max_length (SortedSet<string> l) {
	uint maxlen = 0;
	foreach (string s in l)
		maxlen = uint.max (s.length, maxlen);
	return maxlen;
}

So, I tried writing:

uint max_length (SortedSet<string> l) {
	return l.fold ((s, maxlen) => {
			return uint.max (s.length, maxlen);
		}, 0U);
}

But valac says:

foo.vala:XX.31-XX.36: error: Argument 2: Cannot convert from `unowned A' to `uint'
			return uint.max (s.length, maxlen);
			                          ^^^^^^
foo.vala:XX.17-XX.3: error: Argument 1: Cannot convert from `A max_length._lambda4_ (owned string, owned A)' to `A Gee.FoldFunc<uint,string> (owned G, owned A)'
Compilation failed: 2 error(s), 0 warning(s)

I can only find the source code for Gee, and a few mailing list messages discussing its design, but no actual uses (I tried looking in the vala compiler sources, but it doesn’t seem to use FoldFunc; Geary has a similar definition, but doesn’t use it either).

1 Like

It turns out (thanks, @ricotz) that I was missing a type annotation: the call to l.fold needs to be “l.fold”.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.