Repository navigation
Open
Labels
Milestone
Description
Originally proposed by @shoooe in npgsql/efcore.pg#785
Motivation
As @shoooe already pointed out, the current API isn't friendly. This proposal is trying to solve it and investigate more natural way to create ranges.
Proposal
public readonly struct NpgsqlRange<T> : IEquatable<NpgsqlRange<T>>
where T : IEquatable<T>, IComparable<T>
{
// Creates a range with an exclusive upper bound
public NpgsqlRange(T lowerBound, T upperBound);
// Inclusivity is specified by bounds
public NpgsqlRange(NpgsqlRangeBound<T> lowerBound, NpgsqlRangeBound<T> upperBound);
public NpgsqlRangeBound<T> LowerBound { get; }
public NpgsqlRangeBound<T> UpperBound { get; }
public bool IsEmpty { get; }
public bool IsCanonical { get; }
public NpgsqlRange<T> Canonicalize();
public bool Equals(object other);
public bool Equals(NpgsqlRange<T> other);
public int GetHashCode();
}
public readonly struct NpgsqlRangeBound<T> : IEquatable<NpgsqlRangeBound<T>>, IComparable<NpgsqlRangeBound<T>>
where T : IEquatable<T>, IComparable<T>
{
public NpgsqlRangeBound(T value);
public NpgsqlRangeBound(T value, bool isInclusive);
public bool IsInclusive { get; }
public bool IsInfinite { get; }
public T Value { get; }
public bool Equals(object other);
public bool Equals(NpgsqlRangeBound<T> other);
public int CompareTo(NpgsqlRangeBound<T> other);
public int GetHashCode();
public static bool operator ==(NpgsqlRangeBound<T> left, NpgsqlRangeBound<T> right);
public static bool operator !=(NpgsqlRangeBound<T> left, NpgsqlRangeBound<T> right);
public static bool operator <(NpgsqlRangeBound<T> left, NpgsqlRangeBound<T> right);
public static bool operator >(NpgsqlRangeBound<T> left, NpgsqlRangeBound<T> right);
public static implicit operator T?(NpgsqlRangeBound<T value);
}
public static class NpgsqlRangeBound
{
public static NpgsqlRangeBound<T> Exclusive(T value)
where T : IEquatable<T>, IComparable<T>;
public static NpgsqlRangeBound<T> Inclusive(T value)
where T : IEquatable<T>, IComparable<T>;
public static NpgsqlRangeBound<T> Infinite()
where T : IEquatable<T>, IComparable<T>;
}
Activity
austindrenski commentedon Feb 5, 2019
IIRC we can't constrain
Tin this way, since the subtypes of some known ranges lack theIComparable<T>.austindrenski commentedon Feb 5, 2019
Some more thoughts:
khellang commentedon Feb 5, 2019
Why not make the bounds themselves nullable? Why force lower and upper bounds only to store a field to tell whether it's infinite or not?
A missing bound means it's infinite. Example:
By modeling the type like this, you'll prevent error conditions like the following:
npgsql/src/Npgsql/NpgsqlTypes/NpgsqlRange.cs
Lines 510 to 518 in 064fa94
It's common to have non-generic static classes with generic factory methods to allow C# type inference, i.e.
khellang commentedon Feb 5, 2019
There's no need to constrain
TonIEquatable<T>either, as you can implement equality and hash code usingEqualityComparer<T>.Default, which is now a JIT intrinsic (https://github.com/dotnet/coreclr/issues/6688) 😄austindrenski commentedon Feb 5, 2019
Oops, that's right. Wasn't thinking about the associated noise of static methods on a generic class!
austindrenski commentedon Feb 6, 2019
Sigh, that would be the silver bullet.... but some range subtypes are reference types, and IIRC there's a compilation error associated with mixing nullables/generics.
(I did some work on this last time we updated these types, and ran into the above problem. I'll link the relevant issues/PRs when I'm off mobile.)
khellang commentedon Feb 6, 2019
NpgsqlRangeBound<T>is a struct, structs can be wrapped inNullable<T>. I'm not saying we should makeTnullable (which would force us to constrain it tostructand thus excluding reference types). WhetherTis a reference type or not, doesn't really matter 😄There's nothing stopping you from doing
austindrenski commentedon Feb 6, 2019
Oh, that's a great point (and possibly the best argument in favor of this proposal so far).
YohDeadfall commentedon Feb 6, 2019
@austindrenski @khellang What types do lack of comparison interfaces?
As @khellang said, it is for type inference, but it's not the only reason for that. Another reason is
using staticdirective which accepts only non-generic types or closed generics. So you can do something like in the example below:Memory footprint (triple size of
IntPtrinstead of two) and an unusable way to check that the bound is inclusive or exclusive, but the last one is minor. It could be fixed by extension methods onNpgsqlRangeBound<T>.But making bounds nullable will provide a PostgeSQL like way to create ranges. It's a pro of nullability.
khellang commentedon Feb 6, 2019
Unusual, maybe. But it reflects the problem you're trying to model perfectly. If it doesn't have a lower bound, there's no point in checking if it's inclusive or not.
roji commentedon Feb 6, 2019
Take a look at dotnet/csharplang#2194. Now that nullable references are coming, it makes sense for C# to allow you do do T? without constraining to value types or references types. However, the discussion is ongoing and it's very unclear whether this will make it in. It might be a good idea to postpone this discussion a little bit until that's clarified.
roji commentedon Feb 6, 2019
Some other notes...
At least as far as the check is concerned, that makes a lot of sense to me - you can't check if a bound is inclusive or exclusive if there's no bound (i.e. it's infinity). So it seems OK to ask users to do
bound?.IsInclusiveand handle the null case. However, I'm still hoping we'll be able to make T itself nullable, rather than the bound (see previous comment).How do you propose to handle inclusivity? It seems like we should default to lower bounds being inclusive and upper ones being exclusive (seems pretty standard). Since we don't want to force the user to specify inclusivity all the time, this may mean that the bound's inclusivity should be nullable; if it's unspecified, the range will automatically treat it as inclusive for lower, and exclusive for upper. This does complicate the idea of having
IsInclusive(orIsInclusive()), because there's no good answer for the null case - but that seems OK to me. What do you guys think?Also, I think we should definitely keep some simpler constructor on the range itself, which don't involve NpgsqlRangeBound. We want the user to just be able to specify values, and I really don't think we should go into implicit cast operators - those can be quite dangerous.
YohDeadfall commentedon Feb 6, 2019
Good point, @khellang, but it's possible to incapsulate nullability check logic into the
NpgsqlRangeBound<T>, so it will throw an exception if there's no value in it. Otherwise, it will return it. Even more if Microsofties implement the proposal @roji mentioned, we could ad an implicit cast toT?.By the way, PostgreSQL allows you to specify inclusive null bounds even if it means the same as exclusive.
khellang commentedon Feb 6, 2019
@YohDeadfall Yeah, that's essentially what
Nullable<T>does. I guess that could be baked into theNpgsqlRangeBound<T>type, but if you want to prevent an exception being thrown, instead of writingYou'd have to write
I'm not seeing the gain.
Nullable<T>is a tried and true concept, well understood by .NET developers. It even has language support 😄@roji I'm not a big fan of making
IsInclusivenullable, because theHasValueand default value propagation everywhere.I'm thinking you could work around the verboseness of forcing the user to consider inclusivity by adding a value ctor to
NpgsqlRange<T>with the default (inclusive/exclusive) behavior:You could also add factory methods for ranges with other characteristics:
(You could even add a
using staticforNpgsqlRangeBoundto shorten the code even more)8 remaining items
shoooe commentedon Feb 7, 2019
Oh dear, I'm having a deja-vù.
I just want to crosspost that one problem with the following:
is that of constructing a range from one of the bounds of another range.
Comparing:
with:
To be fair, with 3 values it was way worse. The latter is not as readable as the one above, but acceptable, I guess.
YohDeadfall commentedon Feb 24, 2019
Opened dotnet/corefx#35420 to add the range type to the base class library.
ghuntley commentedon Mar 19, 2019
Discussed at https://youtu.be/vHqgsQ23GRg