www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - AST Macros: Just what will they be able to do?

reply Robert Fraser <fraserofthenight gmail.com> writes:
On my bus ride this morning, I was thinking about how to make web 2.0
programming more like desktop GUI programming, and I came up with an idea
that's probably been come up with many times before: what if the entire website
could be designed programmatically, similarly to .NET forms or something? In
particular, I was thinking "what if callbacks could somehow be parsed into
client and server-side components automatically?" I think an example is
necessary here, so lets' say the coder has two checkboxes -- cb1 and cb2. They
are D objects when the website is being created by the servlet, that render as
<input type="checkbox"> or whatever. Now say the coder writes this:

cb1.onChange = {
    if(cb1.enabled)
        cb2.enabled = true;
    else
        cb2.enabled = false;
}

cb2.onChange = {
    if(cb2.enabled)
        doSomethingOnTheServer();
}

Using AST macros, would there be any way for the program to figure out at
compile-time cb1s callback could be done entirely on the client side via
JavaScript or something while cb2's would require some AJAX calls in some cases
(which would then be generated automatically)? In particular, it would need to
know that everything within the callback for cb1 refers to variables/state
within a particular set those which are available on the client).

So, what's the chance that AST macros would have this power? I think it'd make
writing web interfaces a lot more interesting... Of course, I'd still need to
figure out how to transform the D code to JavaScript, but I'm sure a little
CTFE magic will help out with that (time to port that DMDFE to CTFE...).
Aug 15 2007
next sibling parent reply Paul Collier <paching gmail.com> writes:
Robert Fraser wrote:
 On my bus ride this morning, I was thinking about how to make web 2.0
programming more like desktop GUI programming, and I came up with an idea
that's probably been come up with many times before: what if the entire website
could be designed programmatically, similarly to .NET forms or something? In
particular, I was thinking "what if callbacks could somehow be parsed into
client and server-side components automatically?" I think an example is
necessary here, so lets' say the coder has two checkboxes -- cb1 and cb2. They
are D objects when the website is being created by the servlet, that render as
<input type="checkbox"> or whatever. Now say the coder writes this:
 
 cb1.onChange = {
     if(cb1.enabled)
         cb2.enabled = true;
     else
         cb2.enabled = false;
 }
 
 cb2.onChange = {
     if(cb2.enabled)
         doSomethingOnTheServer();
 }
 
 Using AST macros, would there be any way for the program to figure out at
compile-time cb1s callback could be done entirely on the client side via
JavaScript or something while cb2's would require some AJAX calls in some cases
(which would then be generated automatically)? In particular, it would need to
know that everything within the callback for cb1 refers to variables/state
within a particular set those which are available on the client).
 
 So, what's the chance that AST macros would have this power? I think it'd make
writing web interfaces a lot more interesting... Of course, I'd still need to
figure out how to transform the D code to JavaScript, but I'm sure a little
CTFE magic will help out with that (time to port that DMDFE to CTFE...).

Cool to see a post about this :) I was also pondering something similar a while back... D to Javascript converter, RPC inserted for you to link up client/server stuff, automatic Google Gears integration, all the rest. It's a huge endeavour, though--I mean full-stack-web-framework huge. Steve Yegge even mentioned something on the topic. Still neat to think about, though :) Anyways, relating to your post... you'd definitely want to make a separate tool using the frontend code to do any of that sort of stuff. While you'll quite possibly be able to do a huge amount of introspection or whatnot a la Lisp there're bound to be some corner cases where you find that the compiler's just not capable yet, or has a bug, or something else that'll cause you headaches. There's just too much stuff that can trip you up. Besides, while CTFEs are pretty impressive, it's much nicer to have a *fully* capable programming environment. And you probably don't want to just bloat up the object code with Javascript code, etc etc. Not to discourage you! I truly hope macros would be up to the task too. I don't think anyone really knows what they'll be capable of, yet...
Aug 16 2007
parent reply Regan Heath <regan netmail.co.nz> writes:
Why javascript, why not:
http://www.digitalmars.com/dscript/index.html
Aug 16 2007
next sibling parent Paul Collier <paching gmail.com> writes:
Regan Heath wrote:
 Why javascript, why not:
 http://www.digitalmars.com/dscript/index.html

the designated frontend bits translated to Javascript and sent to the client's browser. But you're right that dscript would be great for testing if such a framework did exist :D
Aug 16 2007
prev sibling parent BCS <ao pathlink.com> writes:
Reply to Regan,

 Why javascript, why not:
 http://www.digitalmars.com/dscript/index.html

If dscript has net access, then why not both?
Aug 16 2007
prev sibling parent reply BCS <ao pathlink.com> writes:
Reply to Robert,

 lets' say the
 coder has two checkboxes -- cb1 and cb2. They are D objects when the
 website is being created by the servlet, that render as <input
 type="checkbox"> or whatever. Now say the coder writes this:
 
 cb1.onChange = {
 if(cb1.enabled)
 cb2.enabled = true;
 else
 cb2.enabled = false;
 }
 cb2.onChange = {
 if(cb2.enabled)
 doSomethingOnTheServer();
 }
 Using AST macros, would there be any way for the program to figure out
 at compile-time cb1s callback could be done entirely on the client
 side via JavaScript or something while cb2's would require some AJAX
 calls in some cases (which would then be generated automatically)? In
 particular, it would need to know that everything within the callback
 for cb1 refers to variables/state within a particular set those which
 are available on the client).

