implement compare to own classes

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

implement compare to own classes

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

implement compare to own classes

Post 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>)
______________________
Johan Nel
Boshof, South Africa
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

implement compare to own classes

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

implement compare to own classes

Post 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
______________________
Johan Nel
Boshof, South Africa
FFF
Posts: 1581
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

implement compare to own classes

Post 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
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

implement compare to own classes

Post 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
______________________
Johan Nel
Boshof, South Africa
User avatar
Chris
Posts: 4915
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

implement compare to own classes

Post 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".
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

implement compare to own classes

Post 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,
______________________
Johan Nel
Boshof, South Africa
User avatar
robert
Posts: 4529
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

implement compare to own classes

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

implement compare to own classes

Post by wriedmann »

Hi Robert,

thank you very much!

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

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply