BetterC, int to string?

2021-06-18 Thread Mike Brown via Digitalmars-d-learn

Hi all,

I would like to convert a D string to an int - im doing this in a 
compile time function as well. conv throws an error due to it 
using TypeInfo?


How would I do this?

Kind regards,
Mike


UDAs on function calls or return locations?

2021-06-14 Thread Mike Brown via Digitalmars-d-learn

Hi all,

Is it possible to add a UDA onto either the function call, and 
detect its present? or onto the variable that the result will go 
to?


E.g.
auto int x = function() @UDA;

int function() {
  if (hasUDA(call_site)) { ... }
}

or

@UDA int y = function();

int function() {
  if (hasUDA(RESULT)) { ... }
}

Kind regards,
Mike Brown


Re: Problem with Exceptions in Constructor

2021-06-12 Thread Mike Brown via Digitalmars-d-learn

On Saturday, 12 June 2021 at 02:19:21 UTC, Mike Parker wrote:

On Friday, 11 June 2021 at 20:09:22 UTC, Mike Brown wrote:



The error is flagged on the writeln(e.msg). Do I need to do 
something special to pass a string to an exception? dup?




No, that code is fine. You're passing a string literal, so 
there's no need for a dup.


Hi Mike,

Thank you - OK, I had another issue that was related to an out of 
date LDC2 and Druntime. But that now looks to be sorted.


I have set up a VM of Linux Mint. The code doesn't raise an error 
on there. I guess this might be a bug in the windows release? I 
will log it


Kind regards,
Mike Brown


Re: Unittests not firing?

2021-06-12 Thread Mike Brown via Digitalmars-d-learn

On Friday, 11 June 2021 at 21:10:32 UTC, H. S. Teoh wrote:
On Fri, Jun 11, 2021 at 02:05:31PM -0700, H. S. Teoh wrote: 
[...]
Huh, that doesn't look right. This was fixed since June last 
year, so it *should* have made it into the latest compiler 
releases already. Unless this one was missed somehow (but I 
doubt it).

[...]

Just checked on LDC 1.26.0, the message has already been fixed.
 I seriously doubt the beta would have the fix reverted 
somehow; this seems to be a sign that your druntime is indeed 
out-of-sync with your compiler.  You should check your system 
for stray stale copies of druntime.



T


Hi Toeh

Thank you, found the old druntime and this is now fixed. Thanks 
again!


Re: Unittests not firing?

2021-06-11 Thread Mike Brown via Digitalmars-d-learn

On Friday, 11 June 2021 at 16:28:48 UTC, H. S. Teoh wrote:
On Fri, Jun 11, 2021 at 03:20:49PM +, Mike Brown via 
Digitalmars-d-learn wrote:
On Friday, 11 June 2021 at 15:13:17 UTC, rikki cattermole 
wrote:

[...]
Right OK, mine says 1/1 unittests failed - but this should say 
Modules?


I will interpret it as Modules, ty!


This is a bug that was fixed in more recent versions of 
druntime. Basically it was a misleading message because the 
count is the number of modules, not the number of individual 
tests.



T


Hi T,

OK, thank you - Im using LDC 1.27.0-beta1 which looks to be the 
newest I can find, is there a way to update the druntime manually?


I've problems with exceptions that might be related to me being 
out of date?


Kind regards,
Mike Brown


Problem with Exceptions in Constructor

2021-06-11 Thread Mike Brown via Digitalmars-d-learn

Hi all,

AddressSanitizer is throwing an access-violation error - I've 
managed to boil it down to this example:


```d
import std.stdio;

class example: Exception {
	this(string msg, string file = __FILE__, size_t line = __LINE__) 
{

super(msg, file, line);
}
}

class test {
this() {
throw new example("this is a test");
}
}

void main() {
try {
auto t = new test();
} catch (example e) {
writeln(e.msg);
}
}
```

Compiled with:
ldc2.exe -g -fsanitize='address' .\test.d -of='test.exe'

