Thread overview
Phobos in BetterC
Mar 08, 2019
Vasyl Teliman
Mar 09, 2019
Bastiaan Veelo
Mar 09, 2019
Laeeth Isharc
Mar 09, 2019
Nicholas Wilson
Mar 09, 2019
Sebastiaan Koppe
Mar 09, 2019
Nicholas Wilson
Mar 09, 2019
9il
Mar 09, 2019
Sebastiaan Koppe
Mar 10, 2019
9il
Mar 10, 2019
Seb
March 08, 2019
I've tried to use Mallocator in BetterC but it seems it's not available there:

https://run.dlang.io/is/pp3HDq

This produces a linker error.

I'm wondering why Mallocator is not available in this mode (it would be intuitive to assume that it's working). Also I would like to know what parts of Phobos are available there (e.g. std.traits, std.typecons...).

Thanks in advance.

March 09, 2019
On Friday, 8 March 2019 at 09:24:25 UTC, Vasyl Teliman wrote:
> I've tried to use Mallocator in BetterC but it seems it's not available there:
>
> https://run.dlang.io/is/pp3HDq
>
> This produces a linker error.
>
> I'm wondering why Mallocator is not available in this mode (it would be intuitive to assume that it's working). Also I would like to know what parts of Phobos are available there (e.g. std.traits, std.typecons...).
>
> Thanks in advance.

I can’t answer that but you can use C’s malloc directly: https://run.dlang.io/is/fnRFIr

Bastiaan.
March 09, 2019
On Friday, 8 March 2019 at 09:24:25 UTC, Vasyl Teliman wrote:
> I've tried to use Mallocator in BetterC but it seems it's not available there:
>
> https://run.dlang.io/is/pp3HDq
>
> This produces a linker error.
>
> I'm wondering why Mallocator is not available in this mode (it would be intuitive to assume that it's working). Also I would like to know what parts of Phobos are available there (e.g. std.traits, std.typecons...).
>
> Thanks in advance.

I would guess it's not available because it was written before betterC mode was a thing and nobody has yet updated it.

If you look at spasm on code.dlang.org there is a version you can copy paste that should work in betterC mode if I remember correctly.

March 09, 2019
On Friday, 8 March 2019 at 09:24:25 UTC, Vasyl Teliman wrote:
> I've tried to use Mallocator in BetterC but it seems it's not available there:
>
> https://run.dlang.io/is/pp3HDq
>
> This produces a linker error.
>
> I'm wondering why Mallocator is not available in this mode (it would be intuitive to assume that it's working). Also I would like to know what parts of Phobos are available there (e.g. std.traits, std.typecons...).
>
> Thanks in advance.

This is really a linker problem, because -betterC doesn't link druntime, and pbobos links druntime. to get around this pass -i=std.experimental.allocator to dmd along with the rest of you usual arguments.
March 09, 2019
On Friday, 8 March 2019 at 09:24:25 UTC, Vasyl Teliman wrote:
> Also I would to know what parts of Phobos are available there
> (e.g. std.traits, std.typecons...).

There is no clear rule on which phobos packages work and which don't. It all depends on what underlying features the phobos package uses. Here is the list of what's not allowed in betterC:

- Garbage Collection
- TypeInfo and ModuleInfo
- Classes
- Built-in threading (e.g. core.thread)
- Dynamic arrays (though slices of static arrays work) and associative arrays
- Exceptions
- synchronized and core.sync
- Static module constructors or destructors

Generally anything meta/compile-time works. Packages like std.traits / std.meta / std.range / std.algorithm are not an issue. You would expect std.typecons.tuple to work as well, but it doesn't.

If you are doing ctfe, then you are in for a bummer. Because the same restrictions apply there as well. There is supposed to be a workaround by including the ctfe file in question via -I on the command line, but I could never make it work in dub.

If you encounter something that doesn't work, there are a couple of options. Sometimes the function you are trying to use is betterC compatible but is inside a package that isn't. Just extract it into a separate file. At other times it is because the struct has a toString method, or throws an exception. Again, copy the relevant part, rip out the toString method and/or replace the exception with an assert (of course, after you make sure the assert doesn't get triggered). There might also be the option to use @nogc exceptions (dip 1008), but I am not sure.

If all that isn't possible, you will have to rewrite the thing in question.
March 09, 2019
On Saturday, 9 March 2019 at 12:42:34 UTC, Sebastiaan Koppe wrote:
> There might also be the option to use @nogc exceptions (dip 1008), but I am not sure.

That won't work as the implementation on DIP1008 cheats the type system but doesn't actually work, i.e. the exceptions are still CG allocated.

March 09, 2019
On Friday, 8 March 2019 at 09:24:25 UTC, Vasyl Teliman wrote:
> I've tried to use Mallocator in BetterC but it seems it's not available there:
>
> https://run.dlang.io/is/pp3HDq
>
> This produces a linker error.
>
> I'm wondering why Mallocator is not available in this mode (it would be intuitive to assume that it's working). Also I would like to know what parts of Phobos are available there (e.g. std.traits, std.typecons...).
>
> Thanks in advance.

Try this package
https://github.com/dlang-community/stdx-allocator

(v3.0.2)

It was fixed to be used in BetterC. If it still does not work you can open an issue and ping me (@9il).

Best,
Ilya
March 09, 2019
On Saturday, 9 March 2019 at 17:14:37 UTC, 9il wrote:
> It was fixed to be used in BetterC. If it still does not work you can open an issue and ping me (@9il).

That is awesome. I suppose support for betterC is only from v3 upwards?
March 10, 2019
On Saturday, 9 March 2019 at 19:40:27 UTC, Sebastiaan Koppe wrote:
> On Saturday, 9 March 2019 at 17:14:37 UTC, 9il wrote:
>> It was fixed to be used in BetterC. If it still does not work you can open an issue and ping me (@9il).
>
> That is awesome. I suppose support for betterC is only from v3 upwards?

Yes. However, it is hard to test. Also, BetterC works better in LDC. I have never seen a real production betterC program compiled with DMD.
March 10, 2019
On Friday, 8 March 2019 at 09:24:25 UTC, Vasyl Teliman wrote:
> I've tried to use Mallocator in BetterC but it seems it's not available there:
>
> https://run.dlang.io/is/pp3HDq
>
> This produces a linker error.
>
> I'm wondering why Mallocator is not available in this mode (it would be intuitive to assume that it's working). Also I would like to know what parts of Phobos are available there (e.g. std.traits, std.typecons...).
>
> Thanks in advance.

Just for reference there's also a few number of tests in Phobos annotated with @betterC. As expected, they are tested with -betterC. However, at the moment this annotated subset is pretty small and not exposed to the documentation. Though it shouldn't be too hard to increase the subset and mark the annotated function in the documentation.