www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Object - part of the language, or part of Phobos?

reply Steve Teale <steve.teale britseyeview.com> writes:
module smem;

import std.stdio;

struct X
{
   int n;
   double d;
   char[] a;
}

void main()
{
   X x;
   TypeInfo typeinfo = typeid(x);                  // or typeid(x) for that
matter
   writefln("%s", typeinfo);                           // smem.X
   OffsetTypeInfo[] tia = typeinfo.offTi();        // "Get type information on
the contents of the type; null if not available"
   writeln((tia is null)? "null": "");                 // null
   // Under what circumstances is the OffsetTypeInfo information available?
}
Sep 30 2011
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, October 01, 2011 05:38:45 Steve Teale wrote:
 module smem;
 
 import std.stdio;
 
 struct X
 {
    int n;
    double d;
    char[] a;
 }
 
 void main()
 {
    X x;
    TypeInfo typeinfo = typeid(x);                  // or typeid(x) for that
 matter writefln("%s", typeinfo);                           // smem.X
    OffsetTypeInfo[] tia = typeinfo.offTi();        // "Get type information
 on the contents of the type; null if not available"
    writeln((tia is null)? "null": "");                 // null
    // Under what circumstances is the OffsetTypeInfo information available?
 }
Object is part of druntime, so it's in neither the language itself nor Phobos, but it's fairly closely tied to the language. It's in object_.d in druntime. - Jonathan M Davis
Sep 30 2011
next sibling parent reply Steve Teale <steve.teale britseyeview.com> writes:
Jonathan,

I had a secondary question - under what circumstances is the OffsetTypeInfo
available?

Since it's not part of the language, I guess whoever implemented the current
runtime just chose not to provide it, or it's a bug. I'll check out the code.

I want to associate a struct with a database row in my database wrapper, and
that
information would be very useful. The struct.tupleof goes so far, but the offset
information is not accessible unless you know what field to ask for it on.

Steve
Sep 30 2011
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, October 01, 2011 06:20:17 Steve Teale wrote:
 Jonathan,
 
 I had a secondary question - under what circumstances is the OffsetTypeInfo
 available?
 
 Since it's not part of the language, I guess whoever implemented the current
 runtime just chose not to provide it, or it's a bug. I'll check out the
 code.
 
 I want to associate a struct with a database row in my database wrapper, and
 that information would be very useful. The struct.tupleof goes so far, but
 the offset information is not accessible unless you know what field to ask
 for it on.
I don't really know the implementation details on that level, which is why I didn't give more information. It's probably in druntime. There's a set of stuff in druntime (like the GC) which the language depends on but which is part of druntime rather than the compiler, so it's sort of in the language and sort of not. Object is one of those. So, what you're looking for is probably in there, but again, I'm not that familiar with all of the implementation details. - Jonathan M Davis
Sep 30 2011
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2011-10-01 08:20, Steve Teale wrote:
 Jonathan,

 I had a secondary question - under what circumstances is the OffsetTypeInfo
available?

 Since it's not part of the language, I guess whoever implemented the current
 runtime just chose not to provide it, or it's a bug. I'll check out the code.

 I want to associate a struct with a database row in my database wrapper, and
that
 information would be very useful. The struct.tupleof goes so far, but the
offset
 information is not accessible unless you know what field to ask for it on.

 Steve
I'm pretty sure the compiler needs to provide this information, then the runtime puts it in OffsetTypeInfo. If it doesn't there's no way to provided that using OffsetTypeInfo. Are you really sure you want to depend on the field offset of a struct? If the order of the fields in the struct change, won't all code the that relies on the offset break? Wouldn't it be much better to depend on the names instead? If you really want to use the offset you should be able to use: Struct.field.offsetof; Documented here: http://www.d-programming-language.org/struct.html Search for "Struct Field Properties". -- /Jacob Carlborg
Oct 01 2011
parent reply Steve Teale <steve.teale britseyeview.com> writes:
Jacob,