The error is flagged on the writeln(e.msg). Do I need to do 
something special to pass a string to an exception? dup?


Kind regards,
Mike Brown


Re: Unittests not firing?

2021-06-11 Thread Mike Brown via Digitalmars-d-learn

On Friday, 11 June 2021 at 15:13:17 UTC, rikki cattermole wrote:


rdmd -main -unittest file.d

```d
import std.stdio;

unittest
{
writeln("first");
}

unittest
{
writeln("second");
assert(0);
}
```

Output:

```
first
second
onlineapp.d(11): [unittest] unittest failure
1/1 modules FAILED unittests
```

The first assert to execute should kill the rest of the 
unittests.


Right OK, mine says 1/1 unittests failed - but this should say 
Modules?


I will interpret it as Modules, ty!


Unittests not firing?

2021-06-11 Thread Mike Brown via Digitalmars-d-learn

Hi all,

I'm testing the unittest features of D, and having some issues. I 
only seem able to fire one unittest, e.g.


module test;

unittest {
assert(0);
}

unittest {
assert(0);
}

If I run,
rdmd -g -unittest -main "test.d"

It returns,
1/1 unittests FAILED
test.d(4): [unittest] unittest failure

Im using rdmd as I want a simple command that will pull in 
dependancies to run these tests. Am I right in using rdmd?


I have also tried ldc2 and getting the same results. What am I 
missing?


Kind regards,
Mike Brown


Creating a static library

2021-06-11 Thread Mike Brown via Digitalmars-d-learn

Hi all,

My goal is to modularise my code better.

I think I'd like to make a static library, this will primarily to 
be used in other D projects.


https://dlang.org/articles/dll-linux.html mentions the GC and 
linking requirements. I am happy to not use the GC for this 
library, what is the best way to turn this off for the entire 
library?


I would like to be able to select a library at link time, 
libraries with the same interface but different implemenations - 
to build different versions of my project (e.g. Windows, Linux 
etc).

It would be useful to understand how in D the best way to this?

Is there a good tutorial or information on the above?

Kind regards,
Mike


Re: opDispatch not working

2021-06-05 Thread Mike Brown via Digitalmars-d-learn

On Saturday, 5 June 2021 at 15:23:09 UTC, drug wrote:

05.06.2021 18:15, Mike Brown пишет:

[...]


incomplete `cast` I would say. You did:
```D
mixin("cast(T*)obj."~name)(vals);
```
instead of
```D
mixin("(cast(T*)obj)."~name)(vals);
```

i.e. you try to call method `test` using a pointer to void and 
then try to cast it to a pointer to T.


Thank you!!


opDispatch not working

2021-06-05 Thread Mike Brown via Digitalmars-d-learn

Hi All,

Im still experimenting with WASM and D - I really would like to 
be able to use reference types. I think I can get there with the 
metaprogramming abilities in D.


I'm trying to get the foundations right, I need a reference-like 
handle - which I've named "ptr". I'm trying to see how I can 
dispatch method calls to the underline object, I've tried 
opDispatch.


https://godbolt.org/z/a1Tc4ErTv

But I can't get it to fire? What am I missing?

P.S. Is it also possible to provide a way to implicitly cast from 
ptr!byte to ptr!\ ?


Kind regards,
Mike Brown


Re: Can Metaprogramming Help Here?

2021-02-26 Thread Mike Brown via Digitalmars-d-learn

On Friday, 26 February 2021 at 20:42:50 UTC, H. S. Teoh wrote:
On Fri, Feb 26, 2021 at 11:37:18AM -0800, H. S. Teoh via 
Digitalmars-d-learn wrote:

> [...]

[...]

Alright, here's an actual working example. Instead of using 
classes, I decided to use templates instead, but the underlying 
concept is the same:


[...]


Hi T,

Thank you so much for that, I appriciate the time and hard work 
you've put in for this! I'm sure the code examples above will be 
workable for my needs.


Thanks again
Mike


Re: Can Metaprogramming Help Here?

