www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: [GSOC] Database API

reply Kagamin <spam here.lot> writes:
Christian Manning Wrote:

 On 25/03/2011 14:56, Piotr Szturmaj wrote:
 To sum up, Phobos need some additional changes for database support,
 mainly addition of Nullable.

For now, couldn't this just be included with the database module?

Why not just use Variant? It should be able to hold anything, so Nullable should be not needed.
Mar 26 2011
parent reply Piotr Szturmaj <bncrbme jadamspam.pl> writes:
Kagamin wrote:
 Christian Manning Wrote:

 On 25/03/2011 14:56, Piotr Szturmaj wrote:
 To sum up, Phobos need some additional changes for database support,
 mainly addition of Nullable.

For now, couldn't this just be included with the database module?


I think it should, for the same reason. Many times you want more constrained data type than Variant. Say, some interface expect int or null. Nullable!int is perfect for it. But if you use Variant, you may end up having some struct inside or maybe some string, like 'abc' :) In other words, base type of Nullable (for example int) is enforceable at compile time, unlike Variant. Anyway my probosal is compatible with Variant. Variant is an alias for backend type VariantN. Quote from std.variant: "VariantN is a back-end type seldom used directly by user code. Two commonly-used types using VariantN as back-end are: Algebraic: A closed discriminated union with a limited type universe (e.g., Algebraic!(int, double, string) only accepts these three types and rejects anything else). Variant: An open discriminated union allowing an unbounded set of types. The restriction is that the size of the stored type cannot be larger than the largest built-in type. This means that Variant can accommodate all primitive types and all user-defined types except for large structs. Both Algebraic and Variant share VariantN's interface. (See their respective documentations below.)" I propose that, Nullable should be an alias (or subtype) of Algebraic, for example: template Nullable(T) { alias Algebraic!(T, void*) Nullable; } This way it will share the same backend as Variant, so both types will be compatible.
Mar 26 2011
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 3/26/11 2:11 PM, Piotr Szturmaj wrote:
 I propose that, Nullable should be an alias (or subtype) of Algebraic,
 for example:

 template Nullable(T)
 {
 alias Algebraic!(T, void*) Nullable;
 }

Should be Algebraic!(T, void). If that doesn't currently compile, we should make it compile. Andrei
Mar 26 2011
next sibling parent reply Kagamin <spam here.lot> writes:
Robert Jacques Wrote:

 Should be Algebraic!(T, void). If that doesn't currently compile, we  
 should make it compile.

 Andrei

For what it's worth, both Algebraic!(T, void*) and Algebraic!(T, void) compile on my branch of std.variant.

can't assign null alias Algebraic!(int,void) nint; nint n1=5; assert(n1==5); n1=null; \std\variant.d:497: Error: static assert "Cannot store a void* in a VariantN!(maxSize,int,void). Valid types are (int, void)" test.d:45: instantiated from here: opAssign!(void*)
Mar 26 2011
next sibling parent Piotr Szturmaj <bncrbme jadamspam.pl> writes:
Kagamin wrote:
 Robert Jacques Wrote:

 Should be Algebraic!(T, void). If that doesn't currently compile, we
 should make it compile.

 Andrei

For what it's worth, both Algebraic!(T, void*) and Algebraic!(T, void) compile on my branch of std.variant.

can't assign null alias Algebraic!(int,void) nint; nint n1=5; assert(n1==5); n1=null; \std\variant.d:497: Error: static assert "Cannot store a void* in a VariantN!(maxSize,int,void). Valid types are (int, void)" test.d:45: instantiated from here: opAssign!(void*)

Yes, compiler qualifies null as void*. I was playing with void* version few weeks ago and I can confirm that both assignment and equality testing works for nulls. One thing I missed was lack of conversion to string "null". For example array of Algebraic!(int, void*) was printed as [0, 0, 2], when element 1 was null. I think it should rather be [0, null, 2].
Mar 26 2011
prev sibling parent Kagamin <spam here.lot> writes:
Kagamin Wrote:

 Robert Jacques Wrote:
 
 Should be Algebraic!(T, void). If that doesn't currently compile, we  
 should make it compile.

 Andrei

For what it's worth, both Algebraic!(T, void*) and Algebraic!(T, void) compile on my branch of std.variant.

can't assign null alias Algebraic!(int,void) nint; nint n1=5; assert(n1==5); n1=null; \std\variant.d:497: Error: static assert "Cannot store a void* in a VariantN!(maxSize,int,void). Valid types are (int, void)" test.d:45: instantiated from here: opAssign!(void*)

Ahh, there's a workaround: alias Algebraic!(int,void) nint; nint n1=5; assert(n1.get!(int)==5); n1=nint(); //uninitialized Variant, sort of null assert(!n1.hasValue); assert(n1.peek!(int)==null); writeln("error here:"); assert(n1.get!(int)==0);
Mar 26 2011
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 3/26/11 4:20 PM, Robert Jacques wrote:
 On Sat, 26 Mar 2011 15:58:18 -0400, Andrei Alexandrescu
 <SeeWebsiteForEmail erdani.org> wrote:

 On 3/26/11 2:11 PM, Piotr Szturmaj wrote:
 I propose that, Nullable should be an alias (or subtype) of Algebraic,
 for example:

 template Nullable(T)
 {
 alias Algebraic!(T, void*) Nullable;
 }

Should be Algebraic!(T, void). If that doesn't currently compile, we should make it compile. Andrei

For what it's worth, both Algebraic!(T, void*) and Algebraic!(T, void) compile on my branch of std.variant.

Great. I'd prefer the one using void because storing a void* can be a legitimate need. Andrei
Mar 26 2011
prev sibling next sibling parent "Robert Jacques" <sandford jhu.edu> writes:
On Sat, 26 Mar 2011 15:58:18 -0400, Andrei Alexandrescu  
<SeeWebsiteForEmail erdani.org> wrote:

 On 3/26/11 2:11 PM, Piotr Szturmaj wrote:
 I propose that, Nullable should be an alias (or subtype) of Algebraic,
 for example:

 template Nullable(T)
 {
 alias Algebraic!(T, void*) Nullable;
 }

Should be Algebraic!(T, void). If that doesn't currently compile, we should make it compile. Andrei

For what it's worth, both Algebraic!(T, void*) and Algebraic!(T, void) compile on my branch of std.variant.
Mar 26 2011
prev sibling parent KennyTM~ <kennytm gmail.com> writes:
On Mar 27, 11 03:11, Piotr Szturmaj wrote:
 I propose that, Nullable should be an alias (or subtype) of Algebraic,
 for example:

 template Nullable(T)
 {
      alias Algebraic!(T, void*) Nullable;
 }

 This way it will share the same backend as Variant, so both types will
 be compatible.

template Nullable(T) if (!__traits(compiles, {T a=null;}) { ... } I don't think it makes sense to Nullable something that can already take null.
Mar 26 2011