www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there any writeln like functions without GC?

reply lili <akozhao tencent.com> writes:
Hi:
    why writeln need GC?
Oct 30 2019
next sibling parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Thursday, 31 October 2019 at 03:56:56 UTC, lili wrote:
 Hi:
    why writeln need GC?
I cannot answer why it needs GC but something can be implemented like: import core.stdc.stdio; struct Point { int x; int y; } class Person { string name; uint age; } template GenStructMemberPrint(string structInstanceName, string memberName){ const char[] GenStructMemberPrint = "printf(\"%d\", " ~structInstanceName ~ "." ~ memberName ~ ");"; // static ifs can be used to use proper formats depending on the typeof struct member } // entire thing can be implemented using sprintf to obtain a nice formatted line void writeln2(A...)(A arguments) nogc nothrow { foreach (a; arguments) { static if (is(typeof(a) == class) || is(typeof(a) == interface)) { printf("%s \n", typeof(a).stringof.ptr); } else static if (is(typeof(a) == string)) { printf("%s \n", a.ptr); } else static if (is(typeof(a) == struct)){ foreach (member; __traits(allMembers, typeof(a))) { //writeln(member); mixin(GenStructMemberPrint!(a.stringof, member)); // this needs some improvements to imitate writeln } } else { static assert( "non-supported type!"); } } } void main() { auto pt = Point(10, 20); auto per = new Person(); // nogc custom allocator can be used here writeln2("ssss", per, pt); }
Oct 31 2019
prev sibling next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 31 October 2019 at 03:56:56 UTC, lili wrote:
 Hi:
    why writeln need GC?
It almost never does, it just keeps the option open in case * it needs to throw an exception (like if stdout is closed) * you pass it a custom type with toString that uses GC nogc is just super strict and doesn't even allow for rare cases.
Oct 31 2019
parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Thursday, 31 October 2019 at 13:46:07 UTC, Adam D. Ruppe wrote:
 On Thursday, 31 October 2019 at 03:56:56 UTC, lili wrote:
 Hi:
    why writeln need GC?
It almost never does, it just keeps the option open in case * it needs to throw an exception (like if stdout is closed) * you pass it a custom type with toString that uses GC nogc is just super strict and doesn't even allow for rare cases.
It would be nice if one reimplement writeln of Phobos by bypassing gc and use a custom nogc exception as described here*? Of course I can imagine that it would be a breaking change in the language and requires so much work for it to be compatible with other std modules/language features. *: https://www.auburnsounds.com/blog/2016-11-10_Running-D-without-its-runtime.html
Oct 31 2019
next sibling parent reply bachmeier <no spam.net> writes:
On Thursday, 31 October 2019 at 15:11:42 UTC, Ferhat Kurtulmuş 
wrote:

 It would be nice if one reimplement writeln of Phobos by 
 bypassing gc and use a custom nogc exception as described 
 here*? Of course I can imagine that it would be a breaking 
 change in the language and requires so much work for it to be 
 compatible with other std modules/language features.

 *: 
 https://www.auburnsounds.com/blog/2016-11-10_Running-D-without-its-runtime.html
I can't imagine any possibility we'll ever see a breaking change to writeln. It would have to be an addition to Phobos.
Oct 31 2019
parent Seb <seb wilzba.ch> writes:
On Thursday, 31 October 2019 at 16:03:22 UTC, bachmeier wrote:
 On Thursday, 31 October 2019 at 15:11:42 UTC, Ferhat Kurtulmuş 
 wrote:

 It would be nice if one reimplement writeln of Phobos by 
 bypassing gc and use a custom nogc exception as described 
 here*? Of course I can imagine that it would be a breaking 
 change in the language and requires so much work for it to be 
 compatible with other std modules/language features.

 *: 
 https://www.auburnsounds.com/blog/2016-11-10_Running-D-without-its-runtime.html
I can't imagine any possibility we'll ever see a breaking change to writeln. It would have to be an addition to Phobos.
Yep or Phobos 2 which is more realistic at this point.
Nov 02 2019
prev sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Thursday, October 31, 2019 9:11:42 AM MDT Ferhat Kurtulmuş via 
Digitalmars-d-learn wrote:
 On Thursday, 31 October 2019 at 13:46:07 UTC, Adam D. Ruppe wrote:
 On Thursday, 31 October 2019 at 03:56:56 UTC, lili wrote:
 Hi:
    why writeln need GC?
It almost never does, it just keeps the option open in case * it needs to throw an exception (like if stdout is closed) * you pass it a custom type with toString that uses GC nogc is just super strict and doesn't even allow for rare cases.
It would be nice if one reimplement writeln of Phobos by bypassing gc and use a custom nogc exception as described here*? Of course I can imagine that it would be a breaking change in the language and requires so much work for it to be compatible with other std modules/language features. *: https://www.auburnsounds.com/blog/2016-11-10_Running-D-without-its-runtime .html
You can always just use printf. - Jonathan M Davis
Nov 02 2019
prev sibling next sibling parent reply 9il <ilyayaroshenko gmail.com> writes:
On Thursday, 31 October 2019 at 03:56:56 UTC, lili wrote:
 Hi:
    why writeln need GC?
See also Mir's nogc formatting module https://github.com/libmir/mir-runtime/blob/master/source/mir/format.d
Nov 02 2019
parent reply dangbinghoo <dangbinghoo gmail.com> writes:
On Sunday, 3 November 2019 at 05:46:53 UTC, 9il wrote:
 On Thursday, 31 October 2019 at 03:56:56 UTC, lili wrote:
 Hi:
    why writeln need GC?
See also Mir's nogc formatting module https://github.com/libmir/mir-runtime/blob/master/source/mir/format.d
hi, is mir right now fully implemented using betterC? thanks! -- binghoo
Nov 09 2019
parent 9il <ilyayaroshenko gmail.com> writes:
On Sunday, 10 November 2019 at 07:57:38 UTC, dangbinghoo wrote:
 On Sunday, 3 November 2019 at 05:46:53 UTC, 9il wrote:
 On Thursday, 31 October 2019 at 03:56:56 UTC, lili wrote:
 Hi:
    why writeln need GC?
See also Mir's nogc formatting module https://github.com/libmir/mir-runtime/blob/master/source/mir/format.d
hi, is mir right now fully implemented using betterC? thanks! -- binghoo
Nope, but you can write a lot of things that would not require to link with DRuntime. The betterC flags also means that you can't use DRuntime during compilation, which makes D generics almost useless.
Nov 13 2019
prev sibling next sibling parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Thursday, 31 October 2019 at 03:56:56 UTC, lili wrote:
 Hi:
    why writeln need GC?
Upon this post, I thought writing a gc-free writeln would be a good learning practice. Although it is not a feature-complete one, it was a lot of fun to do it :) https://github.com/aferust/stringnogc
Nov 09 2019
parent reply bauss <jj_1337 live.dk> writes:
On Saturday, 9 November 2019 at 22:03:03 UTC, Ferhat Kurtulmuş 
wrote:
 On Thursday, 31 October 2019 at 03:56:56 UTC, lili wrote:
 Hi:
    why writeln need GC?
Upon this post, I thought writing a gc-free writeln would be a good learning practice. Although it is not a feature-complete one, it was a lot of fun to do it :) https://github.com/aferust/stringnogc
If you wanted to follow the standard of D then you didn't need a string type. Since it doesn't really exist in D. string is just an alias for immutable(char)[] So what you want is to use this: https://dlang.org/phobos/std_container_array.html
Nov 11 2019
parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Monday, 11 November 2019 at 16:20:59 UTC, bauss wrote:

 If you wanted to follow the standard of D then you didn't need 
 a string type. Since it doesn't really exist in D.

 string is just an alias for immutable(char)[]
And that is why std.exception.assumeUnique converts char[] to string AKA immutable(char)[].
Nov 11 2019
prev sibling parent Ogi <ogion.art gmail.com> writes:
If your goal is to debug your  nogc code, you can use writeln in 
debug statement:

 nogc void main() {
     debug writeln("hello, debug world!");
}
Nov 12 2019