Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 31, 2019 Is there any writeln like functions without GC? | ||||
---|---|---|---|---|
| ||||
Hi: why writeln need GC? |
October 31, 2019 Re: Is there any writeln like functions without GC? | ||||
---|---|---|---|---|
| ||||
Posted in reply to lili | 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); } |
October 31, 2019 Re: Is there any writeln like functions without GC? | ||||
---|---|---|---|---|
| ||||
Posted in reply to lili | 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.
|
October 31, 2019 Re: Is there any writeln like functions without GC? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 |
October 31, 2019 Re: Is there any writeln like functions without GC? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ferhat Kurtulmuş | 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.
|
November 02, 2019 Re: Is there any writeln like functions without GC? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ferhat Kurtulmuş | 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
|
November 02, 2019 Re: Is there any writeln like functions without GC? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bachmeier | 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.
|
November 03, 2019 Re: Is there any writeln like functions without GC? | ||||
---|---|---|---|---|
| ||||
Posted in reply to lili | 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 |
November 09, 2019 Re: Is there any writeln like functions without GC? | ||||
---|---|---|---|---|
| ||||
Posted in reply to lili | 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 |
November 10, 2019 Re: Is there any writeln like functions without GC? | ||||
---|---|---|---|---|
| ||||
Posted in reply to 9il | 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
|
Copyright © 1999-2021 by the D Language Foundation