I once wrote a code generate that attempts to build a network interface proxy class (given a D interface, generate client/server code to allow a client to exercise an object on one computer from another computer) It' was a nasty pile before I more or less gave up on it. However with __traits, it might be buildable at compile time. To boot, you might also be able to build something analogous that would wright the AJAX code to let the client side run from JavaScript. I'm thinking you would use it like this interface I {/*stuff*} class C : I {/*more stuff*/} class MyPage : WebPage { this() { InsertCode(AJAX!(I).ClientAJAX()); // put in needed javascript InsertCode(CustomCodeUsingAJAX); // put in your javascript //... } void BuildState() // called for each new page { auto o = AJAX!(I).Registered(new C()); InsertCode(o.Binding); // insert code to get right object on server side } } Assuming lots of functionality in the WebPage class, the AJAX template would generate: client side AJAX (JavaScript) to call back to the server server side (D) to handle it code to keep track of object all around It's a pipe dream for now, but not outside the range of the possible.
Aug 16 2007
parent kenny <funisher gmail.com> writes:
BCS wrote:
 Reply to Robert,
 
 lets' say the
 coder has two checkboxes -- cb1 and cb2. They are D objects when the
 website is being created by the servlet, that render as <input
 type="checkbox"> or whatever. Now say the coder writes this:

 cb1.onChange = {
 if(cb1.enabled)
 cb2.enabled = true;
 else
 cb2.enabled = false;
 }
 cb2.onChange = {
 if(cb2.enabled)
 doSomethingOnTheServer();
 }
 Using AST macros, would there be any way for the program to figure out
 at compile-time cb1s callback could be done entirely on the client
 side via JavaScript or something while cb2's would require some AJAX
 calls in some cases (which would then be generated automatically)? In
 particular, it would need to know that everything within the callback
 for cb1 refers to variables/state within a particular set those which
 are available on the client).

I once wrote a code generate that attempts to build a network interface proxy class (given a D interface, generate client/server code to allow a client to exercise an object on one computer from another computer) It' was a nasty pile before I more or less gave up on it. However with __traits, it might be buildable at compile time. To boot, you might also be able to build something analogous that would wright the AJAX code to let the client side run from JavaScript. I'm thinking you would use it like this interface I {/*stuff*} class C : I {/*more stuff*/} class MyPage : WebPage { this() { InsertCode(AJAX!(I).ClientAJAX()); // put in needed javascript InsertCode(CustomCodeUsingAJAX); // put in your javascript //... } void BuildState() // called for each new page { auto o = AJAX!(I).Registered(new C()); InsertCode(o.Binding); // insert code to get right object on server side } } Assuming lots of functionality in the WebPage class, the AJAX template would generate: client side AJAX (JavaScript) to call back to the server server side (D) to handle it code to keep track of object all around It's a pipe dream for now, but not outside the range of the possible.

IMO, you will want to do it a bit different than that. (replying to the original post and this post). Let's say I have a bunch of HTML. Now, a common example would be for this sort of thing a form where a textbox has a greyed out text that says "type your name here.." -- then onfocus, that text becomes none. onblur, if the value of field == '', then set it back to the text. that requires knowing the default text (whether hard coded in the events, or the innerHTML of a hidden div) the active css class (custom text, and appears selected) the inactive css class (custom text, but appears deselected) the default-text css class also 3 events: onfocus onblur onchange The only experience I have with your sort of knowledge would be VB 6.0, so bear with me... frm1.onfocus() { this.value = ''; this.className = 'form-active'; } frm1.onchange() { this.onblur(); } frm1.onblur() { if(this.value == '') { this.value = this.default; this.className = 'form-default'; } else if(this.value != this.default) { this.className = 'form-inactive'; } } ---------------------------- Now, why would I ever want to write all of that more than once? Like in VB, I would have to copy and paste that code to every textbox in the form (UGHHHHH!!!! I HATE VB) Wouldn't it be so much neater, tighter and otherwise more awesome if I had something like this: .. some html ... <div id="contain"><%textbox style: "width:198px" name: "fullname" default_text: "your name please..." onchange: "hideElement('contain')'" %></div> ... more html ... ---------------------------- You see, what good is making all that, when a generic template will do the same? It doesn't give you unique examples though... Let's say you wanted it so everything could be clicked on to make an event... Since to do that, you would essentially have to recreate the DOM, you could just parse the HTML and put it in a dom. The parts that get clicked on turn into templates... <%div id: "myuser-${User.uid}" onclick: "alert('${User.firstname} is gay')" class: "myclass" style: "background-color: #f43" %>HEy!!!</div> That's super easy to do. Now, let's say you wanted to reference other javascript ids. Assume you have the $() function like firebug and prototype does... <div id="showme" style="display:none">show me</div> <%span onmouseover: "showElement($('showme'))" class: "lala" %>weird</span> Your template parser could EASILY verify that the id 'showme' exists. ---------------------------- It just seems to me that a VB-like interface would only get you so far (taking into consideration that HTML with wysiwyg is hell), and a MVC-esque design with a smart template scheme could get you a lot further. Writing something like that would take much less time too -- maybe like 2 months or so, instead of a year, lol. Just thoughts, Kenny
Aug 17 2007