2021-02-24 Thread Mike Brown via Digitalmars-d-learn

On Tuesday, 23 February 2021 at 22:55:53 UTC, H. S. Teoh wrote:
On Tue, Feb 23, 2021 at 10:24:50PM +, Mike Brown via 
Digitalmars-d-learn wrote:

Hi all,

Im porting some C++ code, which has a mess of a section that 
implements prime number type id's. I've had to smother it to 
death with test cases to get it reliable, I think 
metaprogramming that D provides is the better solution - Id 
rather not reimplement that C++ mess ideally.


Try something like this:

-snip-
import std;

int[] firstNPrimes(int n) {
// FIXME: replace this with actual primes computation
return [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 ];
}

string genEnum(string enumName, idents...)() {
string code = "enum " ~ enumName ~ " {";
auto primes = firstNPrimes(idents.length);
foreach (i, ident; idents) {
code ~= ident ~ " = " ~ primes[i].to!string ~ ", ";
}
code ~= "}";
return code;
}

template PrimeEnum(idents...) {
mixin(genEnum!("PrimeEnum", idents));
}

alias MyEnum = PrimeEnum!(
"unknown", "newline", "identifier", "var", "user_defined",
);

void main() {
writefln("%(%d\n%)", [
MyEnum.unknown,
MyEnum.newline,
MyEnum.identifier,
MyEnum.var,
MyEnum.user_defined
]);
}
-snip-


You can substitute the body of firstNPrimes with any standard 
prime-generation algorithm. As long as it's not too 
heavyweight, you should be able to get it to compile without 
the compiler soaking up unreasonable amounts of memory. :-D  If 
you find the compiler using up too much memory, try 
precomputing the list of primes beforehand and pasting it into 
firstNPrimes (so that the CTFE engine doesn't have to recompute 
it every time you compile).


Note that PrimeEnum can be used to generate any number of enums 
you wish to have prime values.  Or if you replace the call to 
firstNPrimes with something else, you can generate enums whose 
identifiers map to any integer sequence of your choosing.



T


Hi T,

Thank you for the reply. Im struggling extending this to get the 
nesting working.


I'm trying something like:

string entry(string i, string[] inherit = []) {
return i;
}

alias token_type2 = PrimeEnum!(
entry("unknown"),
entry("newline"),
entry("identifier"),
entry("var", ["identifier"]),
entry("userDefined", ["identifier"])
);

Its worth noting that multiple inherited bases are needed too.

But I can't get those functions contexts linking, can I pass a 
function pointer as lazy into the PrimeEnum!() template?


Would it be easier to just parse the text at once into a single 
templating function?


Kind regards,
Mike Brown


Can Metaprogramming Help Here?

2021-02-23 Thread Mike Brown via Digitalmars-d-learn

Hi all,

Im porting some C++ code, which has a mess of a section that 
implements prime number type id's. I've had to smother it to 
death with test cases to get it reliable, I think metaprogramming 
that D provides is the better solution - Id rather not 
reimplement that C++ mess ideally.


A simplified example,

enum token_type {
  endOfFile = 2,

  unknown = 3,
  newline = 5,
  identifier = 7,
userDefined = 13 * identifier,
// Keyword
var = 17 * identifier,
uses = 19 * identifier,
constructor = 23 * identifier,
do_ = 29 * identifier,
end_ = 31 * identifier,

  operator = 11,
copyAssignment = 13 * operator

  // LAST ID = 13
}

Its effectly a tree, with the starting child node being one more 
than the last child number of the parent.


Is it possible to produce this via metaprogramming? I'm thinking 
mixin? Do I need to make a function that returns D code that will 
produce the enum structure above? I'm unsure as to how I would be 
able to get the LAST ID of the siblings at each level, and I 
would describe this with some kind of tree/function call?


e.g. Pseudo-code

