Re: Phobos in BetterC

2019-03-09 Thread 9il via Digitalmars-d-learn

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.


Re: this is null

2019-03-09 Thread ANtlord via Digitalmars-d-learn

On Saturday, 9 March 2019 at 21:00:51 UTC, Ali Çehreli wrote:

I haven't run the code but which pointer is null? Try adding
I mean `this` by "this" word. You can see that `this` is null if 
you run gdb and before that line make `p/x this` [0]

this check as well:

  auto node = this.list.getFisrtFreeOrAdd(memViewLen);
  assert(node !is null);

I get segfault in `getFisrtFreeOrAdd` method. Before the line I 
have an assertion [1]. It looks like the program misses (I have 
no idea how) `list` object while calling its method 
`getFisrtFreeOrAdd`.


[0] 
https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/freelist.d#L56
[1] 
https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/mem.d#L19





Re: this is null

2019-03-09 Thread Ali Çehreli via Digitalmars-d-learn

On 03/09/2019 12:10 PM, ANtlord wrote:

On Saturday, 9 March 2019 at 20:04:53 UTC, Paul Backus wrote:


You can end up with a null `this` reference if you dereference a null 
pointer to a struct and then call a method on the result. For example:




I can but my reference is not null before calling. Take a look at the 
line of code [0]. There is a check before the line.


https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/mem.d#L20 
[0]


I haven't run the code but which pointer is null? Try adding this check 
as well:


  auto node = this.list.getFisrtFreeOrAdd(memViewLen);
  assert(node !is null);

Ali


Re: this is null

2019-03-09 Thread ANtlord via Digitalmars-d-learn

On Saturday, 9 March 2019 at 20:04:53 UTC, Paul Backus wrote:


struct S
{
bool isThisNull() { return  is null; }
}

void main()
{
import.std.stdio;
S* p = null;
writeln((*p).isThisNull); // true
}

Interactive version: https://run.dlang.io/is/fgT2rS


Anyway, thank you! I didn't know about the feature.


Re: this is null

2019-03-09 Thread ANtlord via Digitalmars-d-learn

On Saturday, 9 March 2019 at 20:04:53 UTC, Paul Backus wrote:


You can end up with a null `this` reference if you dereference 
a null pointer to a struct and then call a method on the 
result. For example:




I can but my reference is not null before calling. Take a look at 
the line of code [0]. There is a check before the line.


https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/mem.d#L20 [0]


Re: this is null

2019-03-09 Thread Paul Backus via Digitalmars-d-learn

On Saturday, 9 March 2019 at 19:18:38 UTC, ANtlord wrote:
Hello everyone! I've encountered the problem which I already 
encountered before. Unfortunately, I had no time in the 
previous time to report and to talk about it. So I decided to 
play making my own "malloc" function in pure D (betterC) at 
this time. And I encountered the issue one more time. `this` 
can be null. How? Please take a look at my project [0]. It gets 
the current heap break and tries to increase via a free list. 
So the segfault I meet happens there [1]. Tell me, please. What 
do I wrong?


[0] https://github.com/ANtlord/deadmemory
[1] 
https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/freelist.d#L56


You can end up with a null `this` reference if you dereference a 
null pointer to a struct and then call a method on the result. 
For example:


struct S
{
bool isThisNull() { return  is null; }
}

void main()
{
import.std.stdio;
S* p = null;
writeln((*p).isThisNull); // true
}

Interactive version: https://run.dlang.io/is/fgT2rS


Re: Phobos in BetterC

2019-03-09 Thread Sebastiaan Koppe via Digitalmars-d-learn

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?


Re: Make Dub output *.di files

2019-03-09 Thread bitwise via Digitalmars-d-learn

On Saturday, 9 March 2019 at 19:08:22 UTC, bitwise wrote:

On Saturday, 9 March 2019 at 18:39:29 UTC, bitwise wrote:
Is it possible to get Dub to output import headers for 
compiled D files?


I found this, which almost works:

"dflags": [ "-H", "-Hdimport", "-op" ]

The only problem is that Dub runs *above* the source directory, 
resulting in all my import files being nested in /source/..


So can I get Dub to run the compilation from the /source 
directory instead?


Or is there a way to tell Dub to generate the headers for each 
file some other way?


For now, I guess this will work:

"dflags": [ "-H", "-Hdimport", "-op" ],
"postBuildCommands": [
"mv import/source/root_pkg import/root_pkg",
"rm -rf import/source"
]

A platform independent solution would be preferred.




this is null

2019-03-09 Thread ANtlord via Digitalmars-d-learn
Hello everyone! I've encountered the problem which I already 
encountered before. Unfortunately, I had no time in the previous 
time to report and to talk about it. So I decided to play making 
my own "malloc" function in pure D (betterC) at this time. And I 
encountered the issue one more time. `this` can be null. How? 
Please take a look at my project [0]. It gets the current heap 
break and tries to increase via a free list. So the segfault I 
meet happens there [1]. Tell me, please. What do I wrong?


[0] https://github.com/ANtlord/deadmemory
[1] 
https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/freelist.d#L56


