Re: I don't understand betterC

2023-09-08 Thread rempas via Digitalmars-d-learn

On Monday, 4 September 2023 at 07:39:21 UTC, confused wrote:


So then I guess I'd still like to know how I'm expected to 
store and access an array of characters without the C runtime 
as I tried in my original post.


You are going to allocate memory using the system call of your 
operation system and then you'll use a pointer as you would if 
you had used "malloc".


Is this what you are trying to do?


Re: I don't understand betterC

2023-09-04 Thread evilrat via Digitalmars-d-learn

On Monday, 4 September 2023 at 07:39:21 UTC, confused wrote:


So then I guess I'd still like to know how I'm expected to 
store and access an array of characters without the C runtime 
as I tried in my original post.


Without C runtime functions such as malloc you can still have 
fixed-length arrays, for string variables the compiler will emit 
null-terminated string at compile time in cases like that making 
it compatible with C functions.


Note that it not the case for other string operations like concat 
operator (is it available in betterC?), in that case if you want 
to pass the resulting string you have to add null terminator by 
hand.


If you need to allocate memory at runtime and still wan't to 
avoid C runtime, well I guess you have to do some syscalls then...
Or use another allocator library, jmalloc maybe? Though I don't 
have the experience with them and don't know if they are using C 
runtime somewhere inside or handle that low level OS/syscalls 
stuff by itself.


Re: I don't understand betterC

2023-09-04 Thread confused via Digitalmars-d-learn
On Saturday, 2 September 2023 at 07:59:31 UTC, Jonathan M Davis 
wrote:
If you put it into a package, then you could have your own 
object module that then isn't at the top level - e.g. 
mypkg/object.d with


module mypkg.object;

but you can't have more than one module in your program with 
the same full module name. So, in the case of the top-level 
module, object, you can only declare your own if you replace 
the default one, which you might do in some special situations, 
but it's not something that you would normally do, and you can 
never have both the normal object module and your own in the 
same program.


- Jonathan M Davis


So then I guess I'd still like to know how I'm expected to store 
and access an array of characters without the C runtime as I 
tried in my original post.


Re: I don't understand betterC

2023-09-02 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, September 2, 2023 4:38:41 AM BST confused via Digitalmars-d-learn 
wrote:
> On Friday, 1 September 2023 at 13:45:05 UTC, evilrat wrote:
> > It is shadowing default implicit "import object;", here a
> > demonstration
> >
> > ```d
> > // this example shows default implicit import of "object" module
> > // compile this example:
> > //   ldc2 -c test.d
> > // output:
> > //   tuple("object", "core", "main", "thisModule")
> >
> > // just a random import
> > import core.stdc.stdio;
> >
> > void main() { }
> >
> > alias thisModule = __traits(parent, main);
> > pragma(msg,  __traits(allMembers, thisModule)); // has
> > implicitly imported 'object' module
> > ```
>
> Is there no way for the two to coexist?

If you put it into a package, then you could have your own object module
that then isn't at the top level - e.g. mypkg/object.d with

module mypkg.object;

but you can't have more than one module in your program with the same full
module name. So, in the case of the top-level module, object, you can only
declare your own if you replace the default one, which you might do in some
special situations, but it's not something that you would normally do, and
you can never have both the normal object module and your own in the same
program.

- Jonathan M Davis





Re: I don't understand betterC

2023-09-01 Thread evilrat via Digitalmars-d-learn

On Saturday, 2 September 2023 at 03:27:51 UTC, confused wrote:
So I guess my next question is why, exactly, classes *can*, in 
fact, be implemented in ``` betterC ```, but are not?


IIRC you can have extern(C++) classes in betterC, the real issue 
is the plain extern(D) classes which has some assumptions that 
are not present in betterC runtime.
Be careful though as extern(C++) classes have a bit different 
behavior where you might not expect it.
Specifically they have different vtable layout and some quirks 
with regard to multiple inheritance which is not available in D.
They might have some different destructor logic, and maybe there 
is more...


Otherwise you will have to mimic "classes" behavior with some 
template magic, just like OOP in C similar to COM model or gtk 
gobject, this means no fancy keyword and no language support for 
them though. But with all the templates and CTFE this shouldn't 
be a problem.


Also if you have read this forums regarding betterC... well, I 
think its only valid real use cases are tiny microcontrollers and 
WebAsm (because of GC), but the latter case can probably avoided 
with custom D runtime and people in fact has crafted some custom 
implementations.


