Re: How To Dynamic Web Rendering?

2011-05-17 Thread Jacob Carlborg

On 2011-05-16 15:43, Adam D. Ruppe wrote:

Instead you move the view layer into the model or controller
layer. How's that any different?


Is that really what's happening? Any template has variables made
available to it from the model. I'm just making them available
at a higher level (pre-wrapped in semantic tags and grouped
together).

The actual layout and styling, sometimes even data formatting - the
stuff I think of as the view - is still done entirely in the
separate html and css files.

I realize this isn't a perfect argument, but it's still worth it
to me to have those other processing options available.


I got the impression that you basically did all HTML with D methods, 
maybe I don't understand your work flow.


--
/Jacob Carlborg


Re: Can we make auto return type inference a little more lax?

2011-05-17 Thread bearophile
Andrej Mitrovic:

 I don't think the compiler can figure that one out unless tuples
 become a standard language feature, not a library one. But maybe I'm
 wrong.

Turning tuples into a a _partial_ language feature will be a good thing [TM] 
for D. Tuples are useful in many situations, and they are easy to use.

Bye,
bearophile


Re: How To Dynamic Web Rendering?

2011-05-17 Thread Nick Sabalausky
Jacob Carlborg d...@me.com wrote in message 
news:iqt6kb$1nd1$1...@digitalmars.com...
 On 2011-05-16 15:43, Adam D. Ruppe wrote:
 Instead you move the view layer into the model or controller
 layer. How's that any different?

 Is that really what's happening? Any template has variables made
 available to it from the model. I'm just making them available
 at a higher level (pre-wrapped in semantic tags and grouped
 together).

 The actual layout and styling, sometimes even data formatting - the
 stuff I think of as the view - is still done entirely in the
 separate html and css files.

 I realize this isn't a perfect argument, but it's still worth it
 to me to have those other processing options available.

 I got the impression that you basically did all HTML with D methods, maybe 
 I don't understand your work flow.


AIUI:

1. It starts out as ordinary HTML with no special non-standard tags or 
anything. But it might use id and class attributes to mean certain things.

2. In D, that HTML is loaded and parsed into an HTML DOM.

3. In D, the data is filled into the HTML DOM. Sometimes this might involve 
a amall amount of adding HTML tags/attributes or other weird tweaking if 
necessary.

4. The new HTML DOM is written out as straight HTML.

Basically just like DHTML, except on the server and using D.





Re: How To Dynamic Web Rendering?

2011-05-17 Thread Nick Sabalausky
Nick Sabalausky a@a.a wrote in message 
news:iqtarl$229l$1...@digitalmars.com...
 Jacob Carlborg d...@me.com wrote in message 
 news:iqt6kb$1nd1$1...@digitalmars.com...
 On 2011-05-16 15:43, Adam D. Ruppe wrote:
 Instead you move the view layer into the model or controller
 layer. How's that any different?

 Is that really what's happening? Any template has variables made
 available to it from the model. I'm just making them available
 at a higher level (pre-wrapped in semantic tags and grouped
 together).

 The actual layout and styling, sometimes even data formatting - the
 stuff I think of as the view - is still done entirely in the
 separate html and css files.

 I realize this isn't a perfect argument, but it's still worth it
 to me to have those other processing options available.

 I got the impression that you basically did all HTML with D methods, 
 maybe I don't understand your work flow.


 AIUI:

 1. It starts out as ordinary HTML with no special non-standard tags or 
 anything. But it might use id and class attributes to mean certain things.

 2. In D, that HTML is loaded and parsed into an HTML DOM.

 3. In D, the data is filled into the HTML DOM. Sometimes this might 
 involve a amall amount of adding HTML tags/attributes or other weird 
 tweaking if necessary.

 4. The new HTML DOM is written out as straight HTML.

 Basically just like DHTML, except on the server and using D.


And AIUI, it can also be thought of as being like an HTML templating engine, 
except instead of working with proprietary tags and text subsitution, it 
works with id/class attributes and an enhanced HTML DOM.





Re: How To Dynamic Web Rendering?

2011-05-17 Thread Jacob Carlborg

On 2011-05-17 10:11, Nick Sabalausky wrote:

Jacob Carlborgd...@me.com  wrote in message
news:iqt6kb$1nd1$1...@digitalmars.com...

On 2011-05-16 15:43, Adam D. Ruppe wrote:

