Maybe. If I do that adding all abstract stuff everywhere, it's true that the compiler now is ok, but now it's a little uggly. Objects not concerned by some features have to integrate them, making all the child potentially very big due to stuff that are not of their concern.fxm wrote:About that, I am waiting the dkl's response but I think that none object (static, dynamic and temporary) should be authorized to be created from a Type that does not implement all abstract methods.
For instance MinValue is a property for the integercap type, why should it be introduced in singlecap, zstringcap, and everything you can imagine if I extend the things more. If it's really necessary, there should be a keyword to mean fake implementation to avoid the real fake implementation effort and make the compiler understand why (maybe VIRTUAL FAKE or anything, I don't know how this issue is handled in other languages).
Code: Select all
'*root* -> TYPECAP
'integer -> INTEGERCAP
'single -> SINGLECAP
'zstring_ptr -> ZSTRINGPTRCAP
type TYPECAP extends OBJECT
declare abstract operator cast() as integer
declare abstract operator cast() as single
declare abstract operator cast() as string
declare abstract property MinValue() as integer
declare abstract property MaxValue() as integer
declare abstract property ClosestToZeroValue() as single
declare abstract property ClosestToInfValue() as single
declare abstract function GetType() as string
declare abstract function ToString() as string
end type
type VARIANT as TYPECAP ptr
type INTEGERCAP extends TYPECAP
declare constructor()
declare constructor(byval I as integer)
declare virtual property MinValue() as integer override
declare virtual property MaxValue() as integer override
declare virtual property ClosestToZeroValue() as single override
declare virtual property ClosestToInfValue() as single override
declare operator let(byref as integer)
declare virtual operator cast() as integer override
declare virtual operator cast() as single override
declare virtual operator cast() as string override
declare virtual function GetType() as string override
declare virtual function ToString() as string override
as integer _value
end type
constructor INTEGERCAP()
BASE()
end constructor
constructor INTEGERCAP(byval I as integer)
BASE()
THIS._value => I
end constructor
property INTEGERCAP.MinValue() as integer
#if __FB_64BIT__
dim as integer min => &H8000000000000000
#else
dim as integer min => &H80000000
#endif
'---->
return min
end property
property INTEGERCAP.MaxValue() as integer
#if __FB_64BIT__
dim as integer max => &H7FFFFFFFFFFFFFFF
#else
dim as integer max => &H7FFFFFFF
#endif
'---->
return max
end property
property INTEGERCAP.ClosestToZeroValue() as single
end property
property INTEGERCAP.ClosestToInfValue() as single
end property
operator INTEGERCAP.let(byref LetValue as integer)
THIS._value = LetValue
end operator
operator INTEGERCAP.cast() as integer
'---->
return THIS._value
end operator
operator INTEGERCAP.cast() as single
'---->
return THIS._value
end operator
operator INTEGERCAP.cast() as string
'---->
return Str(THIS._value)
end operator
function INTEGERCAP.GetType() as string
'---->
return "integercap"
end function
function INTEGERCAP.ToString() as string
'---->
return str(THIS._value)
end function
type SINGLECAP extends TYPECAP
declare constructor()
declare constructor(byval S as single)
declare virtual property MinValue() as integer override
declare virtual property MaxValue() as integer override
declare virtual property ClosestToZeroValue() as single override
declare virtual property ClosestToInfValue() as single override
declare operator let(byref as single)
declare virtual operator cast() as integer override
declare virtual operator cast() as single override
declare virtual operator cast() as string override
declare virtual function GetType() as string override
declare virtual function ToString() as string override
as single _value
end type
constructor SINGLECAP()
BASE()
end constructor
constructor SINGLECAP(byval S as single)
BASE()
THIS._value => S
end constructor
property SINGLECAP.MinValue() as integer
end property
property SINGLECAP.MaxValue() as integer
end property
property SINGLECAP.ClosestToZeroValue() as single
'---->
return 1.401298e-45
end property
property SINGLECAP.ClosestToInfValue() as single
'---->
return 3.402823e+38
end property
operator SINGLECAP.let(byref LetValue as single)
THIS._value = LetValue
end operator
operator SINGLECAP.cast() as integer
'---->
return THIS._value
end operator
operator SINGLECAP.cast() as single
'---->
return THIS._value
end operator
operator SINGLECAP.cast() as string
'---->
return Str(THIS._value)
end operator
function SINGLECAP.GetType() as string
'---->
return "singlecap"
end function
function SINGLECAP.ToString() as string
'---->
return str(THIS._value)
end function
type ZSTRINGPTRCAP extends TYPECAP
declare constructor()
declare constructor(byval Z as zstring ptr)
declare operator let(byref as zstring ptr)
declare virtual property MinValue() as integer override
declare virtual property MaxValue() as integer override
declare virtual property ClosestToZeroValue() as single override
declare virtual property ClosestToInfValue() as single override
declare virtual operator cast() as integer override
declare virtual operator cast() as single override
declare virtual operator cast() as string override
declare virtual function GetType() as string override
declare virtual function ToString() as string override
as zstring ptr _value
end type
constructor ZSTRINGPTRCAP()
BASE()
end constructor
constructor ZSTRINGPTRCAP(byval Z as zstring ptr)
BASE()
THIS._value => Z
end constructor
property ZSTRINGPTRCAP.MinValue() as integer
end property
property ZSTRINGPTRCAP.MaxValue() as integer
end property
property ZSTRINGPTRCAP.ClosestToZeroValue() as single
end property
property ZSTRINGPTRCAP.ClosestToInfValue() as single
end property
operator ZSTRINGPTRCAP.let(byref LetValue as zstring ptr)
THIS._value = LetValue
end operator
operator ZSTRINGPTRCAP.cast() as integer
'---->
return Val(*THIS._value)
end operator
operator ZSTRINGPTRCAP.cast() as single
'---->
return Val(*THIS._value)
end operator
operator ZSTRINGPTRCAP.cast() as string
'---->
return *THIS._value
end operator
function ZSTRINGPTRCAP.GetType() as string
'---->
return "zstringptrcap"
end function
function ZSTRINGPTRCAP.ToString() as string
'---->
return *THIS._value
end function
'````````````````````````````````````````````````````````````````````
'define an array of variant
dim as VARIANT arrayOfVariant(any)
'fill the array
redim arrayOfVariant(1 to 3)
arrayOfVariant(1) = new INTEGERCAP(99)
arrayOfVariant(2) = new SINGLECAP(3.1415)
arrayOfVariant(3) = new ZSTRINGPTRCAP("hello_variant")
'now you can get back the type of each variable stored as well as its value
? 1, arrayOfVariant(1)->GetType(), *arrayOfVariant(1)
? 2, arrayOfVariant(2)->GetType(), *arrayOfVariant(2)
? 3, arrayOfVariant(3)->GetType(), *arrayOfVariant(3)
?
'some operations
? "? 1+2+3", *arrayOfVariant(1) + *arrayOfVariant(2) + *arrayOfVariant(3)
? "? 1&2&3", *arrayOfVariant(1) & *arrayOfVariant(2) & *arrayOfVariant(3)
?
'other operations
? *arrayOfVariant(1) + *arrayOfVariant(2)
? *arrayOfVariant(1) & *arrayOfVariant(2)
? *arrayOfVariant(3)
?
'ToString method
? (type<SINGLECAP>(*arrayOfVariant(2) + *arrayOfVariant(1))).ToString()
? (SINGLECAP(*arrayOfVariant(2) + *arrayOfVariant(1))).ToString()
? arrayOfVariant(2)->ToString()
? arrayOfVariant(3)->ToString()
? arrayOfVariant(2)->ClosestToZeroValue
'````````````````````````````````````````````````````````````````````
getKey()
'(eof)