xsharp.eu • implement compare to own classes
Page 1 of 1

implement compare to own classes

Posted: Mon Jul 29, 2019 9:14 am
by wriedmann
Hi Chris,
I have implemented my own date class to be used in applications written in Core dialect.
How can I implement the compare operators >, <, !=?
Wolfgang

implement compare to own classes

Posted: Mon Jul 29, 2019 9:42 am
by lumberjack
Hi Wolfgang,
wriedmann wrote: I have implemented my own date class to be used in applications written in Core dialect.
How can I implement the compare operators >, <, !=?

Code: Select all

PUBLIC [STATIC] OPERATOR +(var1 AS <Type>, var2 AS <Type>) AS <ReturnType>
RETURN <Method/Function Name>(<var1>, <var2>)

implement compare to own classes

Posted: Mon Jul 29, 2019 10:36 am
by wriedmann
Hi Johan,

thanks, it works now.

This is my class:

Code: Select all

class XbDate
	protect _oDatum as Nullable<DateTime>

constructor()
	_oDatum := null
	return

constructor( oDatum as Nullable<DateTime> )
	_oDatum := oDatum
	return

constructor( nYear as int, nMonth as int, nDay as int )
    _oDatum := null
    try
    _oDatum := DateTime{ nYear, nMonth, nDay }
    end try
    return

constructor( cDateString as string )
	local nYear as int
	local nMonth as int
	local nDay as int
	_oDatum := null
	try
	nYear := 0
	nMonth := 0
	nDay:= 0
	if Int32.TryParse( cDateString:Substring( 0, 4 ), ref nYear ) .and. ;
	   Int32.TryParse( cDateString:Substring( 4, 2 ), ref nMonth ) .and. ;
	   Int32.TryParse( cDateString:Substring( 6, 2 ), ref nDay )
		_oDatum := DateTime{ nYear, nMonth, nDay }
	endif
	end try
	return

