Re: D as a Better C

2017-09-07 Thread twkrimm via Digitalmars-d-announce

On Thursday, 31 August 2017 at 13:17:36 UTC, Claude wrote:
I think "betterC" can be a good tool to use D on embedded 
systems, keep as few dependencies as possible, a low ROM 
footprint and a good C interoperability.


I'll try to find some time to play with it.


I agree, embedded systems is one of the reasons I think betterC 
is a good idea.





Re: D as a Better C

2017-09-07 Thread Kagamin via Digitalmars-d-announce

On Monday, 28 August 2017 at 22:45:01 UTC, Parke wrote:

When I write "hello world" in C, the executable is 8,519 bytes.
 When I write "hello world" in D, the executable is 100 times 
larger: 865,179 bytes.


Interestingly, "hello world" in C, compiled statically, yields 
908,608 bytes.  And "hello world" in assembly yields 368 bytes.


https://forum.dlang.org/post/tmofjecvnqdthvete...@forum.dlang.org 
- an example for elf target.


Re: D as a Better C

2017-09-01 Thread Azi Hassan via Digitalmars-d-announce

On Wednesday, 30 August 2017 at 22:48:45 UTC, Adam D. Ruppe wrote:

On Wednesday, 30 August 2017 at 22:22:23 UTC, Azi Hassan wrote:

extern(C) int main(int argc, char*[] argv, char*[] env)


That's a D array of pointers. A D array is larger than a C 
"array" argument, thus you're skipping past it.


The correct declaration is (int argc, char** argv, char** env).

(I'd argue that is the correct way to write it in C too, that's 
how I always do and it works there. But I just loathe C's 
arrays anyway.)


Brilliant, thanks ! I just re-read the blog post and it uses the 
char** syntax too, but I didn't pay attention to it.


Re: D as a Better C

2017-08-31 Thread Claude via Digitalmars-d-announce
I think "betterC" can be a good tool to use D on embedded 
systems, keep as few dependencies as possible, a low ROM 
footprint and a good C interoperability.


I'll try to find some time to play with it.


Re: D as a Better C

2017-08-30 Thread Adam D. Ruppe via Digitalmars-d-announce

On Wednesday, 30 August 2017 at 22:22:23 UTC, Azi Hassan wrote:

extern(C) int main(int argc, char*[] argv, char*[] env)


That's a D array of pointers. A D array is larger than a C 
"array" argument, thus you're skipping past it.


The correct declaration is (int argc, char** argv, char** env).

(I'd argue that is the correct way to write it in C too, that's 
how I always do and it works there. But I just loathe C's arrays 
anyway.)


Re: D as a Better C

2017-08-30 Thread Azi Hassan via Digitalmars-d-announce

On Wednesday, 30 August 2017 at 22:22:23 UTC, Azi Hassan wrote:
How should command-line arguments be used in better C ? Looping 
through argv seems to print environment variables :


import core.stdc.stdio;

extern(C) int main(int argc, char*[] argv, char*[] env)
{
foreach(i; 0 .. argc)
printf("arg %d: %s\n", i, argv[i]);
return 0;
}


Compiling with v2.076.0-b2-dirty on Lubuntu 14.04 and running 
with one argument outputs the following :


arg 0: XDG_VTNR=7
arg 1: LC_PAPER=fr_FR.UTF-8


Forgot to mention, char*[] env was only for testing purposes. The 
code still produces the same output with char*[] env omitted.


Re: D as a Better C

2017-08-30 Thread Azi Hassan via Digitalmars-d-announce
How should command-line arguments be used in better C ? Looping 
through argv seems to print environment variables :


import core.stdc.stdio;

extern(C) int main(int argc, char*[] argv, char*[] env)
{
foreach(i; 0 .. argc)
printf("arg %d: %s\n", i, argv[i]);
return 0;
}


Compiling with v2.076.0-b2-dirty on Lubuntu 14.04 and running 
with one argument outputs the following :


arg 0: XDG_VTNR=7
arg 1: LC_PAPER=fr_FR.UTF-8


Re: D as a Better C

2017-08-30 Thread Kagamin via Digitalmars-d-announce
On Wednesday, 30 August 2017 at 02:19:21 UTC, Michael V. Franklin 
wrote:
As you can see it is not a polished experience and gets much 
worse when you start employing more features of D.  This could 
be improved, and in fact, with GDC you need even less useless 
boilerplate in object.d and may end up with an even smaller 
executable. (Maybe I'll follow up later with GDC illustration.  
Right now I don't have a computer with the latest GDC 
installed).
 If you try this experiment with LDC, you may end up with a 
multi-gigabyte file and crash your PC due to 
https://github.com/ldc-developers/ldc/issues/781


I use stock object.d, just don't link it.


Re: D as a Better C

2017-08-30 Thread Kagamin via Digitalmars-d-announce

On Wednesday, 30 August 2017 at 00:29:19 UTC, Parke wrote:

But my original question was about what you (Kagamin) called
"intermediate D".  I was trying to understand what 
"intermediate D"
is, and whether or not I could use "intermediate D" (whatever 
it is)

to produce small(er) executables.


I rely on llvm tooling to remove unused stuff, build process is a 
bit lengthy, I didn't write a step by step guide for it. Also 
linux might need different approach depending what code compiler 
generates.


Re: D as a Better C

2017-08-29 Thread Parke via Digitalmars-d-announce
On Tue, Aug 29, 2017 at 7:19 PM, Michael V. Franklin via
Digitalmars-d-announce  wrote:
> For example, the following is the most minimal "Hello World" I can make with
> D that does not require the -betterC switch, and does not use the official D
> runtime.  Instead the runtime features required by this program are
> implemented in object.d.

Thank you for the very helpful example and explanation.

-Parke


Re: D as a Better C

2017-08-29 Thread Michael V. Franklin via Digitalmars-d-announce

On Wednesday, 30 August 2017 at 00:29:19 UTC, Parke wrote:


But my original question was about what you (Kagamin) called
"intermediate D".  I was trying to understand what 
"intermediate D"
is, and whether or not I could use "intermediate D" (whatever 
it is)

to produce small(er) executables.


"Intermediate D" probably refers to not use the -betterC switch, 
not linking in the official druntime, and instead implementing 
the features of D required by your program yourself.


For example, the following is the most minimal "Hello World" I 
can make with D that does not require the -betterC switch, and 
does not use the official D runtime.  Instead the runtime 
features required by this program are implemented in object.d.


object.d

module object;

alias immutable(char)[] string;

struct ModuleInfo { }

class Object { }

class TypeInfo
{
bool equals(in void* p1, in void* p2) const
{
return p1 == p2;
}

int compare(in void* p1, in void* p2) const
{
return _xopCmp(p1, p2);
}
}

class TypeInfo_Class : TypeInfo
{
ubyte[136] ignore;
}

alias TypeInfo_Class ClassInfo;

class TypeInfo_Struct : TypeInfo
{
ubyte[120] ignore;
}

extern (C) Object _d_newclass(const ClassInfo ci)
{
return null;
}

extern(C) void _d_throwc(Object h) { }

class Throwable { }

class Error : Throwable
{
this(string x)
{ }
}

extern(C) void _d_throwdwarf(Throwable o) { }

extern(C) void _d_dso_registry(void* data) { }


// The following code basically replaces the C runtime
//
extern extern(C) int main(int argc, char** argv);

extern(C) void sys_exit(long arg1)
{
asm
{
mov RAX, 60;
mov RDI, arg1;
syscall;
}
}

extern(C) void _start()
{
auto ret = main(0, null);
sys_exit(ret);
}

private alias extern(C) int function(char[][] args) MainFunc;

