Re: On the benefits of reflection

Douglas Cox <[email protected]> Tue, 19 Mar 2013 16:21:10 -0400
Newsgroups gmane.games.devel.sweng
Message-ID <CA+i4re4HnFnFcSka12fAVRQFzMdgjns=f4tQuGn47GyzDqyW9g@mail.gmail.com>
--===============1697544044==
Content-Type: multipart/alternative; boundary=bcaec51dd567a67cf004d84cd8d7

--bcaec51dd567a67cf004d84cd8d7
Content-Type: text/plain; charset=ISO-8859-1

I'm not entirely sure what your question is (perhaps an example of what
you're trying to ask may help), but I can tell you what we would do and
what the benefits are. This example is not real code, but it shows

class Texture
{
    [EditableFilename( "png" )]
    String filename;

    [Editable( onChanged=OnFormatChanged )]
    TextureFormat format;

    [EditableFloat( 0.0, 1.0 )]
    float scale;

    TextureData data;

    void OnFormatChanged()
    {
       ... re-cache texture on PC ...
    }
}

// Just some example of how we access the type info.
void DoSomethingWithFields( Object object )
{
    foreach( var field in typeof( object ).fields )
        Console.WriteLine( "Field: {0} {1} = {2}", field.type.name,
field.name, field.GetValue( object ) );
}

So from reflection we're obviously able to enumerate the fields of any
type, get the type of field, its attributes, and from reflection and
invoking, we can modify the field and notify the class of changes from
editor-side only if needed.  Those attributes and notification methods can
easily be compiled away in final builds if need be.

Reflection can tell us what needs to save to disk or what needs to be
serialized over the network.  Sure you can write your own Read/Write code
(and we did in the past), but that gets messy and is just another copy of
the information (fields) that you've already entered somewhere else.  And
sure, you could have a compiler generate a Read/Write function for you, but
changing/updating that code then requires changing the compiler and
recompiling your entire code base.

Our goal is to make writing code as simple and clean as possible while
avoiding redundant typing.  If you have another way to do this, then that's
cool too - I'd love to see an example.

And for us, there's also a small amount of reflection needed for our
garbage collector...

-Doug



On Tue, Mar 19, 2013 at 3:15 PM, Massimo Del Zotto <[email protected]>wrote:

> Let me elaborate on those points so I hope people can get in my point of
> view.
>
> 1- "Trivial" network system. Done for ages, before reflection was in
> vogue. Considering how UDK manages that thing, I believe we're cutting a
> lot of corners. What's the problem in just doing serialization in the
> standard way?
> 2- Config system. No idea what "the same hardware" is supposed to mean.
> I'd be careful in making my networking subsystem work the same as a disk IO.
> 3- save load. In my system, just restoring VM state is going nowhere.
> Maybe the scripts will just churn along. Sure engine state isn't going to
> make it there.
> 4- Message passing... through the networking system? I don't see the
> connection with reflection. I'd be extra super careful in doing that.
> Message passing to who? For what purpose? At what frequency?
> 5- I am using reflection myself in my system as well. Good for your
> friend. Perhaps you could contact him and ask what exactly made his work
> easier? I'd be interested in considering those cases. As I said, I have
> this little language of mine I want to evolve in the right direction.
>
>   I then use reflection to automatically type safe handle the messages.
>>  So, you just put:
>> void handl( msg.MoveTo move )
>>
>>   And higher level code calls down into it.  If this shows up in
>> profiling, ill special case the heavily called messages and leave the rest.
>>
>>
> I have no idea on what you mean there. I actually don't even understand
> what's really the point. The prototype you show leaves me confused. If the
> above is the equivalent of void handl(Message *something) then I'm afraid
> this could work with dynamic_cast in some cases or with structures designed
> to be somehow analyzed, perhaps through a "property" system. What is the
> extra plus reflection gives me?
>
> Massimo
>
>
> _______________________________________________
> Sweng-Gamedev mailing list
> [email protected]
> http://lists.midnightryder.com/listinfo.cgi/sweng-gamedev-midnightryder.com
>
>

--bcaec51dd567a67cf004d84cd8d7
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I&#39;m not entirely sure what your question is (perhaps a=
n example of what you&#39;re trying to ask may help), but I can tell you wh=
at we would do and what the benefits are. This example is not real code, bu=
t it shows=A0<div>
<br></div><div style>class Texture</div><div style>{</div><div style>=A0 =
=A0 [EditableFilename( &quot;png&quot; )]</div><div style>=A0 =A0 String fi=
lename;</div><div style><br></div><div style>=A0 =A0 [Editable( onChanged=
=3DOnFormatChanged )]</div>
<div style>=A0 =A0 TextureFormat format;</div><div style><br></div><div sty=
le>=A0 =A0 [EditableFloat( 0.0, 1.0 )]</div><div style>=A0 =A0 float scale;=
</div><div style><br></div><div style>=A0 =A0 TextureData data;</div><div s=
tyle><br></div>
<div style>=A0 =A0 void OnFormatChanged()</div><div style>=A0 =A0 {</div><d=
iv style>=A0 =A0 =A0 =A0... re-cache texture on PC ...</div><div style>=A0 =
=A0 }<br>}</div><div style><br></div><div style>// Just some example of how=
 we access the type info.</div>
