Tech Tip: Safe Pattern for Updating 4d.Vector Components
PRODUCT: 4D | VERSION: 21 | PLATFORM: Mac & Win
Published On: February 23, 2026
4D.Vector objects are immutable by design . However, real-world logic often requires updating individual values. This technical tip demonstrates the "Convert-Modify-Recreate" pattern using a utility method. It covers how to bypass the "Cannot use numeric properties" error, implements bounds-checking, and utilizes the Throw() command for error handling.
Method: Util_Vector_SetComponent
Updates a vector component safely. Throws an error if index is out of bounds.
Test Exemple
to trigger the out of bound error
$v:=Util_Vector_SetComponent($v; 3; 1,618)

Method: Util_Vector_SetComponent
Updates a vector component safely. Throws an error if index is out of bounds.
| #DECLARE($originalVector : 4D.Vector; $index : Integer; $newValue : Real)\ ->$resultVector : 4D.Vector If ($originalVector=Null) throw(20001; "The original vector is Null.") $resultVector:=Null return End if If ($index>=0) & ($index<$originalVector.length) var $tempCol : Collection $tempCol:=$originalVector.toCollection() $tempCol[$index]:=$newValue $resultVector:=4D.Vector.new($tempCol) Else var $errMsg : Text $errMsg:="Index "+String($index)+" is out of bounds. "+\ "Vector length is "+String($originalVector.length)+"." throw(20002; $errMsg) End if |
Test Exemple
| var $v: 4D.Vector $v:=4D.Vector.new([0; 0; 0]) // => $v[0]=0;$v[1]=0;$v[2]=0 $v:=Util_Vector_SetComponent($v; 0; 2) // $v[0]=2 $v:=Util_Vector_SetComponent($v; 1; 5.6)// $v[1]=5.6 $v:=Util_Vector_SetComponent($v; 2; 3.14)// $v[2]=3.14 |
to trigger the out of bound error
$v:=Util_Vector_SetComponent($v; 3; 1,618)