extern (C) int _d_run_main(int argc, char **argv, MainFunc 
mainFunc)

{
// ignore args for now
return mainFunc(null);
}

main.d
--
module main;

long sys_write(long arg1, in void* arg2, long arg3)
{
long result;

asm
{
mov RAX, 1;
mov RDI, arg1;
mov RSI, arg2;
mov RDX, arg3;
syscall;
}

return result;
}

void write(in string text)
{
sys_write(2, text.ptr, text.length);
}

void main()
{
write("Hello\n");
}

Building and executing
--
On Linux 64-bit compile with:
$ dmd -c -fPIC -release object.d main.d -of=main.o

Link with:
$ ld main.o -o main

Report size:
$ size main
   textdata bss dec hex filename
   10701872   82950 b86 main

Text execution:
$ ./main
Hello

If you link with:
$ ld main.o -o main --gc-sections

You end up with:
$ size main
   textdata bss dec hex filename
9501688   02638 a4e main


This illustration does not require -betterC, but instead requires 
you to implement a "minimal D runtime" specific to your program, 
and the features of D that it employs.  In this illustration that 
"minimal D runtime" is object.d.


As you can see it is not a polished experience and gets much 
worse when you start employing more features of D.  This could be 
improved, and in fact, with GDC you need even less useless 
boilerplate in object.d and may end up with an even smaller 
executable. (Maybe I'll follow up later with GDC illustration.  
Right now I don't have a computer with the latest GDC installed). 
 If you try this experiment with LDC, you may end up with a 
multi-gigabyte file and crash your PC due to 
https://github.com/ldc-developers/ldc/issues/781


Hopefully we can improve the compiler/runtime implementation so 
doing this kind of programming won't require so many useless 
stubs, and users can implement just the features of D that they 
need for their program without having to rely on the on the blunt 
and heavy hand of -betterC.


Mike



Re: D as a Better C

2017-08-29 Thread Adam D. Ruppe via Digitalmars-d-announce

On Wednesday, 30 August 2017 at 00:29:19 UTC, Parke wrote:
The above D code yields 445,187 bytes when compiled with 
-release -betterC.

DMD64 D Compiler 2.075.0-b2 on Linux on x86-64.


-betterC does virtually nothing on that version of dmd...


But my original question was about what you (Kagamin) called
"intermediate D".  I was trying to understand what 
"intermediate D"
is, and whether or not I could use "intermediate D" (whatever 
it is)

to produce small(er) executables.


Regular D with a custom runtime library. You can get as small as 
3 KB on Linux (though that is a super bare bones hello world).


But note that if you are distributing several executables you 
might also just use the shared phobos lib too with 
-defaultlib=libphobos2.so on Linux.


Re: D as a Better C

2017-08-29 Thread Parke via Digitalmars-d-announce
> On Monday, 28 August 2017 at 22:45:01 UTC, Parke wrote:
>> When I write "hello world" in C, the executable is 8,519 bytes.
>>  When I write "hello world" in D, the executable is 100 times larger:
>> 865,179 bytes.


On Tue, Aug 29, 2017 at 8:26 AM, Kagamin via Digitalmars-d-announce
<digitalmars-d-announce@puremagic.com> wrote:
> You mean the examples from the blog post
> https://dlang.org/blog/2017/08/23/d-as-a-better-c/ give you 800kb
> executables?


No, I was talking about the below version of "hello world" that I
compiled several weeks prior to reading the blog post.

import std.stdio;
void main() { writeln("Hello, world!"); }

The above D code yields 865,179 bytes.


Below is the version from the blog post:

import core.stdc.stdio;
extern (C) int main( int argc, char** argv ) {
printf ( "hello world\n" );
return 0;
}

The above D code yields 445,244 bytes when compiled with -release.
The above D code yields 445,187 bytes when compiled with -release -betterC.
DMD64 D Compiler 2.075.0-b2 on Linux on x86-64.

Still 50 times larger than C.  Perhaps it would be smaller with a
newer version of DMD.

But my original question was about what you (Kagamin) called
"intermediate D".  I was trying to understand what "intermediate D"
is, and whether or not I could use "intermediate D" (whatever it is)
to produce small(er) executables.

-Parke


Re: D as a Better C

2017-08-29 Thread Kagamin via Digitalmars-d-announce

On Monday, 28 August 2017 at 22:45:01 UTC, Parke wrote:

When I write "hello world" in C, the executable is 8,519 bytes.
 When I write "hello world" in D, the executable is 100 times 
larger: 865,179 bytes.


You mean the examples from the blog post 
https://dlang.org/blog/2017/08/23/d-as-a-better-c/ give you 800kb 
executables?


Re: D as a Better C

2017-08-28 Thread Parke via Digitalmars-d-announce
> On Friday, 25 August 2017 at 18:08:06 UTC, Parke wrote:
>> Is there any documentation on how to access and use the minimal runtime?

On Mon, Aug 28, 2017 at 5:22 AM, Kagamin via Digitalmars-d-announce
 wrote:
> Runtime implements language features like boundschecking, it's not used
> explicitly in the code.

I understand that.

On Wed, Aug 23, 2017 at 10:17 AM, Kagamin via Digitalmars-d-announce
> 7.5kb totext.exe (encodes stdin to base64 and writes to stdout) - wrote it
> to put images in xml for opensearch descriptions.
> 12.5kb retab.exe (retabifies source code with various features)
> 5.5kb keepower.exe (manages screen saver and power settings because of
> obnoxious domain policy)
> 14.5kb fsum.exe (computes various hash sums of a file)

Is the source code available for totext.exe, retab.exe keepower.exe,
and fsum.exe?

If the source code were available, I could try compiling them and see
if I could reproduce the nice, small executable sizes you list above.

When I write "hello world" in C, the executable is 8,519 bytes.  When
I write "hello world" in D, the executable is 100 times larger:
865,179 bytes.

Interestingly, "hello world" in C, compiled statically, yields 908,608
bytes.  And "hello world" in assembly yields 368 bytes.

Thanks,

Parke


Re: D as a Better C

2017-08-28 Thread Kagamin via Digitalmars-d-announce

On Friday, 25 August 2017 at 18:08:06 UTC, Parke wrote:
Is there any documentation on how to access and use the minimal 
runtime?


Runtime implements language features like boundschecking, it's 
not used explicitly in the code.


Re: D as a Better C

2017-08-25 Thread Michael V. Franklin via Digitalmars-d-announce

On Friday, 25 August 2017 at 23:13:53 UTC, Mengu wrote:
On Friday, 25 August 2017 at 00:24:14 UTC, Michael V. Franklin 
wrote:
On Thursday, 24 August 2017 at 19:21:31 UTC, Walter Bright 
wrote:

[...]


Great! I look forward to seeing improvements and hope to help.

[...]


i believe that should be an opt-out. what about newcomers? will 
they have to learn how to link std lib?


No, because the dmd.conf that is delivered with the toolchain is 
already ready to go with reasonable defaults; and not hard-coded 
into the compiler.  Advanced users can update dmd.conf or point 
the compiler to a different dmd.conf as they choose.


Mike


Re: D as a Better C

2017-08-25 Thread Mengu via Digitalmars-d-announce
On Friday, 25 August 2017 at 00:24:14 UTC, Michael V. Franklin 
wrote:
On Thursday, 24 August 2017 at 19:21:31 UTC, Walter Bright 
wrote:

[...]


Great! I look forward to seeing improvements and hope to help.

[...]


i believe that should be an opt-out. what about newcomers? will 
they have to learn how to link std lib?


Re: D as a Better C

2017-08-25 Thread Jolly James via Digitalmars-d-announce

On Friday, 25 August 2017 at 15:29:54 UTC, Swoorup Joshi wrote:

On Friday, 25 August 2017 at 08:54:02 UTC, Kagamin wrote:

On Thursday, 24 August 2017 at 19:09:58 UTC, Parke wrote:

What is "intermediate D"?


D with minimal runtime.


5 years later...

D - BetterC++ (no gc)
D - BetterJava (full on gc + other goodie tissue)
D - BetterRust (full interoperability with rust)

Forgive my cynicism


D - BetterC# (no simple and easy-to-use generics, complex 
templates instead)
Phobos - Better.NET (pointless names, weird structure, outdated 
stuff)
DCD - BetterIntelliSense (tends to always not find about ~10 of 
all real possibilities, is rather "patched into" IDEs/editors 
than integrated)