<div style>void DoSomethingWithFields( Object object )</div><div style>{</d=
iv><div style>=A0 =A0 foreach( var field in typeof( object ).fields )</div>=
<div style>=A0 =A0 =A0 =A0 Console.WriteLine( &quot;Field: {0} {1} =3D {2}&=
quot;, <a href=3D"http://field.type.name">field.type.name</a>, <a href=3D"h=
ttp://field.name">field.name</a>, field.GetValue( object ) );<br>
}</div><div style><br></div><div style>So from reflection we&#39;re obvious=
ly able to enumerate the fields of any type, get the type of field, its att=
ributes, and from reflection and invoking, we can modify the field and noti=
fy the class of changes from editor-side only if needed. =A0Those attribute=
s and notification methods can easily be compiled away in final builds if n=
eed be.</div>
<div style><br></div><div style>Reflection can tell us what needs to save t=
o disk or what needs to be serialized over the network. =A0Sure you can wri=
te your own Read/Write code (and we did in the past), but that gets messy a=
nd is just another copy of the information (fields) that you&#39;ve already=
 entered somewhere else. =A0And sure, you could have a compiler generate a =
Read/Write function for you, but changing/updating that code then requires =
changing the compiler and recompiling your entire code base.</div>
<div style><br></div><div style>Our goal is to make writing code as simple =
and clean as possible while avoiding=A0redundant=A0typing. =A0If you have a=
nother way to do this, then that&#39;s cool too - I&#39;d love to see an ex=
ample.<br>
</div><div style><br></div><div style>And for us, there&#39;s also a small =
amount of reflection needed for our garbage collector...</div><div style><b=
r></div><div style>-Doug</div><div style><br></div></div><div class=3D"gmai=
l_extra">
<br><br><div class=3D"gmail_quote">On Tue, Mar 19, 2013 at 3:15 PM, Massimo=
 Del Zotto <span dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" ta=
rget=3D"_blank">[email protected]</a>&gt;</span> wrote:<br><blockquote c=
lass=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;=
padding-left:1ex">
<div>Let me elaborate on those points so I hope people can get in my point =
of view.</div><div><br></div>1- &quot;Trivial&quot; network system. Done fo=
r ages, before reflection was in vogue. Considering how UDK manages that th=
ing, I believe we&#39;re cutting a lot of corners. What&#39;s the problem i=
n just doing serialization in the standard way?<br>

<div>2- Config system. No idea what &quot;the same hardware&quot; is suppos=
ed to mean. I&#39;d be careful in making my networking subsystem work the s=
ame as a disk IO.</div><div>3- save load. In my system, just restoring VM s=
tate is going nowhere. Maybe the scripts will just churn along. Sure engine=
 state isn&#39;t going to make it there.</div>

<div>4- Message passing... through the networking system? I don&#39;t see t=
he connection with reflection. I&#39;d be extra super careful in doing that=
. Message passing to who? For what purpose? At what frequency?</div><div>

5- I am using reflection myself in my system as well. Good for your friend.=
 Perhaps you could contact him and ask what exactly made his work easier? I=
&#39;d be interested in considering those cases. As I said, I have this lit=
tle language of mine I want to evolve in the right direction.<br>

<br><div class=3D"gmail_quote"><div class=3D"im"><blockquote class=3D"gmail=
_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:=
1ex"><div dir=3D"ltr"><div>=A0 I then use reflection to automatically type =
safe handle the messages. =A0So, you just put:</div>



<div>void handl( msg.MoveTo move )</div><div><br></div><div>=A0 And higher =
level code calls down into it. =A0If this shows up in profiling, ill specia=
l case the heavily called messages and leave the rest. =A0</div></div></blo=
ckquote>

</div><div>I have no idea on what you mean there. I actually don&#39;t even=
 understand what&#39;s really the point. The prototype you show leaves me c=
onfused. If the above is the equivalent of void handl(Message *something) t=
hen I&#39;m afraid this could work with dynamic_cast in some cases or with =
structures designed to be somehow analyzed, perhaps through a &quot;propert=
y&quot; system. What is the extra plus reflection gives me?</div>
<span class=3D"HOEnZb"><font color=3D"#888888">
<div><br></div><div>Massimo=A0</div><div><br></div></font></span></div></di=
v>
<br>_______________________________________________<br>
Sweng-Gamedev mailing list<br>
<a href=3D"mailto:[email protected]">Sweng-Gamedev@list=
s.midnightryder.com</a><br>
<a href=3D"http://lists.midnightryder.com/listinfo.cgi/sweng-gamedev-midnig=
htryder.com" target=3D"_blank">http://lists.midnightryder.com/listinfo.cgi/=
sweng-gamedev-midnightryder.com</a><br>
<br></blockquote></div><br></div>

--bcaec51dd567a67cf004d84cd8d7--

--===============1697544044==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Sweng-Gamedev mailing list
[email protected]
http://lists.midnightryder.com/listinfo.cgi/sweng-gamedev-midnightryder.com

--===============1697544044==--