Subscribe to The Madman Scribblings        RSS Feed
***-- 2 Votes

In-line Assignment Operator

Icon Leave Comment
In-line Assignment Operator

I admit that I'm not a fan of in-line assignment, as the symbol for the operator closely resembles the comparison operator for equality.
Which to me is easily confusing.

Since does allow assignment mid expression, due to symbol = meaning being contextual.

So I have tried to achieve the best I can, with the restriction on method names.
Public  Module Exts
  ''' <summary>
  ''' A psuedo-inline assignment operator.
  ''' </summary>
  ''' <typeparam name="T">The type being used</typeparam>
  ''' <param name="Target">The target for the assignment.</param>
  ''' <param name="Value">The value to be assigned.</param>
  ''' <returns>The value being assigned</returns>
  ''' <remarks></remarks>
  <Extension>  Public Function Ξ(Of T)(ByRef Target As T, Value As T) As T
    Target = Value : Return Value
  End Function

End Module

An example of the in-line assignment in use. With an implementation of get the first n numbers of the Fibonacci Sequence.
   Public Iterator Function FibN(ByVal n As Integer) As IEnumerable(Of Integer)
    If n < 0 then Throw New ArgumentOutOfRangeException("Must be greater equal to zero (0)")
    Dim Fib = {1, 0}
    If n > 0 Then Yield 0
    If n > 1 Then Yield 1
     While n > 2
      Yield Fib(n Mod 2).Ξ(Fib.Sum) ' The "assignment" '
      n -= 1
    End While 
  End Function

Note: VB11's Iterators

The result does look too bad (in my opinion).

0 Comments On This Entry