How to create a nil spread of an object in VL?

Hi VLVLVLVLers

I have a quick question, how do you create a nil spread in VL for a custom datatype?
I have a simple sift function for 'MyObject’s but I want to return an empty spread (I.E no objects) if the sift fails.

In the image I would connect my empty spread to the third pin on the switch at the bottom.

Empty [Spread]? but this node is theoretical, the default of the pin should already be an empty spread.

I can’t connect Empty [Spread] to the switch.
If I leave the switch unconnected (or connect an iobox to it) the output will be a single slice at default values.

ok i see, unlike vvvv, in VL a single object is something different than a spread with one object.

you can use ToSpread [Spread] to convert the single object coming out of the ForEach into a spread with one entry. then both inputs of the Switch can have the same type.

Ok it took me a little while but I think I understand now. (I’m having trouble letting go of vvvv ideas…)

As you say in VL a single instance of an object is not the same as a spread with 1 slice of that object.

Is it the case that:
With a spread of objects we can have a spread of [0] objects, (known in vvvv as NIL)
However a single object is exactly that, always exactly one object in the current context. If some logic leaves the single object undefined VL will create a single object with default values.
?

If this is true does it follow that if you convert a VL spread with 0 slices into a single object (eg with SplitFirst or GetSlice node) then because you’ve explicitly said you want one object you’ll get a new single object that has default values? Instead of some kind of null object? It seems to be from my testing.
And the same with a single object type switch that has nothing on one of the pins? The empty pin will contain a default singleobject on those pins instead of an ‘empty’ slice?

So then a ‘ToSpread [Spread]’ solution will work but I must ensure that down the graph I pass that as a spread (not a single object) to my next operation. I can’t convert the spread back to a single object (even if it’s a [0] spread) or I will end up with one new default object instead of no objects.
If your next operation only accepts single objects than you will need to enclose that operation in a ForEach loop so that it can accept a spread. This is the solution that worked for me.

In case anyone else has the same trouble as me understanding VL and spreads/slices I did a lot of experiments and made a sort of tutorial patch so I could understand it, which is attached.

PatchesVLUnderstandingSpreads.zip (39.9 KB)

that is correct. and yes, VL will try to generate a default value.[quote=“tobyk, post:5, topic:14746”]
if you convert a VL spread with 0 slices into a single object (eg with SplitFirst or GetSlice node) then because you’ve explicitly said you want one object you’ll get a new single object that has default values?
[/quote]

also correct. these nodes, like GetSlice, should all have a default value input for the case of inputting an empty spread. other nodes like GetItem will throw an “Index out of Bounds” error as if you would try to access element 11 from a spread of 10 values. note that GetSlice does a modulo and gives you element 1 when you input 11.

this is a more complex topic, it sounds like you are actually want to have a NIL or NULL object to check whether you have found a value or not. the solution with having a spread with 1 or 0 objects inside is not bad at all, since you can check the length to determine whether there is a value.

another solution would be to call the operations below only if you found an object using an IF region and connecting your ‘Match Found’ bool to it. this avoids the need of an encapsulating spread but is very similar to the ForEach region solution that you found in that it only executes parts of the patch if something was found.

so there are many ways to rome and the best is the one you find first.

1 Like

Thanks so much, that really cleared things up for me.

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