Re: D as a Better C

2017-08-25 Thread Parke via Digitalmars-d-announce
> On Thursday, 24 August 2017 at 19:09:58 UTC, Parke wrote:
>> What is "intermediate D"?

On Fri, Aug 25, 2017 at 1:54 AM, Kagamin via Digitalmars-d-announce
 wrote:
> D with minimal runtime.

Is there any documentation on how to access and use the minimal runtime?

-Parke


Re: D as a Better C

2017-08-25 Thread Swoorup Joshi via Digitalmars-d-announce

On Friday, 25 August 2017 at 08:54:02 UTC, Kagamin wrote:

On Thursday, 24 August 2017 at 19:09:58 UTC, Parke wrote:

What is "intermediate D"?


D with minimal runtime.


5 years later...

D - BetterC++ (no gc)
D - BetterJava (full on gc + other goodie tissue)
D - BetterRust (full interoperability with rust)

Forgive my cynicism


Re: D as a Better C

2017-08-25 Thread Basile B. via Digitalmars-d-announce

On Friday, 25 August 2017 at 10:01:25 UTC, Basile B. wrote:

On Friday, 25 August 2017 at 09:50:39 UTC, Suliman wrote:

On Friday, 25 August 2017 at 08:54:02 UTC, Kagamin wrote:

On Thursday, 24 August 2017 at 19:09:58 UTC, Parke wrote:

What is "intermediate D"?


D with minimal runtime.


How to pass to dub -betterC flag?


{
...
"dflags" : ["betterC"],
...
}


oops, add the hyphen before, so

{
...
"dflags" : ["-betterC"],
...
}





Re: D as a Better C

2017-08-25 Thread Basile B. via Digitalmars-d-announce

On Friday, 25 August 2017 at 09:50:39 UTC, Suliman wrote:

On Friday, 25 August 2017 at 08:54:02 UTC, Kagamin wrote:

On Thursday, 24 August 2017 at 19:09:58 UTC, Parke wrote:

What is "intermediate D"?


D with minimal runtime.


How to pass to dub -betterC flag?


{
...
"dflags" : ["betterC"],
...
}


Re: D as a Better C

2017-08-25 Thread Suliman via Digitalmars-d-announce

On Friday, 25 August 2017 at 08:54:02 UTC, Kagamin wrote:

On Thursday, 24 August 2017 at 19:09:58 UTC, Parke wrote:

What is "intermediate D"?


D with minimal runtime.


How to pass to dub -betterC flag?


Re: D as a Better C

2017-08-25 Thread Kagamin via Digitalmars-d-announce

On Thursday, 24 August 2017 at 19:09:58 UTC, Parke wrote:

What is "intermediate D"?


D with minimal runtime.


Re: D as a Better C

2017-08-24 Thread Michael V. Franklin via Digitalmars-d-announce

On Thursday, 24 August 2017 at 19:21:31 UTC, Walter Bright wrote:

On 8/24/2017 11:56 AM, Walter Bright wrote:
I find -betterC to be somewhat of a copout for avoiding the 
hard work of improving D's implementation.


On the contrary, I view it as providing motivation for dealing 
with those issues. The PR above is stalled for lack of 
motivation.


-betterC also brings into sharp focus exactly what the issues 
are.


Great! I look forward to seeing improvements and hope to help.

Allow me to point out a recent pull request that should have 
resulted in an improvement in the full-featured D implementation 
rather than the -betterC implementation.


https://github.com/dlang/dmd/pull/6918

DMD should never link in Phobos or druntime automatically. 
Rather, I think such dependencies should be specified on a 
platform-by-platform basis using a dmd.conf, linker script, or 
some other configuration file that is distributed with the 
toolchain's package.


This puts the power in the hands of the user to avoid linking in 
Phobos and druntime without having to use the -betterC switch 
which is especially useful if the user is providing their own 
minimal runtime implementation to support features of D that are 
excluded with the heavy hand of -betterC.


Mike


Re: D as a Better C

2017-08-24 Thread Michael V. Franklin via Digitalmars-d-announce

On Thursday, 24 August 2017 at 18:26:37 UTC, H. S. Teoh wrote:

For instance, a D project targeting STM board, makes heavy use 
of classes and templates, resultant code segment is 3k.


https://github.com/JinShil/stm32f42_discovery_demo#the-good


To be fair, though, the above-mentioned project did have to 
create a stub druntime in order to get things to work.  Not 
everyone may have the know-how required to construct a minimal 
druntime that works for their purposes.


Those runtime stubs are needed precisely because of problems in 
D's implementation.  If the implementation were fixed, none of 
those stubs would be required, and neither would the -betterC 
switch.


Because the project above is not using any feature provided by 
those runtime stubs, those stubs should not be required, and 
neither should the -betterC switch.


GDC has made some improvements here, and that is why the project 
above only compiles with GDC.


