Re: Are stack+heap classes possible in D?
On Friday, 19 June 2015 at 19:10:11 UTC, Shachar Shemesh wrote: On 14/06/15 04:31, Adam D. Ruppe wrote: On Sunday, 14 June 2015 at 00:52:20 UTC, FujiBar wrote: I have read that in D structs are always allocated on the stack while classes are always allocated on the heap. That's not true; it is a really common misconception. Putting a struct on the heap is trivial and built into the language: `S* s = new S();` Well Yeah. You would get a reference to a struct. The struct will be on the heap. In that narrow sense, you are right that it is possible. However, this does not behave like a normal struct. In particular, when will the destructor be called? (answer: never, not even before the memory is collected). So, no, I think D experts should avoid telling newbies it is okay to just new struct foo.[1] Shachar 1 - The counter argument is, of course, that struct destructors should not be counted upon to do anything useful anyways, as they are far from guaranteed to run even in situations where one would expect them to. This just relates to another area where D skirts truth in advertising when people say that D supports RAII. the destructor bug has been fixed for a while. for your second point, the issue is that D doesn't separate destructors from finalizers and it feels like it was designed by someone with little knowledge in low level memory management honestly.
Re: Are stack+heap classes possible in D?
On 14/06/15 04:31, Adam D. Ruppe wrote: On Sunday, 14 June 2015 at 00:52:20 UTC, FujiBar wrote: I have read that in D structs are always allocated on the stack while classes are always allocated on the heap. That's not true; it is a really common misconception. Putting a struct on the heap is trivial and built into the language: `S* s = new S();` Well Yeah. You would get a reference to a struct. The struct will be on the heap. In that narrow sense, you are right that it is possible. However, this does not behave like a normal struct. In particular, when will the destructor be called? (answer: never, not even before the memory is collected). So, no, I think D experts should avoid telling newbies it is okay to just new struct foo.[1] Shachar 1 - The counter argument is, of course, that struct destructors should not be counted upon to do anything useful anyways, as they are far from guaranteed to run even in situations where one would expect them to. This just relates to another area where D skirts truth in advertising when people say that D supports RAII.
Re: Are stack+heap classes possible in D?
On Wed, 17 Jun 2015 06:02:46 +, WhatMeWorry wrote: I guess the question would be why would one want a struct on the heap and a class on the stack? Performance reasons? struct on the heap: some containers, for example, doing their own memory management. class on the stack: guaranteed destruction when leaving a scope. signature.asc Description: PGP signature
Re: Are stack+heap classes possible in D?
On Sunday, 14 June 2015 at 01:31:25 UTC, Adam D. Ruppe wrote: On Sunday, 14 June 2015 at 00:52:20 UTC, FujiBar wrote: I have read that in D structs are always allocated on the stack while classes are always allocated on the heap. That's not true; it is a really common misconception. Putting a struct on the heap is trivial and built into the language: `S* s = new S();` Putting a class on the stack is a bit trickier, but the standard library provides a helper to do it: http://dlang.org/phobos/std_typecons.html#scoped follow the examples given there. True, but wasn't there a conscientious decision to make structs more amenable to value semantics while classes are more conducive to reference semantics. I guess the question would be why would one want a struct on the heap and a class on the stack? Performance reasons?
Are stack+heap classes possible in D?
I have read that in D structs are always allocated on the stack while classes are always allocated on the heap. Well, I often have classes where I want some instances on the stack, some on the heap. So.. what to do?
Re: Are stack+heap classes possible in D?
On Sunday, 14 June 2015 at 01:31:25 UTC, Adam D. Ruppe wrote: On Sunday, 14 June 2015 at 00:52:20 UTC, FujiBar wrote: I have read that in D structs are always allocated on the stack while classes are always allocated on the heap. That's not true; it is a really common misconception. Putting a struct on the heap is trivial and built into the language: `S* s = new S();` Putting a class on the stack is a bit trickier, but the standard library provides a helper to do it: http://dlang.org/phobos/std_typecons.html#scoped follow the examples given there. Thanks!
Re: Are stack+heap classes possible in D?
On Sunday, 14 June 2015 at 00:52:20 UTC, FujiBar wrote: I have read that in D structs are always allocated on the stack while classes are always allocated on the heap. That's not true; it is a really common misconception. Putting a struct on the heap is trivial and built into the language: `S* s = new S();` Putting a class on the stack is a bit trickier, but the standard library provides a helper to do it: http://dlang.org/phobos/std_typecons.html#scoped follow the examples given there.
Re: Is this possible in D?
On Thursday, 19 February 2015 at 08:24:08 UTC, Jonathan Marler wrote: I am having a heck of a time trying to figure out how to do this. How do I change the attributes of a function based on the version without copying the function body? For example: version(StaticVersion) { static void myLongFunction() { // long body ... } } else { void myLongFunction() { // same long body copied... } } In one version I want the function to be static and in another I don't want it to be static. I cannot figure out how to do this without copy/pasting the entire function body to both versions. Same problem is seen with shared and non-shared class methods as well. I hope there is a good solution for it. If it was C, by using #ifdef #else #endif, just the name line of function could be changed, but I am not sure whether that works with version() expression.
Is this possible in D?
I am having a heck of a time trying to figure out how to do this. How do I change the attributes of a function based on the version without copying the function body? For example: version(StaticVersion) { static void myLongFunction() { // long body ... } } else { void myLongFunction() { // same long body copied... } } In one version I want the function to be static and in another I don't want it to be static. I cannot figure out how to do this without copy/pasting the entire function body to both versions.
Re: Is this possible in D?
On Thursday, 19 February 2015 at 09:38:48 UTC, Mike Parker wrote: On Thursday, 19 February 2015 at 08:24:08 UTC, Jonathan Marler wrote: I am having a heck of a time trying to figure out how to do this. How do I change the attributes of a function based on the version without copying the function body? For example: version(StaticVersion) { static void myLongFunction() { // long body ... } } else { void myLongFunction() { // same long body copied... } } In one version I want the function to be static and in another I don't want it to be static. I cannot figure out how to do this without copy/pasting the entire function body to both versions. You should be able to cobble something together with string mixins. I'm still holding out hope for some sort of .codeof
Re: Is this possible in D?
On Thursday, 19 February 2015 at 08:24:08 UTC, Jonathan Marler wrote: I am having a heck of a time trying to figure out how to do this. How do I change the attributes of a function based on the version without copying the function body? For example: version(StaticVersion) { static void myLongFunction() { // long body ... } } else { void myLongFunction() { // same long body copied... } } In one version I want the function to be static and in another I don't want it to be static. I cannot figure out how to do this without copy/pasting the entire function body to both versions. You should be able to cobble something together with string mixins.
Re: Is this possible in D?
Most practical approach I am currently aware of is wrapping actual implementation (in most restrictive version): class Test { private static void foo_() {} version (Static) { static void foo() { foo_(); } } else { void foo() { foo_(); } } private void bar_() shared { } version (Shared) { void bar() shared { bar_(); } } else { void bar() { (cast(shared Test)this).bar_(); } } } But this relies on very careful code review because of casual casts for certain attributes (static and public/private are easy in that regard)
Re: Is this possible in D?
On Thursday, 19 February 2015 at 10:17:47 UTC, Dicebot wrote: Most practical approach I am currently aware of is wrapping actual implementation (in most restrictive version): I really like mixins for this sort of thing. ``` enum signature = void longFunction(); version( Static ) enum sig = static ~ signature; else alias sig = signature; enum funcBody = q{{ import std.stdio : writeln; writeln( Hi there! ); version( Static ) writeln( I'm static! ); }}; struct Mixed { mixin(sig ~ funcBody); } void main() { version( Static ) Mixed.longFunction(); else { Mixed m; m.longFunction(); } } ``` @OP: By using a token string (q{}) for funcBody rather than a WYSIWYG string (r or ``), you can still get syntax highlighting in your editor.
Re: Is this possible in D?
On Thursday, 19 February 2015 at 17:23:47 UTC, Mike Parker wrote: I agree that string mixins can kill readability. I encountered that when I used them to support both D1 and D2 in Derelict 2 years ago. But I think that when they are kept small and local as in cases like this, they aren't bad at all. Thanks for your example. It's ugly but it's the only solution I've seen that gives me what I'm looking for. I hadn't thought about putting the function body inside a q{ string }
Re: Is this possible in D?
On Thursday, 19 February 2015 at 12:16:18 UTC, Mike Parker wrote: On Thursday, 19 February 2015 at 10:17:47 UTC, Dicebot wrote: Most practical approach I am currently aware of is wrapping actual implementation (in most restrictive version): I really like mixins for this sort of thing. ``` enum signature = void longFunction(); version( Static ) enum sig = static ~ signature; else alias sig = signature; enum funcBody = q{{ import std.stdio : writeln; writeln( Hi there! ); version( Static ) writeln( I'm static! ); }}; struct Mixed { mixin(sig ~ funcBody); } void main() { version( Static ) Mixed.longFunction(); else { Mixed m; m.longFunction(); } } ``` @OP: By using a token string (q{}) for funcBody rather than a WYSIWYG string (r or ``), you can still get syntax highlighting in your editor. Based on your example, bye bye readibility. It is like writing rocket taking off procedures. People are complaining about different parts of D languages some structures (there are good ones, there are bad ones), but unfortunately, bad ones are continuously ignored in the name of trying to reach the language a stability.
Re: Is this possible in D?
On Thu, 19 Feb 2015 08:24:06 +, Jonathan Marler wrote: I am having a heck of a time trying to figure out how to do this. How do I change the attributes of a function based on the version without copying the function body? For example: version(StaticVersion) { static void myLongFunction() { // long body ... } } else { void myLongFunction() { // same long body copied... } } In one version I want the function to be static and in another I don't want it to be static. I cannot figure out how to do this without copy/pasting the entire function body to both versions. you can turn `myLongFunction()` to a template `myLongFunctionImlp()()`, and then simply declare `myLongFunction()` with required attributes, simply instantiating template in it's body. signature.asc Description: PGP signature
Re: Is this possible in D?
On 2/20/2015 1:06 AM, tcak wrote: @OP: By using a token string (q{}) for funcBody rather than a WYSIWYG string (r or ``), you can still get syntax highlighting in your editor. Based on your example, bye bye readibility. It is like writing rocket taking off procedures. People are complaining about different parts of D languages some structures (there are good ones, there are bad ones), but unfortunately, bad ones are continuously ignored in the name of trying to reach the language a stability. I agree that string mixins can kill readability. I encountered that when I used them to support both D1 and D2 in Derelict 2 years ago. But I think that when they are kept small and local as in cases like this, they aren't bad at all.
Re: Using custom attribute for a general parser. Is possible in D ?
On 05/23/2014 09:09 AM, bioinfornatics wrote: struct A { @section( ( word ) = word[0] == '@', ( word ) = word[0] == '\n') int a; } I was able to make that work if I used function instead of delegate: import std.stdio; struct attribute { } alias MatchFunc = bool function( string ); @attribute struct section { MatchFunc start; MatchFunc end; this( in MatchFunc start, in MatchFunc end) { this.start = start, this.end= end; } } struct A { @section((string word) = word[0] == '@', (string word) = word[0] == '\n') int a; } import std.traits; void main() { auto aa = A(); auto t = __traits(getAttributes, A.a); foreach (sect; t) { auto start_result = sect.start(@test); auto end_result = sect.end(\n); writefln(Results: %s and %s, start_result, end_result); } } Ali
Re: Using custom attribute for a general parser. Is possible in D ?
On Monday, 26 May 2014 at 19:50:29 UTC, Ali Çehreli wrote: On 05/23/2014 09:09 AM, bioinfornatics wrote: struct A { @section( ( word ) = word[0] == '@', ( word ) = word[0] == '\n') int a; } I was able to make that work if I used function instead of delegate: import std.stdio; struct attribute { } alias MatchFunc = bool function( string ); @attribute struct section { MatchFunc start; MatchFunc end; this( in MatchFunc start, in MatchFunc end) { this.start = start, this.end= end; } } struct A { @section((string word) = word[0] == '@', (string word) = word[0] == '\n') int a; } import std.traits; void main() { auto aa = A(); auto t = __traits(getAttributes, A.a); foreach (sect; t) { auto start_result = sect.start(@test); auto end_result = sect.end(\n); writefln(Results: %s and %s, start_result, end_result); } } Ali thanks Ali nice works as usual :-) it seem delegate will be suppoted into dmdfe 2.066
Using custom attribute for a general parser. Is possible in D ?
I bask originally this qustion in this thread : http://forum.dlang.org/post/rsnswykpjfzenpliv...@forum.dlang.org but another thread is better as that is not exactly same topic. I would like to use custom annotate/attribute on members as: struct A { @Parser( start = ( word ) = word[0] == '', end = ( word ) = word[0] == '\n' ) string header; } but it seem we can't use curstom annotation in D and mxin in this case are not easy. This custom attribute is a metadata to explain how to retrieve this field from a file. This will aloow to create a global parser and give to this parrser data structure to get back. Parser could to b an input range were hront give currentfilled struct and popfront will look foward in buffer given by file.byChunk() using this metadata. When all field are filled give the instance to front …