Functions returning UDTs


#1

Based on what I’ve read elsewhere in the forum http://www.scidb.org/forum/viewtopic.php?f=15&t=490, it appears that functions can only return scalar values. So far whenever I’ve run into what appears to be a limiting design like this, I have subsequently understood that there is a good reason for it (i.e. great job designing a product that makes sense!). But this one I don’t entirely understand. Is there some reason to restrict functions to returning scalars? Why not allow them to return UDTs? I.e. why not allow them to return anything that fits in a single slot of an attribute?

I am in a situation where I need to apply() a fitting function, but I can only pull out one kind of result out of it for every call of apply(). So I have to do one apply() to get the fitting error, and one apply() for each fitted coefficient that I need to pull out. It seems logical to me to want to shove all the fitting results into a UDT and get them out of a single call to apply(). Just in case this was undocumented but possible, I did code it up. It compiled and loaded, but calling apply() on a function returning a UDT (which, by your definition, is not a function, but rather a “converter”, right?) failed.

Just trying to gain some insight here…
Thank you!


#2

Hello,

As far as I understand, returning “scalar only” does NOT mean you cannot return UDTs. As far as I know, you can absolutely return a UDT from a function. Take a look at examples/point/function.h. There is str2point that takes a string and returns a point UDT. If that didn’t exist, there’d be no way to load points from a text file… So let’s examine why your UDT failed. It could be you didn’t define a conversion to a string and so the system barfed when trying to output it. It could be something else…

What you can’t return from a function is any kind of of set or list. When we say “scalar” we mean “one thing” - even tho it could be a UDT Value which actually contains a encoded list of things…

Hope it helps.

  • Alex Poliakov

#3

Aah, that makes perfect sense. I had interpreted “scalar” in a numerical sense, not as the opposite of array, but as “a single value of a built-in numerical type”. Thank you for explaining it. Sure enough, once I added the string conversion everything works.
One more question, while we’re debugging my understanding of functions: are there any plans for allowing functions with a variable number of arguments, or is there a good reason not to do so?
Thanks!