Re: Make Dub output *.di files

2019-03-09 Thread bitwise via Digitalmars-d-learn

On Saturday, 9 March 2019 at 18:39:29 UTC, bitwise wrote:
Is it possible to get Dub to output import headers for compiled 
D files?


I found this, which almost works:

"dflags": [ "-H", "-Hdimport", "-op" ]

The only problem is that Dub runs *above* the source directory, 
resulting in all my import files being nested in /source/..


So can I get Dub to run the compilation from the /source 
directory instead?


Or is there a way to tell Dub to generate the headers for each 
file some other way?


Re: Distinguish float and integer types from string

2019-03-09 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 9 March 2019 at 18:11:09 UTC, Jacob Shtokolov wrote:w
One of the task was to take a string from STDIN and detect its 
type.


The way I'd do this is a very simple loop:

enum Type { String, Float, Int }
if(str.length && str[0] == '-') {
   str = str[1 .. $];
}
Type type = str.length ? Type.Int : Type.String;
foreach(ch; str) {
   if(ch == '.' && type = Type.Int)
type = Type.Float;
   else if(ch < '0' || ch > '9') {
   type = Type.String;
   break;
   }
}

And if you need to support other details, add them on top of 
that. For example, exponents on floats may be a second clause 
like how I put negative ahead.


You may also choose to use a regular expression though I think 
that is overkill for this.



if (data.isNumeric) {


There are several caveats on that isNumeric function: it sees if 
something looks like a D numeric literal, which might not be what 
you want. For example, isNumeric("1UL") passes because the U and 
L suffixes are allowed in D literals...



But I think that's ugly. The thing is that in PHP, for example, 
I would do that like this:


```
if (is_integer($data)) {


Simiarly, this also will not od what you want. is_integer("1") 
will return false. "1" is of type string. Those functions check 
the dynamic type tag, not the contents of a string.


(actually, you arguably can just always return "string" cuz stdin 
is basically just a string or a binary stream anyway :P )


PHP's is_numeric returns true for both integer and floating point 
things, similarly to D's...


Make Dub output *.di files

2019-03-09 Thread bitwise via Digitalmars-d-learn
Is it possible to get Dub to output import headers for compiled D 
files?


Distinguish float and integer types from string

2019-03-09 Thread Jacob Shtokolov via Digitalmars-d-learn

Hi,

Recently, I was trying to solve some funny coding challenges 
(https://www.techgig.com).
The questions were really simple, but I found it interesting 
because the website allows to use D.


One of the task was to take a string from STDIN and detect its 
type.
There were a few options: Float, Integer, string and "something 
else" (which, I think, doesn't have any sense under the scope of 
the task).


Anyway, I was struggling to find a built-in function to 
distinguish float and integer types from a string.


I came to the following solution:

```
import std.stdio;
import std.range;
import std.conv;
import std.string;
import std.format;

immutable msg = "This input is of type %s";

void main()
{
string type;
auto data = stdin.byLine.takeOne.front;

if (data.isNumeric) {
type = data.indexOf(".") >= 0 ? "Float" : "Integer";
}
else {
type = "string";
}

writeln(msg.format(type));
}
```

But I think that's ugly. The thing is that in PHP, for example, I 
would do that like this:


```
if (is_integer($data)) {
//...do smth
}
else if (is_float($data)) {
//...do smth
}
else {
//...do smth
}
```

I tried to use std.conv.to and std.conv.parse, but found that 
they can't really do this. When I call `data.to!int`, the value 
of "123.45" will be converted to int!


Is there any built-in way to detect these types?

Thanks!


Re: Phobos in BetterC

2019-03-09 Thread 9il via Digitalmars-d-learn

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


Re: Phobos in BetterC

2019-03-09 Thread Nicholas Wilson via Digitalmars-d-learn

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.




Re: Phobos in BetterC

2019-03-09 Thread Sebastiaan Koppe via Digitalmars-d-learn

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.


Re: Phobos in BetterC

2019-03-09 Thread Nicholas Wilson via Digitalmars-d-learn

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.


Re: Phobos in BetterC

2019-03-09 Thread Laeeth Isharc via Digitalmars-d-learn

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.




Re: Phobos in BetterC

2019-03-09 Thread Bastiaan Veelo via Digitalmars-d-learn

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.


Re: Aliasing a mixin (or alternative ways to profile a scope)

2019-03-09 Thread Dennis via Digitalmars-d-learn

On Friday, 8 March 2019 at 11:42:11 UTC, Simon wrote:
Thanks, this works flawlessly. Out of interest: what is the 
"enum" doing there? I had the exact same behaviour in a 
function before, that I only called at compile-time, so why did 
it complain then? Can I somehow tell the compiler that a 
function should only be available at compile-time?


The enum (which in D is not only for enumerations, but also for 
"manifest constants") ensures it is evaluated at compile-time, 
since things are only evaluated at compile-time if they have to.

See also Adam D. Ruppe's post in this thread:
https://forum.dlang.org/post/blaawtdhljjantvga...@forum.dlang.org