Results 1 to 40 of 61

Thread: [RESOLVED] Do not use GOTO

Threaded View

  1. #24
    Lively Member
    Join Date
    Sep 2013
    Posts
    117

    Re: Do not use GOTO

    Restricts implicit data type conversions to only widening conversions, disallows late binding, and disallows implicit typing that results in an Object type.
    Why assume Photoshop?

    Where does it state that the "comparison" operators must return Boolean? Is possible they can return something else?

    Try it out. Doesn't take two minutes to discover, that this assumption is incorrect.
    Code:
    Public Class Eg
      Shared Operator < (ByVal a As Integer, b As Integer) As Eg
    
      End Operator
    End Class
    So if operators are defined by the type that implements them.
    And the a comparison operators aren't activated via IComparable(Of T) interface cos interfaces can't implement static / shared methods.

    So you could (and can) define an object (or as in my implementation multiple objects) that lifts the value into a different type where you can implement operators that does bounds checking.


    Seeing that you don't believe me here that is possible. Here is the code, that does it.
    Code:
    Public Class PartA(Of T As IComparable(Of T))
      Private _Value As T
      Public Sub New(Value As T) 
        _Value = Value 
      End Sub
      Public ReadOnly Property Value As T
        Get
          Return _Value 
        End Get
      End Property
    
      Public Shared Widening Operator CType(ByVal Value As T) As PartA(OF T)
        Return New PartA(Of T)(Value)
      End Operator
    
      Public Shared Operator <(ByVal P As PartA(of T) , Value As T) As PartB(OF T)
        Return New PartB(Of T)( P.Value.CompareTo(Value)<0, Value)
      End Operator
        Public Shared Operator <=(ByVal P As PartA(of T) , Value As T) As PartB(OF T)
        Return New PartB(Of T)( P.Value.CompareTo(Value)<=0, Value)
      End Operator
      <Obsolete("Not Used",True)>
        Public Shared Operator >(ByVal P As PartA(of T) , Value As T) As PartB(OF T)
        Throw  New NotImplementedException()
      End Operator
        <Obsolete("Not Used",True)>
        Public Shared Operator >=(ByVal P As PartA(of T) , Value As T) As PartB(OF T)
        Throw  New NotImplementedException()
      End Operator
    End Class
    
    Public Class PartB(Of T As IComparable(Of T))
      Private _CMP As Boolean
      Private _Value As T
      Public Sub New(CmpState As Boolean, Value As T)
        _CMP =CmpState 
        _Value = Value
      End Sub
      Public ReadOnly Property Value As T
        Get
          Return _Value 
        End Get
      End Property
      Public ReadOnly Property State As Boolean
        Get
          Return _CMP 
        End Get
      End Property
    
        Public Shared Operator <(ByVal P As PartB(of T) , Value As T) As Boolean
        Return P.State AndAlso ( P.Value.CompareTo(Value)<0)
      End Operator
        Public Shared Operator <=(ByVal P As PartB(of T) , Value As T) As Boolean
        Return P.State AndAlso ( P.Value.CompareTo(Value)<=0)
      End Operator
      <Obsolete("Not Used",True)>
        Public Shared Operator >(ByVal P As PartB(of T) , Value As T) As Boolean
        Throw  New NotImplementedException()
      End Operator
        <Obsolete("Not Used",True)>
        Public Shared Operator >=(ByVal P As PartB(of T) , Value As T) As Boolean
        Throw  New NotImplementedException()
      End Operator
      
    End Class
    I use the extension method .__ (I would use ._ but that's not a valid identifier for a method), that does the initial lifting of the type. Since it less visually intrusive than then the implicit conversion operator.

    Code:
    Public Module Exts
      <Runtime.CompilerServices.Extension>
      Public Function __(Of T As IComparable(Of T))(Value As T) As PartA(Of T)
        Return New PartA(Of T)(Value)
      End Function
    End Module
    Took about 15-20 minute to implement as well.


    I await the apologies.
    Last edited by AdamPanic2013; Sep 2nd, 2013 at 01:55 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width