LDC doesn't even display an error message when those stubs aren't 
created.  Instead it enters a codegen loop generating a 
gargantuan multi-gigabyte file, ultimately crashing my VM 
(https://github.com/ldc-developers/ldc/issues/781).


Sometimes, however, it is not known whether a runtime feature 
will be needed until link-time.  In that case, it's OK for the 
compiler to emit TypeInfo, ModuleInfo, etc..., but it should do 
so in a way that the linker (with either LTO or --gc-sections) 
can determine what is needed and what isn't and discard that 
which isn't needed.  Once unneeded object code is discarded, the 
linker errors disappear, and you get a functioning executable 
without linking in the runtime and without a -betterC switch.


GDC recently implemented such an improvement 
(https://github.com/D-Programming-GDC/GDC/pull/505).  It brought 
my binary size from 600kB to 6KB, so now I can get back to 
microcontroller programming in D.  This is the kind of work 
that's needed.


Mike





Re: D as a Better C

2017-08-24 Thread jmh530 via Digitalmars-d-announce

On Thursday, 24 August 2017 at 18:56:25 UTC, Walter Bright wrote:


There is a PR to make it only on demand,

  https://github.com/dlang/dmd/pull/6561

but it is mired in problems that are not in the D test suite 
and for which no test cases exist.


C++ compilers also have a switch, like -fno-rtti, for 
de-activating RTTI. BetterC seems like a combination of several 
pieces of underlying functionality. There is not yet any ability 
to have any kind of granularity. For instance,

-betterC=[flag]
where [flag] might be something like "off-dassert" which calls 
the C assert function instead of the D one.


Re: D as a Better C

2017-08-24 Thread Walter Bright via Digitalmars-d-announce

On 8/24/2017 11:56 AM, Walter Bright wrote:
I find -betterC to be somewhat of a copout for avoiding the hard work of 
improving D's implementation.


On the contrary, I view it as providing motivation for dealing with those 
issues. The PR above is stalled for lack of motivation.


-betterC also brings into sharp focus exactly what the issues are.


Re: D as a Better C

2017-08-24 Thread Parke via Digitalmars-d-announce
On Wed, Aug 23, 2017 at 10:17 AM, Kagamin via Digitalmars-d-announce
<digitalmars-d-announce@puremagic.com> wrote:
> Not a better C, but intermediate D has small footprint for me too.

What is "intermediate D"?

-Parke

> 7.5kb totext.exe (encodes stdin to base64 and writes to stdout) - wrote it
> to put images in xml for opensearch descriptions.
> 12.5kb retab.exe (retabifies source code with various features)
> 5.5kb keepower.exe (manages screen saver and power settings because of
> obnoxious domain policy)
> 14.5kb fsum.exe (computes various hash sums of a file)
>
> Additional features: string switch, array cast. Also how assert failure
> works in C? Mine shows a nice formatted message.


Re: D as a Better C

2017-08-24 Thread 12345swordy via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 13:12:04 UTC, Mike Parker wrote:
To coincide with the improvements to -betterC in the upcoming 
DMD 2.076, Walter has published a new article on the D blog 
about what it is and why to use it. A fun read. And I'm 
personally happy to see the love this feature is getting. I 
have a project I'd like to use it with if I can ever make the 
time for it!


The blog:

https://dlang.org/blog/2017/08/23/d-as-a-better-c/

Reddit:
https://www.reddit.com/r/programming/comments/6viswu/d_as_a_better_c/


Questions regarding c++ classes in betterC mode. Can the c++ 
class destructor be called by the destroy function or do I have 
to call it explicitly like p-<~class()?


Alex


Re: D as a Better C

2017-08-24 Thread Walter Bright via Digitalmars-d-announce

On 8/23/2017 5:35 PM, Michael V. Franklin wrote:
Consider this:  Rust doesn't need a special switch to make it interoperable with 
C.  What's wrong with D's implementation that requires such things?  Granted, D 
is not Rust, but D's implementation could be improved to make it more 
competitive with Rust in these use cases.  For example, there is really no need 
for TypeInfo if you're not doing any dynanmic casts, but the current 
implementation generates it regardless.


There is a PR to make it only on demand,

  https://github.com/dlang/dmd/pull/6561

but it is mired in problems that are not in the D test suite and for which no 
test cases exist.


I find -betterC to be somewhat of a 
copout for avoiding the hard work of improving D's implementation.


On the contrary, I view it as providing motivation for dealing with those 
issues. The PR above is stalled for lack of motivation.


---

Another issue is asserts. -betterC redirects them to C's assert. Perhaps we 
should abandon D's asserts? -betterC provides motivation to examine that.


Re: D as a Better C

2017-08-24 Thread H. S. Teoh via Digitalmars-d-announce
On Thu, Aug 24, 2017 at 08:13:29PM +0200, Iain Buclaw via 
Digitalmars-d-announce wrote:
[...]
> The GDC camp concurs with the sentiment of betterC being a waste of
> time.  My particular stance on the matter is that it should not be an
> all or nothing switch, granular control is fine.  The compiler should
> (and can!) produce a very small footprint whilst using the expressive
> richness of the language.
> 
> For instance, a D project targeting STM board, makes heavy use of
> classes and templates, resultant code segment is 3k.
> 
> https://github.com/JinShil/stm32f42_discovery_demo#the-good
> 
> I quote the author here that when building the project, there is:
> 
> """
> No Stinking -betterC. If you don't want the overhead of a certain
> feature of D, don't use it. -betterC is just a synonymn for -worseD.
> """

To be fair, though, the above-mentioned project did have to create a
stub druntime in order to get things to work.  Not everyone may have the
know-how required to construct a minimal druntime that works for their
purposes.


T

-- 
Those who've learned LaTeX swear by it. Those who are learning LaTeX swear at 
it. -- Pete Bleackley


Re: D as a Better C

2017-08-24 Thread Iain Buclaw via Digitalmars-d-announce
On 23 August 2017 at 19:44, Jonathan M Davis via
Digitalmars-d-announce <digitalmars-d-announce@puremagic.com> wrote:
> On Wednesday, August 23, 2017 13:12:04 Mike Parker via Digitalmars-d-
> announce wrote:
>> To coincide with the improvements to -betterC in the upcoming DMD
>> 2.076, Walter has published a new article on the D blog about
>> what it is and why to use it. A fun read. And I'm personally
>> happy to see the love this feature is getting. I have a project
>> I'd like to use it with if I can ever make the time for it!
>>
>> The blog:
>>
>> https://dlang.org/blog/2017/08/23/d-as-a-better-c/
>>
>> Reddit:
>> https://www.reddit.com/r/programming/comments/6viswu/d_as_a_better_c/
>
> I confess that I tend to think of betterC as a waste of time. Clearly, there
> are folks who find it useful, but it loses so much that I see no point in
> using it for anything unless I have no choice. As long as attempts to
> improve it don't negatively impact normal D, then I don't really care what
> happens with it, but it's clearly not for me.
>
> And it _is_ possible to use full-featured D from C/C++ when D does not
> control main. It's just more of a pain.
>

It's getting better, there are certainly some tough topics that need
to be addressed in the compiler implementation.

The GDC camp concurs with the sentiment of betterC being a waste of
time.  My particular stance on the matter is that it should not be an
all or nothing switch, granular control is fine.  The compiler should
(and can!) produce a very small footprint whilst using the expressive
richness of the language.

For instance, a D project targeting STM board, makes heavy use of
classes and templates, resultant code segment is 3k.

https://github.com/JinShil/stm32f42_discovery_demo#the-good

I quote the author here that when building the project, there is:

"""
No Stinking -betterC. If you don't want the overhead of a certain
feature of D, don't use it. -betterC is just a synonymn for -worseD.
"""


Re: D as a Better C

2017-08-24 Thread twkrimm via Digitalmars-d-announce

On Thursday, 24 August 2017 at 03:31:02 UTC, Swoorup Joshi wrote:
On Wednesday, 23 August 2017 at 17:44:31 UTC, Jonathan M Davis 
wrote:
On Wednesday, August 23, 2017 13:12:04 Mike Parker via 
Digitalmars-d- announce wrote:

[...]


I confess that I tend to think of betterC as a waste of time. 
Clearly, there are folks who find it useful, but it loses so 
much that I see no point in using it for anything unless I 
have no choice. As long as attempts to improve it don't 
negatively impact normal D, then I don't really care what 
happens with it, but it's clearly not for me.


And it _is_ possible to use full-featured D from C/C++ when D 
does not control main. It's just more of a pain.


- Jonathan M Davis


Totally agree with this.


I disagree, I believe BetterC is worth the time.


Re: D as a Better C

2017-08-24 Thread Jacob Carlborg via Digitalmars-d-announce

On 2017-08-24 02:55, H. S. Teoh via Digitalmars-d-announce wrote:


One thing that would help is if things like TypeInfo, ModuleInfo, etc.,
are only emitted on-demand


I think that would be quite difficult if we want to keep all the 
existing features. Combining separate compilation, runtime reflection 
and similar features make it difficult to know when a Type/ModuleInfo is 
used.


--
/Jacob Carlborg


Re: D as a Better C

2017-08-24 Thread Kagamin via Digitalmars-d-announce
On Wednesday, 23 August 2017 at 17:43:27 UTC, Steven 
Schveighoffer wrote:
I thought "closure" means allocating the stack onto the heap so 
you can return the delegate with its context intact.


I understood closure as capture of variables from external 
context. They are divided into upward closures and downward 
closures, the former needs heap allocation.


Re: D as a Better C

2017-08-24 Thread Kagamin via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 22:45:27 UTC, sarn wrote:
I haven't tried the latest iteration of betterC yet, but the 
longstanding problem is that the compiler generates TypeInfo 
instances for structs


LDC doesn't generate TypeInfo for structs until it's required for 
some features like array comparison.


Re: D as a Better C

2017-08-23 Thread Swoorup Joshi via Digitalmars-d-announce
On Wednesday, 23 August 2017 at 17:44:31 UTC, Jonathan M Davis 
wrote:
On Wednesday, August 23, 2017 13:12:04 Mike Parker via 
Digitalmars-d- announce wrote:

[...]


I confess that I tend to think of betterC as a waste of time. 
Clearly, there are folks who find it useful, but it loses so 
much that I see no point in using it for anything unless I have 
no choice. As long as attempts to improve it don't negatively 
impact normal D, then I don't really care what happens with it, 
but it's clearly not for me.


And it _is_ possible to use full-featured D from C/C++ when D 
does not control main. It's just more of a pain.


- Jonathan M Davis


Totally agree with this.


Re: D as a Better C

2017-08-23 Thread 9il via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 13:12:04 UTC, Mike Parker wrote:
To coincide with the improvements to -betterC in the upcoming 
DMD 2.076, Walter has published a new article on the D blog 
about what it is and why to use it. A fun read. And I'm 
personally happy to see the love this feature is getting. I 
have a project I'd like to use it with if I can ever make the 
time for it!


The blog:

https://dlang.org/blog/2017/08/23/d-as-a-better-c/

Reddit:
https://www.reddit.com/r/programming/comments/6viswu/d_as_a_better_c/


Thanks for this feature! Looking forward to see its future  --Ilya


Re: D as a Better C

2017-08-23 Thread H. S. Teoh via Digitalmars-d-announce
On Thu, Aug 24, 2017 at 12:35:22AM +, Michael V. Franklin via 
Digitalmars-d-announce wrote:
[...]
> Consider this:  Rust doesn't need a special switch to make it
> interoperable with C.  What's wrong with D's implementation that
> requires such things?  Granted, D is not Rust, but D's implementation
> could be improved to make it more competitive with Rust in these use
> cases.  For example, there is really no need for TypeInfo if you're
> not doing any dynanmic casts, but the current implementation generates
> it regardless.  I find -betterC to be somewhat of a copout for
> avoiding the hard work of improving D's implementation.
[...]

One thing that would help is if things like TypeInfo, ModuleInfo, etc.,
are only emitted on-demand, or if they are stored as weak symbols in the
object file so that the linker can just omit them if they are never
referenced.

Ideally, the GC would also be on-demand, but it's currently too tightly
integrated with druntime for this to be possible. At least, it would
take a huge amount of effort to make it work.  Similarly, thread-related
stuff might be difficult to make optional, since the D startup code is
dependent on it.

Other smaller things in druntime like string switches, array comparison
functions, etc., could possibly also be optionally included, but then
you'll need to link (parts of) druntime. It will be more troublesome,
but within the realm of possibility, I think.

I, for one, would be happier if D's features are more pay-as-you-go so
that simpler programs don't have to pull in a whole bunch of executable
bloat that's not actually going to be used.


T

-- 
Let's eat some disquits while we format the biskettes.


Re: D as a Better C

2017-08-23 Thread Michael V. Franklin via Digitalmars-d-announce
On Wednesday, 23 August 2017 at 17:44:31 UTC, Jonathan M Davis 
wrote:


I confess that I tend to think of betterC as a waste of time. 
Clearly, there are folks who find it useful, but it loses so 
much that I see no point in using it for anything unless I have 
no choice. As long as attempts to improve it don't negatively 
impact normal D, then I don't really care what happens with it, 
but it's clearly not for me.


And it _is_ possible to use full-featured D from C/C++ when D 
does not control main. It's just more of a pain.


I'm somewhat in agreement here.  I wouldn't call it a "waste of 
time", but I would prefer refactoring D's implementation to make 
using full-featured D from C/C++ less of a pain.  I fear, 
however, that -betterC will be the favored excuse for not 
pursuing or prioritizing such improvements.


Consider this:  Rust doesn't need a special switch to make it 
interoperable with C.  What's wrong with D's implementation that 
requires such things?  Granted, D is not Rust, but D's 
implementation could be improved to make it more competitive with 
Rust in these use cases.  For example, there is really no need 
for TypeInfo if you're not doing any dynanmic casts, but the 
current implementation generates it regardless.  I find -betterC 
to be somewhat of a copout for avoiding the hard work of 
improving D's implementation.


Mike


Re: D as a Better C

2017-08-23 Thread sarn via Digitalmars-d-announce
On Wednesday, 23 August 2017 at 17:44:31 UTC, Jonathan M Davis 
wrote:

I confess that I tend to think of betterC as a waste of time.


The overwhelming majority of programmers don't need betterC.  At 
all.  But today we live in a world where practically everything 
just builds on top of C, and we keep seeing how that goes wrong.  
I think Rust and betterC D are the best candidates we've got for 
replacing C everywhere C is used.


Re: D as a Better C

2017-08-23 Thread sarn via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 16:17:57 UTC, SrMordred wrote:

No structs in -betterC ???


I haven't tried the latest iteration of betterC yet, but the 
longstanding problem is that the compiler generates TypeInfo 
instances for structs, and TypeInfos are classes, which inherit 
from Object, which are implemented in the D runtime.  If you're 
using the current release of D, the workarounds are to include an 
implementation of Object so that classes work, or hack out the 
TypeInfo at link time.


Re: D as a Better C

2017-08-23 Thread Moritz Maxeiner via Digitalmars-d-announce
On Wednesday, 23 August 2017 at 17:43:27 UTC, Steven 
Schveighoffer wrote:

On 8/23/17 11:59 AM, Walter Bright wrote:

On 8/23/2017 7:37 AM, Steven Schveighoffer wrote:

How do dynamic closures work without the GC?


They don't allocate the closure on the GC heap. (Or do I have 
static/dynamic closures backwards?)


I thought "closure" means allocating the stack onto the heap so 
you can return the delegate with its context intact.


From 
https://en.wikipedia.org/wiki/Closure_(computer_programming) :


"A language implementation cannot easily support full closures 
if its run-time memory model allocates all automatic variables 
on a linear stack. In such languages, a function's automatic 
local variables are deallocated when the function returns. 
However, a closure requires that the free variables it 
references survive the enclosing function's execution. 
Therefore, those variables must be allocated so that they 
persist until no longer needed, typically via heap allocation, 
rather than on the stack, and their lifetime must be managed so 
they survive until all closures referencing them have are no 
longer in use."


Right, so if we wanted to support closures in betterC (we don't 
now, as my earlier example shows), they'd need a separate 
lifetime management implementation.
The two straightforward ones are either disable copying of 
closures in betterC (only moving them), so a single ownership 
model of their heap allocated context pointer is possible 
(deallocating the memory once the closure is destroyed), or make 
them reference counted.
The first has the disadvantage that you can't have two closures 
point to the same heap context (though to be honest, I haven't 
seen a codebase so far that actually uses that), but it should be 
trivial to implement. The RC variant is more complex (it would 
require an analysis if reference cycles can occur), but I think 
this might be one of the cases where RC is the right solution 
(and we might even consider using RC in normal D as well, if it 
works sanely).


Re: D as a Better C

2017-08-23 Thread jmh530 via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 17:39:00 UTC, Walter Bright wrote:

On 8/23/2017 10:26 AM, jmh530 wrote:
Am I correct that betterC requires main to be extern(C) and 
must act like a C main (i.e. no void return)?


Yes.


This might be added to
http://dlang.org/dmd-windows.html#switch-betterC
or
http://dlang.org/spec/betterc.html




Is that something that can be changed in the future?


Yes, but I don't see a need for it.


Fair enough. A version statement could handle it

version(BetterC)
{
extern(C) int main()
{
callRealMain();
}
}
else
{
void main()
{
callRealMain();
}
}



Re: D as a Better C

2017-08-23 Thread Steven Schveighoffer via Digitalmars-d-announce

On 8/23/17 11:59 AM, Walter Bright wrote:

On 8/23/2017 7:37 AM, Steven Schveighoffer wrote:

How do dynamic closures work without the GC?


They don't allocate the closure on the GC heap. (Or do I have 
static/dynamic closures backwards?)


I thought "closure" means allocating the stack onto the heap so you can 
return the delegate with its context intact.


From https://en.wikipedia.org/wiki/Closure_(computer_programming) :

"A language implementation cannot easily support full closures if its 
run-time memory model allocates all automatic variables on a linear 
stack. In such languages, a function's automatic local variables are 
deallocated when the function returns. However, a closure requires that 
the free variables it references survive the enclosing function's 
execution. Therefore, those variables must be allocated so that they 
persist until no longer needed, typically via heap allocation, rather 
than on the stack, and their lifetime must be managed so they survive 
until all closures referencing them have are no longer in use."


-Steve


Re: D as a Better C

2017-08-23 Thread Walter Bright via Digitalmars-d-announce

On 8/23/2017 10:17 AM, Kagamin wrote:

Also how assert failure works in C?


It calls the C assert failure function.



Re: D as a Better C

2017-08-23 Thread Jonathan M Davis via Digitalmars-d-announce
On Wednesday, August 23, 2017 13:12:04 Mike Parker via Digitalmars-d-
announce wrote:
> To coincide with the improvements to -betterC in the upcoming DMD
> 2.076, Walter has published a new article on the D blog about
> what it is and why to use it. A fun read. And I'm personally
> happy to see the love this feature is getting. I have a project
> I'd like to use it with if I can ever make the time for it!
>
> The blog:
>
> https://dlang.org/blog/2017/08/23/d-as-a-better-c/
>
> Reddit:
> https://www.reddit.com/r/programming/comments/6viswu/d_as_a_better_c/

I confess that I tend to think of betterC as a waste of time. Clearly, there
are folks who find it useful, but it loses so much that I see no point in
using it for anything unless I have no choice. As long as attempts to
improve it don't negatively impact normal D, then I don't really care what
happens with it, but it's clearly not for me.

And it _is_ possible to use full-featured D from C/C++ when D does not
control main. It's just more of a pain.

- Jonathan M Davis



Re: D as a Better C

2017-08-23 Thread Walter Bright via Digitalmars-d-announce

On 8/23/2017 10:26 AM, jmh530 wrote:
Am I correct that betterC requires main to be extern(C) and must act like a C 
main (i.e. no void return)?


Yes.


Is that something that can be changed in the future?


Yes, but I don't see a need for it.


Re: D as a Better C

2017-08-23 Thread Steven Schveighoffer via Digitalmars-d-announce

On 8/23/17 11:52 AM, Walter Bright wrote:

On 8/23/2017 7:24 AM, Steven Schveighoffer wrote:
Looks like there are some outstanding requests to be fulfilled before 
it's pulled.


I don't agree that the requests improve matters.


You may want to mention that in the PR. Right now it just looks like you 
haven't seen or responded to the requests.


-Steve


Re: D as a Better C

2017-08-23 Thread Steven Schveighoffer via Digitalmars-d-announce

On 8/23/17 11:56 AM, Walter Bright wrote:

On 8/23/2017 7:10 AM, Steven Schveighoffer wrote:

Nope.


A ModuleInfo is generated, as well as FMB/FM/FME sections. Those 
sections may not work with the C runtime.


My point was simply that your small example doesn't cause any runtime or 
link time errors. You need something more complicated to require betterC.


Not sure if ModuleInfo is generated. IIRC, Martin made it so it's not if 
no usage of the ModuleInfo is apparent.


Yes, adding a struct causes link errors, but not because of ModuleInfo, 
it's because of the expected TypeInfo.


-Steve


Re: D as a Better C

2017-08-23 Thread jmh530 via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 14:01:30 UTC, jmh530 wrote:


Great piece.

It might be useful to beef up the documentation on some of the 
things that betterC changes. For instance, here

http://dlang.org/dmd-windows.html#switch-betterC
links to TypeInfo, which has like one line of explanation of 
what it's for, and ModuleInfo isn't linked to at all (and I'm 
still a little unclear on what that does).



Am I correct that betterC requires main to be extern(C) and must 
act like a C main (i.e. no void return)?


Is that something that can be changed in the future? For 
instance, the simplest change would be if the compiler knows that 
its betterC, then it can insert extern(C) to main. A second 
adjustment could potentially to re-write D's void main's to int 
and add in a return. The first seems like a good idea 
superficially, but I'm not 100% on the second.


Re: D as a Better C

2017-08-23 Thread Kagamin via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 14:00:34 UTC, Walter Bright wrote:
One of the reasons people use C is to get that small footprint. 
This has been a large barrier to C programs making use of D.


Not a better C, but intermediate D has small footprint for me too.
7.5kb totext.exe (encodes stdin to base64 and writes to stdout) - 
wrote it to put images in xml for opensearch descriptions.

12.5kb retab.exe (retabifies source code with various features)
5.5kb keepower.exe (manages screen saver and power settings 
because of obnoxious domain policy)

14.5kb fsum.exe (computes various hash sums of a file)

Additional features: string switch, array cast. Also how assert 
failure works in C? Mine shows a nice formatted message.


Re: D as a Better C

2017-08-23 Thread Walter Bright via Digitalmars-d-announce

On 8/23/2017 6:12 AM, Mike Parker wrote:

The blog:

https://dlang.org/blog/2017/08/23/d-as-a-better-c/

Reddit:
https://www.reddit.com/r/programming/comments/6viswu/d_as_a_better_c/


Now on the front page of news.ycombinator.com !


Re: D as a Better C

2017-08-23 Thread XavierAP via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 13:12:04 UTC, Mike Parker wrote:
To coincide with the improvements to -betterC in the upcoming 
DMD 2.076, Walter has published a new article on the D blog 
about what it is and why to use it.


I like this concept of "upward compatibility," -- although 
opposed to backward it should be phrased "forward."


Will share also this one on LinkedIn...
I see D has official account on Facebook, Twitter, Reddit... No 
interest in LinkedIn? I think it can also be a good promotion 
platform for D.


Re: D as a Better C

2017-08-23 Thread via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 16:17:57 UTC, SrMordred wrote:
On Wednesday, 23 August 2017 at 15:53:11 UTC, Walter Bright 
wrote:

On 8/23/2017 7:10 AM, Steven Schveighoffer wrote:
It's only if you do something that needs the runtime, such as 
static ctors, or use the GC.


Or use asserts, or even declare a struct.


No structs in -betterC ???


IIUC, Steven's question was about the need for the `-betterC` 
switch - in his small example there was no need for it. Walter 
pointed out that without -betterC using structs cause link-time 
references to druntime, which are avoided by the use of the 
`-betterC` switch.
Though, one particular thing that doesn't work in `-betterC` 
w.r.t. structs is RAII. You can still call manually the 
destructor, but that's a crude hack. Work on RAII for `-betterC` 
is work in progress.


Re: D as a Better C

2017-08-23 Thread SrMordred via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 15:53:11 UTC, Walter Bright wrote:

On 8/23/2017 7:10 AM, Steven Schveighoffer wrote:
It's only if you do something that needs the runtime, such as 
static ctors, or use the GC.


Or use asserts, or even declare a struct.


No structs in -betterC ???


Re: D as a Better C

