www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - I don't understand betterC

reply confused <no no.no> writes:
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;
}
```
Aug 31 2023
parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
```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).
Aug 31 2023
parent reply confused <no no.no> writes:
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` ```.
Aug 31 2023
parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
``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.
Sep 01 2023
parent reply confused <no no.no> writes:
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.
Sep 01 2023
next sibling parent reply bachmeier <no spam.net> writes:
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.
Sep 01 2023
parent reply confused <no no.no> writes:
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.
Sep 01 2023
parent bachmeier <no spam.net> writes:
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.
Sep 01 2023
prev sibling next sibling parent reply evilrat <evilrat666 gmail.com> writes:
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 ```
Sep 01 2023
parent reply confused <no no.no> writes:
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?
Sep 01 2023
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Sep 02 2023
parent reply confused <no no.no> writes:
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.
Sep 04 2023
next sibling parent evilrat <evilrat666 gmail.com> writes:
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.
Sep 04 2023
prev sibling parent rempas <rempas tutanota.com> writes:
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?
Sep 08 2023
prev sibling parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
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 ;)
Sep 01 2023
parent reply confused <no no.no> writes:
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?
Sep 01 2023
parent evilrat <evilrat666 gmail.com> writes:
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.
Sep 01 2023