Re: I don't understand betterC

2023-09-01 Thread bachmeier via Digitalmars-d-learn

On Saturday, 2 September 2023 at 03:18:31 UTC, confused wrote:

On Friday, 1 September 2023 at 13:31:37 UTC, bachmeier wrote:

You can read the documentation for object.d 
[here](https://dlang.org/phobos/object.html). It's kind of 
important.


I'm not sure which specific part of the documentation was 
supposed to illuminate the exact nature of that error.


This was your inquiry:

I have in fact defined a module called object. How is that 
related to this error?


And at the very top of the page it says:

Forms the symbols available to all D programs. Includes Object, 
which is the root of the class object hierarchy. This module is 
implicitly imported.


That means there's a conflict with your module. This hasn't ever 
been an issue for me, so I can't tell you precisely why it gives 
that specific error message, but it explains how it's related, 
per your inquiry.


The easy fix is to not name your module something other than 
object.





Re: I don't understand betterC

2023-09-01 Thread confused via Digitalmars-d-learn

On Friday, 1 September 2023 at 13:45:05 UTC, evilrat wrote:
It is shadowing default implicit "import object;", here a 
demonstration


```d
// this example shows default implicit import of "object" module
// compile this example:
//   ldc2 -c test.d
// output:
//   tuple("object", "core", "main", "thisModule")

// just a random import
import core.stdc.stdio;

void main() { }

alias thisModule = __traits(parent, main);
pragma(msg,  __traits(allMembers, thisModule)); // has 
implicitly imported 'object' module

```


Is there no way for the two to coexist?


Re: I don't understand betterC

2023-09-01 Thread confused via Digitalmars-d-learn

On Saturday, 2 September 2023 at 01:05:39 UTC, ryuukk_ wrote:
If you are looking for a better C you are not looking for 
classes


You contradict yourself

If you heard about betterC, then you heard about this: 
https://dlang.org/spec/betterc.html



Read it again, and specially this part: 
https://dlang.org/spec/betterc.html#consequences


C has no GC, therefore no class, see, your contradiction ;)


Actually, I'm not looking for C, I'm looking for *better* C. C 
with classes would, in fact, be better. Except I hate C++.


In any case, I read all those links already, but D documentation 
can be rather out of date sometimes, and after having found a 
blog online about retaining classes in ``` betterC ```, I decided 
to try it out. What that blog failed to mention was that such a 
simple means of retaining classes would conflict with ``` stdc 
```.


So I guess my next question is why, exactly, classes *can*, in 
fact, be implemented in ``` betterC ```, but are not?


Re: I don't understand betterC

2023-09-01 Thread confused via Digitalmars-d-learn

On Friday, 1 September 2023 at 13:31:37 UTC, bachmeier wrote:

You can read the documentation for object.d 
[here](https://dlang.org/phobos/object.html). It's kind of 
important.


I'm not sure which specific part of the documentation was 
supposed to illuminate the exact nature of that error.


Re: I don't understand betterC

2023-09-01 Thread ryuukk_ via Digitalmars-d-learn

On Friday, 1 September 2023 at 13:17:08 UTC, confused wrote:
On Friday, 1 September 2023 at 08:19:55 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
``size_t`` is defined in ``object.d`` which is implicitly 
imported into all modules.


If it cannot be found, one of three things is happening:

1) You have messed with some cli args related to picking 
druntime/phobos

2) You have defined a module called object.
3) You have a broken compiler install.


I have in fact defined a module called ``` object ```. How is 
that related to this error?


I'm interested in betterC as a curiosity, and trying to explore 
the extent to which D classes and object-oriented programming 
can be preserved within its bounds (because I really like 
classes, and I really like D, and I really hate C++, and I'm 
trying to learn about bare-metal programming).


Thank you, by the way, for sharing your knowledge and time.


If you are looking for a better C you are not looking for classes

You contradict yourself

If you heard about betterC, then you heard about this: 
https://dlang.org/spec/betterc.html



Read it again, and specially this part: 
https://dlang.org/spec/betterc.html#consequences


C has no GC, therefore no class, see, your contradiction ;)


Re: I don't understand betterC

2023-09-01 Thread evilrat via Digitalmars-d-learn

On Friday, 1 September 2023 at 13:17:08 UTC, confused wrote:
On Friday, 1 September 2023 at 08:19:55 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
``size_t`` is defined in ``object.d`` which is implicitly 
imported into all modules.