enum token_type = prime_ids(
  value("endOfFile"),
  value("unknown"),
  value("newline"),
  branch("identifier",
value("userDefined"),
value("var"),
value("uses"),
value("constructor"),
value("do_"),
value("end_")
  ),
  ...
;

What would be the direction I need to go in to achieve this?

Kind regards,
Mike


Re: Strings and Slices

2021-02-20 Thread Mike Brown via Digitalmars-d-learn

On Saturday, 20 February 2021 at 19:28:00 UTC, Mike Brown wrote:
On Thursday, 18 February 2021 at 21:08:45 UTC, Adam D. Ruppe 
wrote:

[...]


Thank you. Is there a standardised type to make "mark"? size_t 
or is a normal integer suitable?


Ah, and whats the recommended way to iterate over a slice using a 
mark? Can I get the current iteration point from a foreach loop?


Re: Strings and Slices

2021-02-20 Thread Mike Brown via Digitalmars-d-learn
On Thursday, 18 February 2021 at 21:08:45 UTC, Adam D. Ruppe 
wrote:

On Thursday, 18 February 2021 at 20:47:33 UTC, Mike Brown wrote:

[...]


My c++ is rusty af but yes I think so.

A d slice is `struct slice { size_t length; T* ptr; }` so when 
in doubt just think back to what that does.



[...]


And that makes this ref iffy. Since it is already passed as a 
ptr+length pair, you rarely need ref on it. Only when you'd use 
a char** in C; that is, when you can reassign the value of the 
pointer and have the caller see that change (e.g. you are 
appending to it). If you're just looking, no need for ref.



[...]


I do it in two steps:

piece = input[0 .. mark]; // get piece out
input = input[mark .. $]; // advance the original slice


Note that such operations are just `ptr += mark; length -= 
mark;` so they are very cheap.


Thank you. Is there a standardised type to make "mark"? size_t or 
is a normal integer suitable?


Strings and Slices

2021-02-18 Thread Mike Brown via Digitalmars-d-learn

Hi All,

I'm rebuilding a C++, and the beginning section is a lexer that 
uses strings, and string_view.


Is slices comparable to a string_view?

The architecture of the lexer is a single layer (Non-)FSM Lexer. 
Basically a main loop, checking the first letter of the current 
input position, which then calls a function/lambda to continue 
from there.


string lex_identifier(ref string input) {
  ...
}

while(!input.empty()) {
  if (isAlpha(input.front)) {
auto tmp = lex_identifier(input);
  }
}

I'm passing in a ref because I ideally want to iterate over a 
string, and to produce a slice to the lexeme. This needs a way to 
create a mark at a given point, and have an iteration point. 
These marks seem logically to be the slice start and end.


Is there a better way to make these slices?
The ref doesn't work well with unittests that pass in a literal, 
is there an easier way than creating a temp var for the input?


In the body of the lex_identifier, i am using drop(). This 
doesn't seem to do what I thought it did. I want to create a 
slice from the beginning of a ref slice upto a given mark, and 
move the beginning point of that ref slice to that mark also.


what is the best way to achieve this?

Kind regards,
Mikey





Re: BetterC + WASM Update

2020-09-03 Thread Mike Brown via Digitalmars-d-learn

Hi again,

I now understand the issues at play. I get linking errors to the 
druntime features. I now understand the earlier replies.


I was able to resolve all but TypeInfo errors from the suggested 
druntime replacements. Coming from a C++ background - I’m not 
even sure how I’m using TypeInfo features so I don’t even know 
where to begin on rewriting my code to not use it. Also not sure 
how I’d go able altering the druntime to proceed. Happy to leave 
wasm with D at that.


I did however, get taken in by how small and precise the D code 
was compared to my C++ equivalent. The templating is beautiful, 
I’m now considering writing in D rather than C++ as the smaller 
and clearer code would be of real benefit.


Thanks again to all replies. I read all linked articles, which 
were all great reads. Hope wasm support arrives officially one 
day soon.


Kind Regards
Mike Brown



Re: BetterC + WASM Update

2020-09-01 Thread Mike Brown via Digitalmars-d-learn

Hi All,

Thank you for your replies. My tests have been via compiler 
explorer.


https://godbolt.org/z/4f9nKj I have done this without -BetterC, 
as the dynamic array fails. I guess that uses GC? Is there an 
alternative that works with BetterC for dynamic arrays?


Thank you for the other WASM links, I initially only need to call 
a single function, compute, and return a string. But the idea of 
the dlang side interacting with the JS side sounds useful and 
promising.


Is the use of classes in my example a special case? And more 
expanded usage will break?


Kind regards,
Mike Brown



BetterC + WASM Update

2020-08-19 Thread Mike Brown via Digitalmars-d-learn

Hi all,

I'd like to make a WASM project, and looking into options.

I can see that LDC supports WASM output, and I believe this 
requires BetterC to be enabled? The documentation states that 
classes are not supported by BetterC, but its preferable/required 
for me.


I have done some tests, and it appears that classes are supported 
(LDC 1.22.0)? Is it possible to get an update on what is and 
still isn't supported in BetterC?


After a stable experience, so if you can also let me know if 
these features are beta etc too?


Kind regards,
Mike



Re: Setting up Dlang Environment

2019-08-25 Thread Mike Brown via Digitalmars-d-learn

On Saturday, 17 August 2019 at 21:26:27 UTC, Andre Pany wrote:

On Saturday, 17 August 2019 at 20:42:41 UTC, Mike Brown wrote:

Hi All,

I've tried installing and setting up Dlang a few times now. 
I'm struggling to get it reliable and to a decent environment 
in place.


I don't really want to make this thread about specific issues 
- Id very much like to know if there's a decent tutorial or 
guide to setup Dlang?


* I'd like to get the basic's down solid (One of my issues is 
a linker issue on release but not debug, etc)


* A decent IDE which allows debugging, understanding the stack 
traces, step through etc?


* The package management, Should I be using DUB or doing 
packaging etc with make files etc?


I appriciate some of this is down to choice, but an idiots 
guide and recommended practises - to setting up the above so I 
can just get on with coding would be great.


Kind Regards,
Mike


Hi Mike,

Which OS do you target? Setting up DMD on e.g. Ubuntu is just 
installing package build-essentials and installing the dmd deb 
file.
On Windows you just extract the 7z file and add the bin path to 
th PATH environment variable.


I tried so far IntelliJ IDEA and Visual Studio Code. Both are 
multi platform and free to use. Both supports debugging but 
here the details really depends on your OS.
For Visual Studio Code there are even different D plugins 
available. I used vscode-dlang and it was just a fantastic 
experience.
(I still prefer the kind of development flow IntelliJ provides, 
but Intellij D support is more complex to setup).


If you target Windows, there is also Visual Studio, this plugin 
has no dub integration as far as I know.


Kind regards
Andre


Hi Andre,

Thank you for your reply! Windows is my target system. I have 
(hopefully) installed the d compiler toolchain, and Visual Studio 
Code - along with the recommended extension.


I'm wondering on the build systems, I have read up on rdmd and 
dub. I think these are two seperate competing build systems? 
Which should I use, for which situations?


I'd like to use Vibe, so I guess I need to use DUB? Can I use 
rdmd with dub dependencys?


Kind Regards,
Mike


Setting up Dlang Environment

2019-08-17 Thread Mike Brown via Digitalmars-d-learn

Hi All,

I've tried installing and setting up Dlang a few times now. I'm 
struggling to get it reliable and to a decent environment in 
place.


I don't really want to make this thread about specific issues - 
Id very much like to know if there's a decent tutorial or guide 
to setup Dlang?


* I'd like to get the basic's down solid (One of my issues is a 
linker issue on release but not debug, etc)


* A decent IDE which allows debugging, understanding the stack 
traces, step through etc?


* The package management, Should I be using DUB or doing 
packaging etc with make files etc?


I appriciate some of this is down to choice, but an idiots guide 
and recommended practises - to setting up the above so I can just 
get on with coding would be great.


Kind Regards,
Mike