2017-08-23 Thread Walter Bright via Digitalmars-d-announce

On 8/23/2017 7:37 AM, Steven Schveighoffer wrote:

How do dynamic closures work without the GC?


They don't allocate the closure on the GC heap. (Or do I have static/dynamic 
closures backwards?)


Re: D as a Better C

2017-08-23 Thread Walter Bright via Digitalmars-d-announce

On 8/23/2017 8:05 AM, John Colvin wrote:

"D polymorphic classes will not, as they rely on the garbage collector."

They do? Don't have to allocate classes on the GC heap.


Using them without the GC is a fairly advanced technique, and I don't want to 
deal with people writing:


C c = new C();

and complaining that it doesn't work.


Re: D as a Better C

2017-08-23 Thread Walter Bright via Digitalmars-d-announce

On 8/23/2017 7:10 AM, Steven Schveighoffer wrote:

Nope.


A ModuleInfo is generated, as well as FMB/FM/FME sections. Those sections may 
not work with the C runtime.


Re: D as a Better C

2017-08-23 Thread Walter Bright via Digitalmars-d-announce

On 8/23/2017 7:10 AM, Steven Schveighoffer wrote:
It's only if you do something that needs the runtime, such as static ctors, or 
use the GC.


Or use asserts, or even declare a struct.



Re: D as a Better C

2017-08-23 Thread Walter Bright via Digitalmars-d-announce

On 8/23/2017 7:24 AM, Steven Schveighoffer wrote:

Looks like there are some outstanding requests to be fulfilled before it's 
pulled.


I don't agree that the requests improve matters.



Re: D as a Better C

2017-08-23 Thread Moritz Maxeiner via Digitalmars-d-announce
On Wednesday, 23 August 2017 at 15:17:31 UTC, Moritz Maxeiner 
wrote:
On Wednesday, 23 August 2017 at 14:37:19 UTC, Steven 
Schveighoffer wrote:

On 8/23/17 9:12 AM, Mike Parker wrote:
To coincide with the improvements to -betterC in the upcoming 
DMD 2.076, Walter has published a new article on the D blog 
about what it is and why to use it. A fun read. And I'm 
personally happy to see the love this feature is getting. I 
have a project I'd like to use it with if I can ever make the 
time for it!


The blog:

https://dlang.org/blog/2017/08/23/d-as-a-better-c/

Reddit:
https://www.reddit.com/r/programming/comments/6viswu/d_as_a_better_c/


How do dynamic closures work without the GC?

Nice article, BTW.

-Steve


They don't (right now, using dmd ~master), because they depend 
on druntime:


[...]


Sorry, I screwed up when pasting. Here's what I meant to post:


--- a.c ---
#include 
#include 

uint32_t foo();

int main(int argc, char** argv)
{
uint32_t x = foo();
printf("%d\n", x);
return 0;
}
---

--- b.d ---
auto test()
{
uint i = 42;
return () {
return i;
};
}

$ dmd -c -betterC b.d
$ gcc a.c b.d
Undefined symbols for architecture x86_64:
  "__d_allocmemory", referenced from:
  _D1b4testFNaNbNfZDFNaNbNiNfZk in b.o


Re: D as a Better C

2017-08-23 Thread yawniek via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 13:12:04 UTC, Mike Parker wrote:
To coincide with the improvements to -betterC in the upcoming 
DMD 2.076, Walter has published a new article on the D blog 
about what it is and why to use it. A fun read. And I'm 
personally happy to see the love this feature is getting. I 
have a project I'd like to use it with if I can ever make the 
time for it!


The blog:

https://dlang.org/blog/2017/08/23/d-as-a-better-c/

Reddit:
https://www.reddit.com/r/programming/comments/6viswu/d_as_a_better_c/


nice article, however very unfortunate  introduction for the ADHD 
Generation as you start reading and you get put of by historical 
disabilities of D that are not true anymore. you may want to edit 
that and add the "until now" beforehand ;)


Re: D as a Better C

2017-08-23 Thread Moritz Maxeiner via Digitalmars-d-announce
On Wednesday, 23 August 2017 at 14:37:19 UTC, Steven 
Schveighoffer wrote:

On 8/23/17 9:12 AM, Mike Parker wrote:
To coincide with the improvements to -betterC in the upcoming 
DMD 2.076, Walter has published a new article on the D blog 
about what it is and why to use it. A fun read. And I'm 
personally happy to see the love this feature is getting. I 
have a project I'd like to use it with if I can ever make the 
time for it!


The blog:

https://dlang.org/blog/2017/08/23/d-as-a-better-c/

Reddit:
https://www.reddit.com/r/programming/comments/6viswu/d_as_a_better_c/


How do dynamic closures work without the GC?

Nice article, BTW.

-Steve


They don't (right now, using dmd ~master), because they depend on 
druntime:


--- a.c ---
#include 
#include 

uint32_t foo();

int main(int argc, char** argv)
{
uint32_t x = foo();
printf("%d\n", x);
}
---

--- b.d ---
auto test()
{
uint i = 42;
return () {
return i;
};
}

oo()
{
auto x = test();
return x();
}
---

$ dmd -c -betterC b.d
$ gcc a.c b.d
Undefined symbols for architecture x86_64:
  "__d_allocmemory", referenced from:
  _D1b4testFNaNbNfZDFNaNbNiNfZk in b.o
ld: symbol(s) not found for architecture x86_64extern(C) uint 
foo()

{
auto x = test();
return x();
}
---

$ dmd -c -betterC b.d
$ gcc a.c b.d
Undefined symbols for architecture x86_64:
  "__d_allocmemory", referenced from:
  _D1b4testFNaNbNfZDFNaNbNiNfZk in b.o



Re: D as a Better C

2017-08-23 Thread John Colvin via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 13:12:04 UTC, Mike Parker wrote:
To coincide with the improvements to -betterC in the upcoming 
DMD 2.076, Walter has published a new article on the D blog 
about what it is and why to use it. A fun read. And I'm 
personally happy to see the love this feature is getting. I 
have a project I'd like to use it with if I can ever make the 
time for it!


The blog:

https://dlang.org/blog/2017/08/23/d-as-a-better-c/

Reddit:
https://www.reddit.com/r/programming/comments/6viswu/d_as_a_better_c/


"D polymorphic classes will not, as they rely on the garbage 
collector."


They do? Don't have to allocate classes on the GC heap.


Re: D as a Better C

2017-08-23 Thread Steven Schveighoffer via Digitalmars-d-announce

On 8/23/17 9:12 AM, Mike Parker wrote:
To coincide with the improvements to -betterC in the upcoming DMD 2.076, 
Walter has published a new article on the D blog about what it is and 
why to use it. A fun read. And I'm personally happy to see the love this 
feature is getting. I have a project I'd like to use it with if I can 
ever make the time for it!


The blog:

https://dlang.org/blog/2017/08/23/d-as-a-better-c/

Reddit:
https://www.reddit.com/r/programming/comments/6viswu/d_as_a_better_c/


How do dynamic closures work without the GC?

Nice article, BTW.

-Steve


Re: D as a Better C

2017-08-23 Thread Moritz Maxeiner via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 14:00:34 UTC, Walter Bright wrote:

On 8/23/2017 6:28 AM, Moritz Maxeiner wrote:


I've been mixing C and full D for a while now (on Linux) by 
either having the main C program call rt_init/rt_term directly 
(if druntime is linked in when building a mixed C/D 
application), or have Runtime.initialize/Runtime.terminate be 
called from D via some plugin_load/plugin_unload functionality 
when using D shared libraries.

Why is this not considered practical?


Because in order to add a D function as trivial as:

   int foo() { return 3; }