If it cannot be found, one of three things is happening:

1) You have messed with some cli args related to picking 
druntime/phobos

2) You have defined a module called object.
3) You have a broken compiler install.


I have in fact defined a module called ``` object ```. How is 
that related to this error?




It is shadowing default implicit "import object;", here a 
demonstration


```d
// this example shows default implicit import of "object" module
// compile this example:
//   ldc2 -c test.d
// output:
//   tuple("object", "core", "main", "thisModule")

// just a random import
import core.stdc.stdio;

void main() { }

alias thisModule = __traits(parent, main);
pragma(msg,  __traits(allMembers, thisModule)); // has implicitly 
imported 'object' module

```



Re: I don't understand betterC

2023-09-01 Thread bachmeier via Digitalmars-d-learn

On Friday, 1 September 2023 at 13:17:08 UTC, confused wrote:
On Friday, 1 September 2023 at 08:19:55 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
``size_t`` is defined in ``object.d`` which is implicitly 
imported into all modules.


If it cannot be found, one of three things is happening:

1) You have messed with some cli args related to picking 
druntime/phobos

2) You have defined a module called object.
3) You have a broken compiler install.


I have in fact defined a module called ``` object ```. How is 
that related to this error?


I'm interested in betterC as a curiosity, and trying to explore 
the extent to which D classes and object-oriented programming 
can be preserved within its bounds (because I really like 
classes, and I really like D, and I really hate C++, and I'm 
trying to learn about bare-metal programming).


Thank you, by the way, for sharing your knowledge and time.


You can read the documentation for object.d 
[here](https://dlang.org/phobos/object.html). It's kind of 
important.


Re: I don't understand betterC

2023-09-01 Thread confused via Digitalmars-d-learn
On Friday, 1 September 2023 at 08:19:55 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
``size_t`` is defined in ``object.d`` which is implicitly 
imported into all modules.


If it cannot be found, one of three things is happening:

1) You have messed with some cli args related to picking 
druntime/phobos

2) You have defined a module called object.
3) You have a broken compiler install.


I have in fact defined a module called ``` object ```. How is 
that related to this error?


I'm interested in betterC as a curiosity, and trying to explore 
the extent to which D classes and object-oriented programming can 
be preserved within its bounds (because I really like classes, 
and I really like D, and I really hate C++, and I'm trying to 
learn about bare-metal programming).


Thank you, by the way, for sharing your knowledge and time.


Re: I don't understand betterC

2023-09-01 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
``size_t`` is defined in ``object.d`` which is implicitly imported into 
all modules.


If it cannot be found, one of three things is happening:

1) You have messed with some cli args related to picking druntime/phobos
2) You have defined a module called object.
3) You have a broken compiler install.


Re: I don't understand betterC

2023-08-31 Thread confused via Digitalmars-d-learn
On Thursday, 31 August 2023 at 18:42:57 UTC, Richard (Rikki) 
Andrew Cattermole wrote:

```d
extern(C) int main()
{
import core.stdc.stdio;

string hello = "hello";
printf(hello.ptr);

return 0;
}
```

1) You forgot to import ``core.stdc.stdio``
2) String literal is of type string (which is an alias to 
``immutable(char)[]``).
3) A slice is a pointer + length, and C doesn't understand 
slices, so you must explicitly pass in the pointer from the 
slice (the compiler would do this for you if you had the 
literal in the arguments list instead of the variable).


```d
import core.stdc.stdio;
```

This generates ``` Error: undefined identifier `size_t` ```.


Re: I don't understand betterC

2023-08-31 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn

```d
extern(C) int main()
{
import core.stdc.stdio;

string hello = "hello";
printf(hello.ptr);

return 0;
}
```

1) You forgot to import ``core.stdc.stdio``
2) String literal is of type string (which is an alias to 
``immutable(char)[]``).
3) A slice is a pointer + length, and C doesn't understand slices, so 
you must explicitly pass in the pointer from the slice (the compiler 
would do this for you if you had the literal in the arguments list 
instead of the variable).


I don't understand betterC

2023-08-31 Thread confused via Digitalmars-d-learn
I decided to try out betterC with the most trivial example I 
could think of, but why doesn't this work?


```d
extern(C) int main()
{
char[] hello = "hello";
printf(hello);

return 0;
}
```