So lets say I've got a result set from a database. I was thinking it would be
tidy
and convenient if you could dress the result set up as a random access range,
and
just pop the current row into a user defined struct without having to copy each
field, and was wondering if D had sufficient introspection to let me do this in
a
way that was reasonably robust. If the compiler could tell me the names of the
struct fields and the offsets, then I could get the column names and types from
the database, and bingo! But it cant.

Nonetheless, I was experimenting, and then I noticed that the class TypeInfo
OffsetTypeInfo[] offTi() method does not return anything useful.

That might be understandable if TypeInfo applied only to classes. Derived
classes
could make this method do something useful. But it doesn't - you can get an
instance for __all types__ via typeid. So the implementation in the runtime
should
provide some sensible information for structs. Hence this thread.

I can get the field types and offsets using templates defined in std.traits and
std.typecons, but there's no way to get the names of the fields.

However, I may still do it. The situation I'm considering is one where the
programmer is in charge and can make the call as to whether the convenience is
worth the risk. I can only protect her against type mismatches.

Steve
Oct 01 2011
parent reply Jacob Carlborg <doob me.com> writes:
On 2011-10-01 15:26, Steve Teale wrote:
 Jacob,

 So lets say I've got a result set from a database. I was thinking it would be
tidy
 and convenient if you could dress the result set up as a random access range,
and
 just pop the current row into a user defined struct without having to copy each
 field, and was wondering if D had sufficient introspection to let me do this
in a
 way that was reasonably robust. If the compiler could tell me the names of the
 struct fields and the offsets, then I could get the column names and types from
 the database, and bingo! But it cant.
If you get a tuple of the values you want to set you can just do like this: Struct s; s.tupleof = values;
 Nonetheless, I was experimenting, and then I noticed that the class TypeInfo
 OffsetTypeInfo[] offTi() method does not return anything useful.

 That might be understandable if TypeInfo applied only to classes. Derived
classes
 could make this method do something useful. But it doesn't - you can get an
 instance for __all types__ via typeid. So the implementation in the runtime
should
 provide some sensible information for structs. Hence this thread.

 I can get the field types and offsets using templates defined in std.traits and
 std.typecons, but there's no way to get the names of the fields.
You don't need std.typecons or std.traits for that. You can get the field types using: typeof(Struct.tupleof) And you can get the names of the fields using a bit of tupleof and stringof magic. Have a look at: https://github.com/jacob-carlborg/orange/blob/master/orange/util/Reflection.d#L212 You might find some other useful functions in that module as well.
 However, I may still do it. The situation I'm considering is one where the
 programmer is in charge and can make the call as to whether the convenience is
 worth the risk. I can only protect her against type mismatches.

 Steve
-- /Jacob Carlborg
Oct 01 2011
parent Steve Teale <steve.teale britseyeview.com> writes:
Jacob,

Yes, magic - particularly the typeof(Struct.tupleof) bit!

Thanks Steve
Oct 01 2011
prev sibling parent Sean Kelly <sean invisibleduck.org> writes:
TypeInfo instances are generated by the compiler. The types are simply defin=
ed in druntime.  Same with ModuleInfo.=20

Sent from my iPhone

On Sep 30, 2011, at 11:20 PM, Steve Teale <steve.teale britseyeview.com> wro=
te:

 Jonathan,
=20
 I had a secondary question - under what circumstances is the OffsetTypeInf=
o available?
=20
 Since it's not part of the language, I guess whoever implemented the curre=
nt
 runtime just chose not to provide it, or it's a bug. I'll check out the co=
de.
=20
 I want to associate a struct with a database row in my database wrapper, a=
nd that
 information would be very useful. The struct.tupleof goes so far, but the o=
ffset
 information is not accessible unless you know what field to ask for it on.=
=20
 Steve
Oct 01 2011
prev sibling parent Steve Teale <steve.teale britseyeview.com> writes:
Hmm - class TypeInfo is hardly implemented at all!
Sep 30 2011