to a C program, now the C program has to link to druntime, and 
the program no longer has a small footprint. One of the reasons 
people use C is to get that small footprint. This has been a 
large barrier to C programs making use of D.


Thank you, are there other factors involved, or is it only 
impractical for people who require minimal application size / 
memory footprint, then?


Re: D as a Better C

2017-08-23 Thread Steven Schveighoffer via Digitalmars-d-announce

On 8/23/17 10:11 AM, Walter Bright wrote:

On 8/23/2017 7:01 AM, jmh530 wrote:
ModuleInfo isn't linked to at all (and I'm still a little unclear on 
what that does).


That's because ModuleInfo doesn't appear in the online documentation due 
to having a malformed Ddoc comment. I fixed it here:


   https://github.com/dlang/druntime/pull/1906

but nobody has pulled it.


Looks like there are some outstanding requests to be fulfilled before 
it's pulled.


-Steve


Re: D as a Better C

2017-08-23 Thread Meta via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 14:01:30 UTC, jmh530 wrote:

On Wednesday, 23 August 2017 at 13:12:04 UTC, Mike Parker wrote:
To coincide with the improvements to -betterC in the upcoming 
DMD 2.076, Walter has published a new article on the D blog 
about what it is and why to use it. A fun read. And I'm 
personally happy to see the love this feature is getting. I 
have a project I'd like to use it with if I can ever make the 
time for it!


The blog:

https://dlang.org/blog/2017/08/23/d-as-a-better-c/

Reddit:
https://www.reddit.com/r/programming/comments/6viswu/d_as_a_better_c/


Great piece.

It might be useful to beef up the documentation on some of the 
things that betterC changes. For instance, here

http://dlang.org/dmd-windows.html#switch-betterC
links to TypeInfo, which has like one line of explanation of 
what it's for, and ModuleInfo isn't linked to at all (and I'm 
still a little unclear on what that does).


Walter has made a PR to improve the ModuleInfo documentation: 
https://github.com/dlang/druntime/pull/1906


Re: D as a Better C

2017-08-23 Thread Walter Bright via Digitalmars-d-announce

On 8/23/2017 7:01 AM, jmh530 wrote:
ModuleInfo isn't linked to at all (and I'm still a little unclear on what that 
does).


That's because ModuleInfo doesn't appear in the online documentation due to 
having a malformed Ddoc comment. I fixed it here:


  https://github.com/dlang/druntime/pull/1906

but nobody has pulled it.


Re: D as a Better C

2017-08-23 Thread Steven Schveighoffer via Digitalmars-d-announce

On 8/23/17 10:00 AM, Walter Bright wrote:

On 8/23/2017 6:28 AM, Moritz Maxeiner wrote:

Interesting article, though one thing that I'm confused by is

Hence D libraries remain inaccessible to C programs, and chimera 
programs (a mix of C and D) are not practical. One cannot 
pragmatically “try out” D by add D modules to an existing C program.


I've been mixing C and full D for a while now (on Linux) by either 
having the main C program call rt_init/rt_term directly (if druntime 
is linked in when building a mixed C/D application), or have 
Runtime.initialize/Runtime.terminate be called from D via some 
plugin_load/plugin_unload functionality when using D shared libraries.

Why is this not considered practical?


Because in order to add a D function as trivial as:

int foo() { return 3; }

to a C program, now the C program has to link to druntime, and the 
program no longer has a small footprint. One of the reasons people use C 
is to get that small footprint. This has been a large barrier to C 
programs making use of D.




Nope.

Stevens-MacBook-Pro:testd steves$ cat testdfunc.d
extern(C) int foo() { return 3; }
Stevens-MacBook-Pro:testd steves$ cat testdfunc_c.c
#include 
extern int foo();

int main()
{
printf("%d\n", foo());
}
Stevens-MacBook-Pro:testd steves$ dmd -c testdfunc.d
Stevens-MacBook-Pro:testd steves$ gcc -o testdfunc testdfunc_c.c testdfunc.o
Stevens-MacBook-Pro:testd steves$ ./testdfunc
3


It's only if you do something that needs the runtime, such as static 
ctors, or use the GC.


-Steve


Re: D as a Better C

2017-08-23 Thread jmh530 via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 13:12:04 UTC, Mike Parker wrote:
To coincide with the improvements to -betterC in the upcoming 
DMD 2.076, Walter has published a new article on the D blog 
about what it is and why to use it. A fun read. And I'm 
personally happy to see the love this feature is getting. I 
have a project I'd like to use it with if I can ever make the 
time for it!


The blog:

https://dlang.org/blog/2017/08/23/d-as-a-better-c/

Reddit:
https://www.reddit.com/r/programming/comments/6viswu/d_as_a_better_c/


Great piece.

It might be useful to beef up the documentation on some of the 
things that betterC changes. For instance, here

http://dlang.org/dmd-windows.html#switch-betterC
links to TypeInfo, which has like one line of explanation of what 
it's for, and ModuleInfo isn't linked to at all (and I'm still a 
little unclear on what that does).


Re: D as a Better C

2017-08-23 Thread Walter Bright via Digitalmars-d-announce

On 8/23/2017 6:28 AM, Moritz Maxeiner wrote:

Interesting article, though one thing that I'm confused by is

Hence D libraries remain inaccessible to C programs, and chimera programs (a 
mix of C and D) are not practical. One cannot pragmatically “try out” D by 
add D modules to an existing C program.


I've been mixing C and full D for a while now (on Linux) by either having the 
main C program call rt_init/rt_term directly (if druntime is linked in when 
building a mixed C/D application), or have Runtime.initialize/Runtime.terminate 
be called from D via some plugin_load/plugin_unload functionality when using D 
shared libraries.

Why is this not considered practical?


Because in order to add a D function as trivial as:

   int foo() { return 3; }

to a C program, now the C program has to link to druntime, and the program no 
longer has a small footprint. One of the reasons people use C is to get that 
small footprint. This has been a large barrier to C programs making use of D.




Re: D as a Better C

2017-08-23 Thread Moritz Maxeiner via Digitalmars-d-announce

On Wednesday, 23 August 2017 at 13:12:04 UTC, Mike Parker wrote:
To coincide with the improvements to -betterC in the upcoming 
DMD 2.076, Walter has published a new article on the D blog 
about what it is and why to use it. A fun read. And I'm 
personally happy to see the love this feature is getting. I 
have a project I'd like to use it with if I can ever make the 
time for it!


The blog:

https://dlang.org/blog/2017/08/23/d-as-a-better-c/

Reddit:
https://www.reddit.com/r/programming/comments/6viswu/d_as_a_better_c/


Interesting article, though one thing that I'm confused by is

Hence D libraries remain inaccessible to C programs, and 
chimera programs (a mix of C and D) are not practical. One 
cannot pragmatically “try out” D by add D modules to an 
existing C program.


I've been mixing C and full D for a while now (on Linux) by 
either having the main C program call rt_init/rt_term directly 
(if druntime is linked in when building a mixed C/D application), 
or have Runtime.initialize/Runtime.terminate be called from D via 
some plugin_load/plugin_unload functionality when using D shared 
libraries.

Why is this not considered practical?


D as a Better C

2017-08-23 Thread Mike Parker via Digitalmars-d-announce
To coincide with the improvements to -betterC in the upcoming DMD 
2.076, Walter has published a new article on the D blog about 
what it is and why to use it. A fun read. And I'm personally 
happy to see the love this feature is getting. I have a project 
I'd like to use it with if I can ever make the time for it!


The blog:

https://dlang.org/blog/2017/08/23/d-as-a-better-c/

Reddit:
https://www.reddit.com/r/programming/comments/6viswu/d_as_a_better_c/