property DateTime as Nullable<DateTime> get _oDatum
property DateString as string
	get
		if _oDatum == null
			return ""
		endif
		return ( ( DateTime ) _oDatum ):ToString("yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture )
	end get
end property
property Year as int
	get
		if _oDatum == null
			return 0
		endif
		return ( ( DateTime ) _oDatum ):Year
	end get
end property
property Month as int
	get
		if _oDatum == null
			return 0
		endif
		return ( ( DateTime ) _oDatum ):Month
	end get
end property
property Day as int
	get
		if _oDatum == null
			return 0
		endif
		return ( ( DateTime ) _oDatum ):Day
	end get
end property

public static operator >( oDate1 as XbDate, oDate2 as XbDate ) as logic
	return oDate1:DateTime > oDate2:DateTime

public static operator <( oDate1 as XbDate, oDate2 as XbDate ) as logic
	return oDate1:DateTime > oDate2:DateTime

public static operator <=( oDate1 as XbDate, oDate2 as XbDate ) as logic
	return oDate1:DateTime <= oDate2:DateTime

public static operator >=( oDate1 as XbDate, oDate2 as XbDate ) as logic
	return oDate1:DateTime >= oDate2:DateTime

public static operator !=( oDate1 as XbDate, oDate2 as XbDate ) as logic
	return oDate1:DateTime != oDate2:DateTime

public static operator ==( oDate1 as XbDate, oDate2 as XbDate ) as logic
	return oDate1:DateTime == oDate2:DateTime

method GetHashCode() as int
	return _oDatum:GetHashCode()

method Equals( oDate as object ) as logic
	local lReturn as logic

	if oDate is XbDate
		lReturn := ( ( ( XbDate ) oDate ):DateTime == self:DateTime )
	else
		lReturn := false
	endif
	return lReturn

end class
Wolfgang

implement compare to own classes

Posted: Mon Jul 29, 2019 11:17 am
by lumberjack
Hi Wolfgang,
wriedmann wrote:thanks, it works now.

Code: Select all

class XbDate
	protect _oDatum as Nullable<DateTime>
public static operator >( oDate1 as XbDate, oDate2 as XbDate ) as logic
Glad you got it working.
@DevTeam: Maybe one of you guys can enlighten us when to use "STATIC" and in which cases one would not declare it a static operator?

Another more theoretical question:
Is there a way to have an operator, lets say between dates "<=>" which is inclusive of start and end date, or exclusive "<->" e.g.:

Code: Select all

IF oDate <=> dtStart, dtEnd // oDate >= dtStart .AND. oDate <= dtEnd

implement compare to own classes

Posted: Mon Jul 29, 2019 12:36 pm
by FFF
lumberjack wrote:Another more theoretical question:
Is there a way to have an operator, lets say between dates "<=>" which is inclusive of start and end date, or exclusive "<->" e.g.:

Code: Select all

IF oDate <=> dtStart, dtEnd // oDate >= dtStart .AND. oDate <= dtEnd
Not sure i'm with you re "new" operators, at least, when there meaning is not intuitively understandable.
So, if "<=>" means both sides inclusive, i'd expect exclusive to be ">=<", and naturally the "<=<" and ">=>" variants ;-)

(Interisting, years ago i ventured a shy suggestion in PG, which uses similar schemes using [], for a [=[ instead of [=) (IIRC...) - got shot down...)

Karl

implement compare to own classes

Posted: Mon Jul 29, 2019 12:51 pm
by lumberjack
Hi Karl,
FFF wrote:
lumberjack wrote:Another more theoretical question:
Is there a way to have an operator, lets say between dates "<=>" which is inclusive of start and end date, or exclusive "<->" e.g.:

Code: Select all

IF oDate <=> dtStart, dtEnd // oDate >= dtStart .AND. oDate <= dtEnd
Not sure i'm with you re "new" operators, at least, when there meaning is not intuitively understandable.
So, if "<=>" means both sides inclusive, i'd expect exclusive to be ">=<", and naturally the "<=<" and ">=>" variants ;-)

(Interisting, years ago i ventured a shy suggestion in PG, which uses similar schemes using [], for a [=[ instead of [=) (IIRC...) - got shot down...)
Well it was due to PG's PostGIS operators that I asked the question. See this PostGISOperators

implement compare to own classes

Posted: Mon Jul 29, 2019 6:51 pm
by Chris
lumberjack wrote:Hi Wolfgang,
wriedmann wrote:thanks, it works now.

Code: Select all

class XbDate
	protect _oDatum as Nullable<DateTime>
public static operator >( oDate1 as XbDate, oDate2 as XbDate ) as logic
Glad you got it working.
@DevTeam: Maybe one of you guys can enlighten us when to use "STATIC" and in which cases one would not declare it a static operator?
Operator methods are always STATIC, so if you do not explicitly include it, the X# compiler always adds it itself anyway.
lumberjack wrote: Another more theoretical question:
Is there a way to have an operator, lets say between dates "<=>" which is inclusive of start and end date, or exclusive "<->" e.g.:

Code: Select all

IF oDate <=> dtStart, dtEnd // oDate >= dtStart .AND. oDate <= dtEnd
That cannot be done at the user level, because the set of the operators that can be redefined in code is standard. But anything can be implemented at the compiler level, like for example the different = and == operators, which is unique in X# (and xBase languages in general), but in .Net there's only one equality operator.

I think it would be nice having a "between" operator, will add this as an enhancement request, but hope you understand it will not take high priority tight now. Btw, maybe it would be possible to use the already existing $ operator (which is also xBase specific and cannot be overloaded) to cover also "in between" in addition to "inside string".

implement compare to own classes

Posted: Tue Jul 30, 2019 3:04 am
by lumberjack
Hi Chris,
Thanks for your answer.
Chris wrote: I think it would be nice having a "between" operator, will add this as an enhancement request, but hope you understand it will not take high priority tight now. Btw, maybe it would be possible to use the already existing $ operator (which is also xBase specific and cannot be overloaded) to cover also "in between" in addition to "inside string".
No worries, was just wondering if one can define your own "special" operators, as is possible in PostgreSQL. No worries to implement if not part of .NET.

Regards,

implement compare to own classes

Posted: Mon Aug 05, 2019 12:34 pm
by robert
Wolfgang,
The best place to look for the syntax of this kind of things is the source of the X# runtime on Github.
For example the source to the date type is in:
https://github.com/X-Sharp/XSharpPublic ... s/Date.prg

Robert

implement compare to own classes

Posted: Mon Aug 05, 2019 2:28 pm
by wriedmann
Hi Robert,

thank you very much!

Sometimes I have problems to find the right place on GitHub where to look.

Wolfgang