Instead you move the view layer into the model or controller
layer. How's that any different?


Is that really what's happening? Any template has variables made
available to it from the model. I'm just making them available
at a higher level (pre-wrapped in semantic tags and grouped
together).

The actual layout and styling, sometimes even data formatting - the
stuff I think of as the view - is still done entirely in the
separate html and css files.

I realize this isn't a perfect argument, but it's still worth it
to me to have those other processing options available.


I got the impression that you basically did all HTML with D methods, maybe
I don't understand your work flow.



AIUI:

1. It starts out as ordinary HTML with no special non-standard tags or
anything. But it might use id and class attributes to mean certain things.

2. In D, that HTML is loaded and parsed into an HTML DOM.

3. In D, the data is filled into the HTML DOM. Sometimes this might involve
a amall amount of adding HTML tags/attributes or other weird tweaking if
necessary.

4. The new HTML DOM is written out as straight HTML.

Basically just like DHTML, except on the server and using D.


Ok I see.

--
/Jacob Carlborg


Re: How To Dynamic Web Rendering?

2011-05-17 Thread Adam D. Ruppe
Jacob Carlborg wrote:
 That would require a lot of otherwise (possibly) unnecessary
 id/class attributes (if they aren't removed).

Not that much in the sites I've done.

Here's another real example, that new d web site discussed on the
newsgroup a while ago:

Dynamic page:
http://arsdnet.net/d-web-site/

The HTML template it manipulates with DOM:
http://arsdnet.net/d-web-site/index.html


There's only three dynamic elements on that page, so the template
is almost identical to the finished page.

There's three placeholders getting filled in:

div id=announcements /
div id=commits /

and form id=code-runner



And the relevant code:

form:

  auto f = cast(Form) document.getElementById(code-runner);
  f.setValue(code, `void main() { assert(0, Hello, world!); }`);


It fills the form, without caring about how the form is written.
This does name=code and sets value to that string given. It
doesn't care how the HTML is written - here, it's a textarea,
but this code remains the same if it's an input or select.

The view controls how the data is presented, the dom just tells
it what to present.


Announcements and commits are done with more dom calls, but still,
not much.

void addAnnouncements(Element holder) {
  auto document = holder.parentDocument;

  foreach(announcement; getAnnouncements()) {
auto h = holder.addChild(div);

auto date = std.date.parse(announcement.date);

h.addChild(span, formatDate(date));
h.appendText( - );
h.appendChild(new Link(announcement.url, announcement.subject));
  }
}

Commits is a little longer since it has more data, but same idea.


Arguably, this crosses the line into presentation, especially
with that appendText call. (I guess it could do an #announcements 
span:after in css, but that's not reliable) But, you can see
that the vast majority of html is far away from here.

The #announcements id used to fill it in is also used to style
the inside:

#announcements  div { style each announcement }
#announcements  div  span { style the date }
#announcements  div  a { style the link }


(man, css nesting is such a good thing to have!)




Side note: obviously, you don't *have* to do web apps my way, even
with my libraries. cgi.d is template-agnostic. dom.d isn't going
to kill you if you ask it to add logic to templates or do string
replacement instead.

My way is just one possibility of many.


Re: How To Dynamic Web Rendering?

2011-05-17 Thread Nick Sabalausky
Adam D. Ruppe destructiona...@gmail.com wrote in message 
news:iqua65$rm2$1...@digitalmars.com...

  auto f = cast(Form) document.getElementById(code-runner);
  f.setValue(code, `void main() { assert(0, Hello, world!); }`);


Minor suggestion:

There should be an overload of getElementById that's defined like this:

T getElementById(T)(string str) if(is(T:WhateverYourBaseNodeTypeIs))
{
auto node = cast(T) getElementById(str);
if(node)
return node;
else
throw new ElementNotFoundException(T.stringof, str);
}

class ElementNotFoundException : Exception
{
this(string type, string str)
{
super(Element of type '~type~' matching '~str~' not found.);
}
}

That way if it's missing, you get an accurate message instead of an awful 
null pointer exception. Or maybe call it requireElementById.

Or, of course, if it's in a class and can't have templated members (I really 
hate that limitation. Templated class members would be so much more useful 
than being able to ship .obj's and .lib's without the original source), 
then:

T getElementById(T)(WhateverYourBaseNodeTypeIs root, string str)
if(is(T:WhateverYourBaseNodeTypeIs))
{
...
}

And, of course, just use the original getElementById if you want to allow 
the element to be optional.

Another separate thought:

What happens if you want to (or try to) put the same data in more than one 
place? Maybe there should be a way to detect and error if there's more than 
one of the same thing. Or maybe better yet, getElementById could return a 
special collection that contains all matching elements. And it could expose 
the same interface as a single element, but automatically applies it to all 
matched elements. Of course, you couldn't use id= for that since that's 
really not supposed to have duplicates. So you'd have to use class= 
instead. Which opens the possibility, if you're not doing this already, for 
the HTML importing (and maybe the DOM, too, or at least serializing the DOM 
at the end) to detect and error on multiple instances of the same id=.




Re: How To Dynamic Web Rendering?

2011-05-17 Thread Adam D. Ruppe
Nick Sabalausky wrote:
 There should be an overload of getElementById that's defined like
 this:

Aye, I've been thinking about that for a while, but could never think
of a name I like (yeah, petty reason).

I've just trained myself to use assert to avoid the null pointers.

 Or maybe call it requireElementById.

That's a pretty good idea.

 Or, of course, if it's in a class and can't have templated members

It is a class, but the requireElementById (and it's counterpart,
requireSelector*) could always be final. I can't think of a reason
to override them anyway, especially since the underlying
implementation in getElementById is still overridable anyway.

(same reason why I'm ok with opDispatch - it just forwards to
set/getAttribute)

This is definitely good though, it has the best cost/benefit ratio
of things on the todo list. I take your name!


side note::

* After looking at haml yesterday, I'm tempted to do a nothrow
requireSelector. Again, can't think of a name.

But what it'd do is try to get the first element that matches the
css syntax. If it's not there, it starts from the best path that
does exist and creates the rest.

So if you have a blank element html/html and you do:

makeSelector(html); // that root node is returned since it exists

makeSelector(div  span.date);

That doesn't exist, so it does a

return document.root.addChild(div).addChild(span).className(date);


Though, I don't think I have a use for it; it'd be harder to use
than just writing the html yourself in the template file.

:: end side note

 What happens if you want to (or try to) put the same data in more
 than one place?

Use a class or a custom attribute instead of id, then loop through
the collection:

foreach(e; document.getElementsBySelector(div.announcements))
addAnnouncements(e);


Sometimes I do that even when there's just one because the return
value there is guaranteed to never include null.


 Which opens the possibility, if you're not doing this already, for
 the HTML importing (and maybe the DOM, too, or at least
 serializing the DOM at the end) to detect and error on multiple
 instances of the same id=.

No, it doesn't check that right now. It treats id as just another
attribute for the most part. (getElementById returns the first one
it sees in the tree.)

It always could, but I haven't felt it's worth the time yet.


Re: Can we make auto return type inference a little more lax?

2011-05-17 Thread Robert Clipsham

On 17/05/2011 04:40, Andrej Mitrovic wrote:

auto foo()
{
 if (1)
 {
 return [0, 0];
 }
 else
 {
 size_t one;
 size_t two;

 return [one, two];
 }
}

void main(){ }

Error: mismatched function return type inference of
uint[] and int[]

Surely the compiler can figure out a common type for these two when literals 
are involved.


I haven't tested, but make the first array [0u, 0u], that should work, 
as a work around. Make a bug report for this (maybe a patch if you feel 
up to it? :D).


--
Robert
http://octarineparrot.com/


Re: Can we make auto return type inference a little more lax?

2011-05-17 Thread Andrej Mitrovic
Bug is in bugzilla: http://d.puremagic.com/issues/show_bug.cgi?id=6022

I'm not a compiler writer though, sorry. :)


Memory corruption rant.

2011-05-17 Thread Jesse Phillips
I would love to post a bug with a minimum test case, so that I'd be able to 
identify if its my fault, the compiler, or libraries I'm using. But at this 
time I can't seem to reduce it to a reasonable (non-proprietary) portion of 
code. So for now I'll just rant.

I combining much here. I have two DLL's being loaded. One is Lua, being 
accessed with LuaD, and the other is a DLL my company has produced. I've been 
able to get some nice interaction between the three. The company DLL will make 
function calls which are passed through D into Lua and the result back through 
D and all this great stuff.

But I've been fighting with delegates and objects disappearing (I think LuaD 
might not be preserving anonymous delegates, which I can't reproduce in the 
small). I have some code that has this general flow:

auto myObj = new Object();

// Create lua and company dll objects
// call some functions for lua
// call some functions for company dll
// Other heavy processing with D only (takse  60 seconds)
//  make a call to company dll (takes  30 seconds)

writeln(Im still ok)
writeln(myObject is null); // Crash with access violation

I can remove the last call to the company dll and everything is fine. And this 
has been working for some time until recently when I changed some stuff on the 
heavy D processing side (added a struct into the mix).

I'm not even accessing myObject, I'm just asking if it has a value it shouldn't!

In all other respects it has been a blast combining all this.


Re: Memory corruption rant.

2011-05-17 Thread Trass3r

Could it be a stack corruption?


Re: Memory corruption rant.

2011-05-17 Thread Vladimir Panteleev
On Wed, 18 May 2011 02:18:22 +0300, Jesse Phillips  
jessekphillip...@gmail.com wrote:



[snip memory corruption rant]


I've nothing to contribute but by own anecdotal experience. So: are you  
doing *any* allocations or throwing exceptions in finalizers (destructors  
of objects on the heap)? If you are, don't do it. :P


--
Best regards,
 Vladimirmailto:vladi...@thecybershadow.net


Re: Non-GC threads

2011-05-17 Thread Vladimir Panteleev
On Sun, 15 May 2011 21:30:32 +0300, Piotr Szturmaj bncr...@jadamspam.pl  
wrote:



What are the consequences of using non garbage collected threads in D?


If you move pointers around while the GC is looking for them, the GC might  
miss them and free referenced memory. This will lead to memory corruption.


The GC will also not be able to scan the stack and registers of threads it  
doesn't know about.


I want to write a reliable communication protocol, but I don't want  
suspend this code by the GC.


The GC might be much faster than you think it is. You should try  
benchmarking the GC with a typical memory load, the delay might be  
acceptable for your purposes.


Is there any method to bind allocated memory to thread itself, so it  
will be freed after thread terminates, but not in the middle of  
execution?


You can use malloc/free together with RAII. Or something hacky and  
platform-dependent like pthread_cleanup_push().


This is important because in the other case, GC could free memory  
referenced by non-GC thread.


I can think of no perfect solution for your case. You'll either need to  
give up on using managed memory, or accept the periodical delays of the GC.


--
Best regards,
 Vladimirmailto:vladi...@thecybershadow.net


Re: Memory corruption rant.

2011-05-17 Thread Jesse Phillips
Trass3r Wrote:

 Could it be a stack corruption?

That would make the most sense for the behavior I'm seeing, but I don't really 
know how to corrupt the stack without using ASM, and even then I'd probably 
fail.


Re: Memory corruption rant.

2011-05-17 Thread Jesse Phillips
Vladimir Panteleev Wrote:

 On Wed, 18 May 2011 02:18:22 +0300, Jesse Phillips  
 jessekphillip...@gmail.com wrote:
 
  [snip memory corruption rant]
 
 I've nothing to contribute but by own anecdotal experience. So: are you  
 doing *any* allocations or throwing exceptions in finalizers (destructors  
 of objects on the heap)? If you are, don't do it. :P
 
 -- 
 Best regards,
   Vladimirmailto:vladi...@thecybershadow.net

Well I do use allocations to get values out of dll calls, but I didn't want to 
negotiate ownership so I duplicated everything it gave me and promptly freeing 
it through the free function it provides, and I ignore ownership of the values 
I give it (so I wouldn't be surprised if I had memory leaks, but it's not like 
the DLL will tell me when it is done with the memory)

As for the destructor thing, I'll have to check, I originally tried to use a 
reference counted struct and believe I may had thown something in a destructor 
there (think that one is safe?). But when I move to class I'm pretty sure I 
just gave up on cleaning up. I mean I'm done with it when I exit my program 
anyway.

And thing thing is that it is consistent. In that a build that fails will 
always fail and a build the succeeded always continues to function. So not only 
should it not be destroying anything yet, it really seems to be how the code is 
generated.

I actually had a print statement which printed some values of StopWatch, and I 
could consistently use that to get an access violation, and I just hoped I 
wouldn't have any more issues.

What does D do when it runs out of stack space? (Not that it is likely I am)