Re: How can I check a newly set ref value

2021-10-23 Thread Adam D Ruppe via Digitalmars-d-learn
On Saturday, 23 October 2021 at 20:14:25 UTC, solidstate1991 
wrote:
If I wanted to check whether the assigned value is within a 
range, and I want to throw a certain exception if outside of 
it, then how I can do that?


You can't with ref, you will need to do separate getter and 
setter properties for that.


Re: Is there an alternative to "__FUNCTION__" that gives the actual function symbol and not the name as a string?

2021-10-23 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 23 October 2021 at 18:23:47 UTC, Simon wrote:
For debugging purposes, I have built a mixin that will, when 
declared inside a function, output code to the console that 
will reproduce the exact function call.


Sounds like what you really want is

https://github.com/dlang/dmd/pull/13071

but the PR there is not merged :(

the other way though is the string mixin with the parent {} thing 
it is pretty ugly.


Re: Unexpected path of execution

2021-10-19 Thread Adam D Ruppe via Digitalmars-d-learn

On Tuesday, 19 October 2021 at 16:38:50 UTC, Adam D Ruppe wrote:

test `i + 3 < line.length` instead


BTW this is my personal preference, I have gotten into the habit 
of using this style tests with lengths all the time now.


Re: Unexpected path of execution

2021-10-19 Thread Adam D Ruppe via Digitalmars-d-learn

On Tuesday, 19 October 2021 at 16:20:39 UTC, Charles Hixson wrote:

given this code fragment:

            if    (i < (line.length - 3) )

in c4: i = 0, line.length = 2


line.length is an unsigned value. Arithmetic on an unsigned thing 
is still unsigned.


So UNSIGNED 2 - 3 is not -1, but instead it is size_t.max since 
it rolls over.


Then the comparison also becomes unsigned. So 0 < size_t.max is 
true, meaning it goes in there.



you should be able to fix it if you do

 if(i < (cast(int) line.length) - 3)

to force it to become signed. But this isn't great either. You 
probably want to  change the code to avoid going negative in the 
first place. Maybe test `i + 3 < line.length` instead. Or maybe 
`if(!(i > ... wahtever that is)`. You get the idea im brain 
farting.



I personally hate that array.length is unsigned. And I hate that 
the signed/unsigned mixing prefers unsigned instead of just about 
anything else. What a pain in the butt. But that's how it is.


Re: I need a detailed document about druntime.

2021-10-11 Thread Adam D Ruppe via Digitalmars-d-learn
On Monday, 11 October 2021 at 15:18:11 UTC, Ferhat Kurtulmuş 
wrote:

"Each call to initialize must be paired by a call to terminate.'


It is so the refcount works out.

When you call initialize, it does something like:

if(refcount == 0)
   actually intialize; // calls constructors etc
refcount++;


When you call terminate, it does:

refcount--;
if(refcount == 0)
   actually terminate; // calls destructors etc



If you don't pair it, the refcount will be off, so the next call 
to terminate will still see ref > 0 and not actually terminate.


The D main inserts a call to init before main and a call to 
terminate after main automatically.


Re: I need a detailed document about druntime.

2021-10-11 Thread Adam D Ruppe via Digitalmars-d-learn
On Monday, 11 October 2021 at 14:56:19 UTC, Ferhat Kurtulmuş 
wrote:
What I want is to bypass runtime initialization if it is 
already initialized.


That what it does by itself, you can call it and it has an 
internal count.




Re: Traits in a template enum

2021-10-10 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 10 October 2021 at 12:39:17 UTC, Some Guy wrote:

I have this enum to get the type


enums hold values, not types.

try alias instead


Re: What is a "comma expression"?

2021-10-10 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 10 October 2021 at 12:01:33 UTC, rempas wrote:

return *ret = v, 1;


The comma expression in C is a combination of two things but in 
the context of another thing. Well that's not very good, but like 
there's statements and expressions. Statements do something but 
do not have a value. So `if() {}` and `return` and `for() {}` are 
statements, because they don't have a value you can assign to a 
variable.


Expressions do something but DO have a value and thus you can 
assign it to a variable.


a = if (true) { a } ; // not permitted because if is a statement

a = 5 + 4; // OK, 5+4 is an expression and thus has a value


This is a slight simplification but generally true. You'll notice 
in some languages the if assign does work; in those languages you 
have an if expression rather than an if statement.


The language grammar has rules for where statements are allowed 
and where expressions are allowed and you can't mix and match 
(unless the grammar has two rules, one allowing each type).



Anyway, the C comma expression is a combination of two other 
expressions where the first one is evaluated, but its value 
discarded, then the second part becomes the value of the whole 
expression.


a = 1, 2; // the 1 is discarded, so a == 2


It is actually the same as writing out:

1; // evaluate this then discard it
a = 2; // eval this and actually keep the value


The difference being that it is allowed in an expression context.


So that `return *ret = v, 1;`

Could just be written

*ret = v; // this is eval'ed and value discarded
return 1; // second part value is kept


And idk why they didn't just do that here. But the C thing is 
most commonly seen in a for loop:


for(a = 0, b = 0; a < 5; a++, b++) {}


because the grammar only allows an expression in each piece of 
the for loop, so you can't separate the two steps by semicolons 
like you do in different contexts.


Can someone explain what comma expressions are and why they 
don't work in D (or if the only doesn't work as returned values 
from a function)?


D considers it a bit obscure and special-case to use the comma 
for. There was talk a while ago about making the comma do 
something else instead, but this never materialized.


But you can always write it out longer form, perhaps wrapping it 
in a function.


// what I would do here:
*ret = v;
return 1;

// or if you absolutely had to have it in an expression context:

return (){ *ret = v; return 1; }();


The nested function there explicitly does what the comma operator 
does and is allowed in expression context as well.


Re: avoid codegen pass

2021-10-02 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 2 October 2021 at 13:24:19 UTC, Padlev wrote:

-o-
how to run only semantic and avoid codegen to have a quicker 
run?


-o- does skip codegen already


Re: 0 cost template instantiation

2021-09-29 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 30 September 2021 at 02:09:50 UTC, Hipreme wrote:
I could reduce by almost 100kb of code by instead of using the 
former option, I use:


yes this is what id o you can list the Ts in an interface too. 
see my simpledisplay.d `interface GL` for example



I do still don't think that this should be the answer, as the 
alias problem is not yet  solved. It takes a lot of size for 
functions that could be only syntactic sugar, where C would be 
a lot better


well it must generate functions for each thing there by 
definition and since you call them at runtime it can't cut them 
out either. so this is kinda meh


you MIGHT  be able to do a tempalte that forwards to a single 
runtime function though. but i seriously need to g2g maybe can 
look tomorrow


Re: 0 cost template instantiation

2021-09-29 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 30 September 2021 at 02:02:19 UTC, Hipreme wrote:

Instead of writing

myFunction = cast(typeof(myFunction))_loadSymbol(_dll, 
"myFunction");


I could write

loadSymbol!myFunction;

But if no other way is found of doing that, I will do the 
massive rewriting.


---

void function() x;

void load(void* thing, string name) {
   // this lhs cast is the magic, the rhs cast unneeded in 
reality cuz GetProcAddress returns void* naturally

* cast(void**) thing = cast(void*) 0xdeadbeef;
}

void main() {
// no cast here but yes repeated name
load(, "foo");

 // casts needed here just to do the comparison
assert(cast(void*) x == cast(void*) 0xdeadbeef);
}

---


A little void casting inside the function can do the trick. You 
will have to pass the name separately though.



im off to bed ttyl


Re: abstract classes and interfaces

2021-09-27 Thread Adam D Ruppe via Digitalmars-d-learn
On Monday, 27 September 2021 at 16:20:59 UTC, Steven 
Schveighoffer wrote:

That's a regression. In 2.092.1, it reports:


aye known bug here

https://issues.dlang.org/show_bug.cgi?id=21321

maybe once dmd can compile C code we'll fix it so it compiles D 
code correctly again.




Re: Inheriting function template from super class

2021-09-27 Thread Adam D Ruppe via Digitalmars-d-learn

On Monday, 27 September 2021 at 14:54:41 UTC, Alexey wrote:
I'm not sure if this is my incorrect code or incorrect dmd 
behavior.



This is by design, overloads only consider things declared in the 
same place.


see here https://dlang.org/articles/hijack.html


Re: Where is the core.sys (and std.windows) documentation?

2021-09-26 Thread Adam D Ruppe via Digitalmars-d-learn

Some are available on my website

http://druntime.dpldocs.info/core.sys.html

but the source doesn't include much so it is a bit sparse and all 
the version overloads can be awkward to read.


Re: How to do "C++ classes"?

2021-09-20 Thread Adam D Ruppe via Digitalmars-d-learn
On Monday, 20 September 2021 at 15:35:02 UTC, Ferhat Kurtulmuş 
wrote:

I thought it's stack-allocated and scoped.


It is.

But when I try to return a class instance from a function, it 
still works?


dmd only makes that an error if you specify `@safe` and i think 
`-dip1000`. Try adding one or both of those and recompiling and 
see what happens.


Note that even if the compiler doesn't error on it, it is 
undefined behavior to return the stack reference so be sure to 
treat it right.


Re: How to do "C++ classes"?

2021-09-18 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 18 September 2021 at 15:38:38 UTC, rempas wrote:
I'm seeing in the page about "BeterC" and in the part about the 
[retained 
features](https://dlang.org/spec/betterc.html#retained), the 
#11 says about "COM classes and C++ classes". What are the "C++ 
classes"? I tried to create a class using "extern(C++)" but 
this didn't worked. Can someone make an example on that?


extern(C++)
class Foo {}

void main() {
scope Foo foo = new Foo();
}




Re: Program crash: GC destroys an object unexpectedly

2021-09-14 Thread Adam D Ruppe via Digitalmars-d-learn

On Tuesday, 14 September 2021 at 12:42:51 UTC, eugene wrote:

I understand your idea, but even if this will help, the question
remains - why that particular object is so special for GC.


I had a problem just like this before because I was sending 
objects through the pipe. And while they were in the pipe - after 
send but before receive on the other side - it was liable to be 
collected.


idk your code though if you have a separate reference to the 
object you should be ok.


but here's the comment from my code when i broke it

https://github.com/adamdruppe/arsd/blob/master/eventloop.d#L180


Re: GDC - program runs in one thread, DMD - in 4 threads, why?

2021-09-10 Thread Adam D Ruppe via Digitalmars-d-learn

btw why do the threads cause you trouble?


Re: GDC - program runs in one thread, DMD - in 4 threads, why?

2021-09-10 Thread Adam D Ruppe via Digitalmars-d-learn

On Friday, 10 September 2021 at 09:27:49 UTC, eugene wrote:

What are these extra threads for?
GC?

So I think it is **very aggressive usage** of DList that causes 
this.


Yeah, in newer versions, when the garbage collector does its 
first collect, it spawns some helper threads to speed up its mark 
process. When the GC is not actively marking, those extra threads 
are 100% idle, so they don't affect the rest of your program.



Can this (really unwanted) behavior be disabled in DMD?
I do not want to have multiple threads,


here's the page on it
https://dlang.org/spec/garbage.html#gc_config

You either pass as an argument *to your application*

--DRT-gcopt=parallel:0

or to make it permanent, in your source file with the main() 
function, add this:


extern(C) __gshared string[] rt_options = [ "gcopt=parallel:0" ];


and it will stop doing that.


Re: How to simply parse and print the XML with dxml?

2021-09-09 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 9 September 2021 at 17:17:23 UTC, tastyminerals 
wrote:
Maybe I missed something obvious in the docs but how can I just 
parse the XML and print its content?


idk how to use dxml but my dom.d makes these things trivial

http://arsd-official.dpldocs.info/arsd.dom.html

https://github.com/adamdruppe/arsd/blob/master/dom.d
https://code.dlang.org/packages/arsd-official%3Adom

if you're familiar with javascript you'll find a lot of 
similarities with my api there.


for strict xml mode you just use `new XmlDocument` instead of 
`new Document`


Re: Curious effect with traits, meta, and a foreach loop ... mystifies me.

2021-09-08 Thread Adam D Ruppe via Digitalmars-d-learn
On Tuesday, 7 September 2021 at 17:47:15 UTC, james.p.leblanc 
wrote:
What I mean by "dig out" the needed "x" is:  if I could 
alias/enum/
or someother  trick be then able just to use that "x" as a 
simple static array.


You might be able to just cast the struct to a static array of 
the same size if the types are all compatible. Like a reinterpret 
cast of the raw memory kind of idea.


struct A {
int a;
int b;
}

void main() {
A a;
int[2] as_array = cast(int[2]) a;
}


That works. But idk if it will help with your alignment issue, I 
don't know much about avx at all.



Thanks again, Keep Warm in Upstate!


It has actually been kinda nice the last few days!

Winter coming soon though, sigh.


Re: Curious effect with traits, meta, and a foreach loop ... mystifies me.

2021-09-07 Thread Adam D Ruppe via Digitalmars-d-learn
On Tuesday, 7 September 2021 at 17:24:34 UTC, james.p.leblanc 
wrote:
   // this fails with: "Error: variable 'i' cannot be read at 
compile time

   //
   // foreach( i ; 0 .. 3 ){
   //ptr = u.tupleof[i].x.ptr;


tuples only exist at compile time, so you'd have to make sure the 
indexing is itself compile time. Consider that unlike an array, 
each index might give a different type, so like what would


struct A { int a; string b; }

A a;
int idx;
some_type c = a.tupleof[idx];


Which type is some_type? Is it int or string? Impossible to tell 
since it doesn't know what idx is. So idx needs to be known at 
compile time so it knows which type you get there.


The reason why it works here:

foreach( i, val ; u.tupleof ){


is because the compiler knows you're specifically looping over 
the tupleof, so it expands it and knows what i is going to be at 
compile time. If you assigned that i to an intermediate variable 
then it would break this direct knowledge and it doesn't compile 
again.



If you want to do a runtime lookup, you need to separate the two 
pieces. This pattern works:



switch(runtime_index) {
   foreach(i, val; item.tupleof)
 case i:
   // use val
}


So the switch is at runtime but the loop and cases are all known 
at compile time.


Re: Documentation generator is not working

2021-09-02 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 2 September 2021 at 17:20:55 UTC, Vinod K Chandran 
wrote:

"dmd -run test.d -D"


Anything after -run goes to your program not the compiler.

Args to the compiler must be before -run.


Re: vibe.d: is it possible to use bare HTML with the functionalty of DIET templates ?

2021-08-30 Thread Adam D Ruppe via Digitalmars-d-learn

On Tuesday, 31 August 2021 at 00:09:14 UTC, someone wrote:
Can I use vibe.d *without* DIET templates manually writing say, 
XHTML 1.1 pages, *while having D* at my disposal with the - 
prefixes I have seen so far ?


I don't know much about vibe.d (I have my own D web stuff) but 
just for fun I wanted to try passing my dom.d through the ctfe 
engine to do the embedded code thing.


50ish lines for the basic extractor i slapped together in 20 mins.

---

import arsd.dom;

string toD(string s) {
return `append(` ~ "`" ~ s ~ "`" ~ `);`;
}

template loadTemplateMixin(string doc) {
string helper() {
Document document = new Document;
document.parseSawAspCode = (string) => true;
document.parseStrict(doc);

string code;

void expand(Element element) {
if(auto asp = cast(AspCode) element) {
if(asp.source.length > 3 && asp.source[1] == 
'=')
code ~= `append(` ~ asp.source[2 .. 
$-1] ~ `);`;
else
code ~= asp.source[1 .. $-1];
} else if(auto tn = cast(TextNode) element) {
code ~= toD(tn.toString());
} else if(auto sn = cast(SpecialElement) element) {
code ~= toD(sn.toString());
} else {
code ~= toD("<" ~ element.tagName);
foreach(k, v; element.attributes) {
code ~= toD(" ");
code ~= toD(k.htmlEntitiesEncode);
code ~= toD("=\"");
code ~= toD(v.htmlEntitiesEncode);
code ~= toD("\"");
}

code ~= toD(">");
foreach(child; element.children)
expand(child);
code ~= toD("");
}
}

expand(document.root);

return code;

}
enum loadTemplateMixin = helper();
}


// USAGE HERE

// this could be in a file import("file.html") too btw
enum doc = ` foocssid="main"><%= my_string[0 .. 5] %>

foo
foo
foo
<% foreach(item; strings)
append(item);
%>
`;

void main() {

string html;

// and it can see these variables in the <% %> blocks
string my_string = "hello world";

string[] strings = ["omg", "wtf", "lol"];

void append(string s) {
html ~= s;
}

mixin(loadTemplateMixin!doc);

import std.stdio;
writeln(html);

}
---


Not exactly the fastest compile though but I could prolly 
optimize that if i spent a lil more time on it.


Now that it yields a string though you can return that to vibe 
using whatever method it uses.


Also note that you get a compile error on malformed input xhtml 
too.


Re: Object.dup?

2021-08-30 Thread Adam D Ruppe via Digitalmars-d-learn

On Monday, 30 August 2021 at 20:21:38 UTC, Per Nordlöw wrote:

Why doesn't `Object` have an auto-defined `.dup` property?


I don't think it could. What if you have a child class with a 
delegate that points to one of its own methods?


You can define one yourself of course with overrides in child 
classes to update those kinds of things. But doing it 
automatically is tricky.




Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 26 August 2021 at 19:31:54 UTC, DLearner wrote:

   if (typeof(v).stringof == "int" ) {


Tip: you can instead of string of do

if (is(typeof(v) == int))


That is operator lets you compare types directly.

(stringof is something you will almost never use as you learn 
more of the language)


Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 26 August 2021 at 18:07:48 UTC, Ali Çehreli wrote:
In some cases it's more useful to have a 'static if' inside a 
single function template instead of two separate function 
templates.


In most cases that's better. A template constraint is really a 
way to say "this template cannot accept this". When you use it to 
overload, the conditions get ugly fast since you need to make 
sure they're all mutually exclusive and the error messages get 
wrong since the compiler isn't sure if the thing can or can't be 
accepted.


What you generally want to do here is if the call - from the 
user's perspective - should work, make sure it gets through the 
constraint. Then do the details of branches inside.


Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 26 August 2021 at 17:39:16 UTC, Ali Çehreli wrote:
String mixins are appealing because they can inject code like C 
macros do. It's not trivially possible to do the same with 
template mixins.


Template mixins are great, but obviously totally inappropriate 
here. I'm just talking about using a normal function, possibly 
with an alias argument, instead of any kind of mixin.


Too often D programmers reach for fancy code generation when a 
simpler function is a better fit.


Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 26 August 2021 at 17:01:06 UTC, DLearner wrote:
The object was to take a variable, and do alternative things 
with it depending on (say) whether it was an 'int' or an 'int*'.


That's *very* easy to do with the alias. You can just check 
`typeof(v)` in there.


Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 26 August 2021 at 16:16:55 UTC, DLearner wrote:

Please confirm that mixins of format:


You really shouldn't use string mixins like this at all. If you 
want to work with a variable, pass the variable itself as an 
argument to the function and use it with regular code instead of 
passing names as strings.


void do_something(alias v)() {
   // use v like a normal variable
}

int a;
do_someting!a; // pass the variable a as an alias so you can use 
it inside




Re: alias this - am I using it wrong?

2021-08-25 Thread Adam D Ruppe via Digitalmars-d-learn

On Wednesday, 25 August 2021 at 12:11:01 UTC, Johann Lermer wrote:
I have a little problem understanding alias this. I always 
thought, that alias this only makes implicit conversions from 
the aliased object to this.


What it does is if "a SOMETHING b" doesn't compile, it instead 
tries "a.alias_this_member SOMETHING b" instead, or "a SOMETHING 
b.alias_this_member" instead if that's on the other side. The 
object with alias this must already exist though, so constructors 
are an exception (though another object's constructor can trigger 
some existing object's alias this when used as a param to that).


The "SOMETHING" there can be operators like + or = or a .member.

Only if both fail to compile do you actually get an error.


17 Test_Struct ts = ac;  // compiles


So what really happens here is the compiler sees ts = ac; fails 
to compile, so it is rewritten into "ts = ac.ts;"



18 ac = ts;  // compiles as well - why?


So ac = ts fails, meaning it rewrites into `ac.ts = ts;`


20 auto tc = new Test_Class;
21 ts = tc.ac;   // compiles


So here it is rewritten into `ts = tc.ac.ts`.

22 tc.ac = ts;   // again this compiles, but seg 
faults


And now

tc.ac.ts = ts;

is the rewrite since the plain one didn't compile, thus accessing 
the null member.




Note too that alias this can be to a function, in which case the 
rewrite will call the function.


Implicit conversion isn't really what alias this is about. It 
kinda works (though note with a alias this struct it can be 
passed by value and thus copy, again the compiler just does the 
rewrite). It is just giving transparent access to a member.


Re: std.stdio.File is throwing with the message of: "Access Violation"

2021-08-21 Thread Adam D Ruppe via Digitalmars-d-learn
On Saturday, 21 August 2021 at 23:50:08 UTC, Ruby The Roobster 
wrote:

wndclass.lpszClassName = "Test"; //May need casting...


don't cast it just use the w suffix

wndclass.lpszClassName = "Test"w;


casts are usually indicating a mistake


Re: Cannot catch exception in debug mode

2021-08-11 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 12 August 2021 at 01:53:12 UTC, frame wrote:

Is this a known DMD bug or feature?


Huh that is weird, it works correctly in gdc but i can reproduce 
in dmd and ldc. And removing the debug keyword makes it work. 
Certainly smells like a regression bug to me.


Re: -L/SUBSYSTEM:windows Error when using -m64

2021-08-10 Thread Adam D Ruppe via Digitalmars-d-learn

On Tuesday, 10 August 2021 at 18:59:33 UTC, Marcone wrote:

Using -Lgdi32.lib -Luser32.lib? Same error.


The part after that:

If you want the Windows subsystem too, use -L/subsystem:windows 
-L/entry:mainCRTStartup.



Pass BOTH -L/subsystem:windows AND -L/entry:mainCRTStartup


Re: -L/SUBSYSTEM:windows Error when using -m64

2021-08-10 Thread Adam D Ruppe via Digitalmars-d-learn

On Tuesday, 10 August 2021 at 18:45:35 UTC, Marcone wrote:
Hi, do you have some other solution without arsd.simpledisplay? 
I want only dmd feature.


Did you read the paragraph under the link? That IS a dmd switch. 
Or a ldc switch. It explains the concept which works with 
anything.


Re: -L/SUBSYSTEM:windows Error when using -m64

2021-08-10 Thread Adam D Ruppe via Digitalmars-d-learn

On Tuesday, 10 August 2021 at 18:34:03 UTC, Marcone wrote:

How can I hide console using -m64?


http://dpldocs.info/experimental-docs/arsd.simpledisplay.html#installation-instructions


Re: writef, compile-checked format, pointer

2021-08-09 Thread Adam D Ruppe via Digitalmars-d-learn

On Monday, 9 August 2021 at 22:30:43 UTC, Ali Çehreli wrote:
I don't know the reason but I suspect a bug in the compile-time 
checker that involves .init values for arguments.


The compile time checker is pretty bad tbh, it just tries to ctfe 
execute the given string and sees if it throws. That's kinda 
clever; the simplest thing that can possibly work, no doubt. But 
since ctfe cannot work with certain things - like the pointer 
casts which are done here* - this makes it a bit fragile. And the 
implementation is really really slow and pretty bloated in 
codegen too.


My personal policy is to never use it.

* The compiler probably could special-case `null` at least since 
that's consistently 0 in D.



Perhaps the un-typed 'null' literal is used somewhere.


The line is this:

const pnum = () @trusted { return cast(ulong) p; }();


Which is already blargh code, but in ctfe it is a prohibited 
operation.


Re: How suppress (Hide) prompt command console in DMC? Like -mwindows in C++?

2021-08-07 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 8 August 2021 at 00:02:18 UTC, Marcone wrote:
I create a gui program using DMC. I want to know how suppress 
(Hide) prompt command console in DMC? Like -mwindows in C++. 
Thank you.


use /subsystem:windows

a few more details here 
http://arsd-official.dpldocs.info/arsd.simpledisplay.html#installation-instructions


Re: Generating C Headers From D Code

2021-08-06 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 5 August 2021 at 16:28:35 UTC, Jack Stouffer wrote:

I know the compiler has C++ header generation


Your best bet is probably to use this and just do the slight 
modifications to turn it from C++ into C.


dmd -HC

followed by a modification script to rip some of the stuff out 
might get you somewhere.


A long time ago i made a program called dtoh that did it from the 
generated json but like i think that broke years ago, i haven't 
touched it for ages. can't find it anymore. dmd -HC surely better 
anyway.




Re: Conditional compilation: Which version identifier for release code ? version(assert) ?

2021-08-05 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 5 August 2021 at 09:18:08 UTC, wjoe wrote:
If it's to be determined whether or not the code is being 
compiled in debug or release mode, i.e. e.g. the dmd 
```-release```


You should never use the -release flag. It should be renamed to 
"-enable-security-holes" since that's what it actually does.


Instead you can disable specific things as-needed, but it is 
probably never needed. These are also never supposed to actually 
change the behavior of your program, but in reality, like I said 
they do tend to change it - by enabling security holes.


Re: __traits() to get parameter details only ? ... hasMember looks up everything within

2021-08-04 Thread Adam D Ruppe via Digitalmars-d-learn

On Wednesday, 4 August 2021 at 22:01:21 UTC, someone wrote:

No. I didn't know it ever existed :(


It is good to look at the source code implementations of some of 
those things too. Most are built out of language features and 
some of it is simpler to use directly (and some of it are pretty 
tricky that's educational to look at too).




Re: Two major problems with dub

2021-08-04 Thread Adam D Ruppe via Digitalmars-d-learn

On Wednesday, 4 August 2021 at 08:18:34 UTC, evilrat wrote:
Than again like I said it is library author mistake, if only 
JSON is ever used then it should depend on vibe-d:data 
specifically and not the whole vibe-d thing.


It is also a problem with dub though since D, the language, 
supports this just fine.


If you use dmd -i, it only includes the specific modules you 
actually used. This can even get down to fine-grained functions 
if you code carefully, like if you use my dom.d you just need 
dom.d for most things. But call Document.fromUrl and now dmd -i 
pulls in my http2.d. Or Document.parseGarbage pulls in 
characterencodings.d.


It does all that transparently based on your actual function 
calls thanks to those things being templates with local imports. 
But dub can't express that at all; it is incapable of using D's 
full modular strengths.


This could be fixed.


Re: Two major problems with dub

2021-08-01 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 1 August 2021 at 17:18:39 UTC, Alain De Vos wrote:
A simple and small wrapper around for instance the C-library 
libpq should be part of the language itself and should not pull 
in more than libpq itself.


i have one of those in two files: database.d for the interface 
and postgres.d for the impl found in here 
https://github.com/adamdruppe/arsd


are you suggesting some kind of mandatory review to be featured 
on dub? that might not be a bad idea actually.


Re: How to put an arbitrary string to clipboard in D?

2021-07-31 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 31 July 2021 at 18:30:47 UTC, tastyminerals wrote:
So I thought there may be a way in D to communicate with the 
system clipboard...


You can always call the same system functions from D that you'd 
use from C.


This is the source to my simpledisplay.d library on its clipboard 
function:


https://github.com/adamdruppe/arsd/blob/master/simpledisplay.d#L5279

The Windows side isn't too bad, the linux side a bit more 
complicated. Generally window libs offer something for this but 
idk about the one you're using.


Re: Any way to create derived classes from Exception with canned messages?

2021-07-31 Thread Adam D Ruppe via Digitalmars-d-learn
I'll add just for info sake even though you can just use normal 
gc in your own constructors as wanted, other options include:


1) making a buffer inside your new object and copying the message 
to it, then passing a slice of your buffer to the super ctor.


2) making all the messages compile-time literals so each class 
has a static message. then there's no runtime work at all. (and 
you can differentiate things based on type!)


can even be defined inline like

`throw new NotImplemented!"thing"`

and then the impl is like

class NotImplementedBase : Exception {
  mixin ExceptionCtors;
}

class NotImplemented(string msg) : NotImplmentedntedBase {
   this() { super ("Not implemented" ~ msg); }
}


that kind of thing just plus the file line info etc too arguments.

3) ignore hte msg member and just override toString



But again the base ctor limitations do NOT apply to derived 
classes so it is all moot.



btw for virtual interfaces base things do apply but you're 
allowed to tighten if you want. so like base toString is NOT 
@nogc but if you wanted derived @nogc that's perfectly allowed. 
virtual methods must be as strict or stricter since they're 
inherited. just again ctors are not inherited so there's no 
connection at all.


Re: Any way to create derived classes from Exception with canned messages?

2021-07-31 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 31 July 2021 at 08:25:56 UTC, Jeremy T. Gibson wrote:
Now, https://github.com/dlang/druntime/blob/master/src/object.d 
clearly expresses that the constructors of Exception are @nogc.


That doesn't mean your constructors have to be! You can do 
whatever you want. Constructors aren't an inherited interface.


Re: Passing delegate indirectly to createLowLevelThread doesn't work

2021-07-26 Thread Adam D Ruppe via Digitalmars-d-learn

On Monday, 26 July 2021 at 17:14:45 UTC, Tejas wrote:
Yeah after reading the error diagnostics carefully I realized 
that the compiler is inferring many attributes when passing 
```func``` directly but not when passing via delegate


Well, technically, it is inferred there too, but since you 
specified `void delegate()` it implicitly casts to that and drops 
the detail.


Similar to the difference between like

Object o = new MyClass;
// now o is still Object since the class implicitly casted and 
thus drops the extensions in the child class


and

auto c = new MyClass;
// no conversion requested there, c has the full type of MyClass


Re: __FILE__

2021-07-26 Thread Adam D Ruppe via Digitalmars-d-learn

On Monday, 26 July 2021 at 11:43:56 UTC, workman wrote:

__FILE__[0..$]


Why do you have that [0..$] there? It is probably breaking the 
__FILE__ magic.


Re: byKeyValue is not available at compilation-time right ?

2021-07-25 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 25 July 2021 at 18:03:05 UTC, someone wrote:

pstrExchangeID = structureExchanges[r"NYSE"d].ID;


well that's a compile time constant


structureExchanges[lstrExchangeID].location;


And that's a compile time constant.

So you could just pass the data directly as a variable. So 
instead of looking up the exchanges[id], just pass the exchange.


AA's are good for runtime but if it is all compiled in you can 
just do static data.


i'll be back later gotta run


Re: byKeyValue is not available at compilation-time right ?

2021-07-25 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 25 July 2021 at 17:29:46 UTC, someone wrote:
What is the proper syntax to use manifest-constants with 
associative arrays then ? The one you showed me ?


You have the right syntax for that. What I'm saying is you might 
not need the associative array at all. Why do you want to use 
that specifically?


You say a normal foreach ... to be used at compilation-time ... 
huh ?


Normal foreach is evaluated at compile time if it is looping over 
an aliasseq tuple or if it is run in a compile time context.


Part of the magic of D is ordinary code might be compile time or 
run time depending on how you use it.


But even static foreach I think can do the

static foreach(k, v; your_assoc_array) {}

in some cases.



Re: How to check if variable of some type can be of null value?

2021-07-25 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 24 July 2021 at 18:10:07 UTC, Alexey wrote:
The goal I with to achieve by this check - is to use template 
and to assign value to variable basing on it's ability to 
accept null as a value.


The most direct representation of that is __traits(compiles, (T 
t) { t = null; });



Another cool trick to consider is:

is(typeof(null) : Whatever)


What that means is if the null literal will implicitly convert to 
Whatever type. This means you can pass `null` as an argument to a 
function accepting Whatever. Thus it includes pointers, classes, 
interfaces, arrays. But does NOT include structs, even if they 
have a null accepting constructor / opAssign since you still must 
explicitly construct them.


struct S {
   void opAssign(typeof(null) n) {}
}

void main() {
  S s;
  s = null; // allowed due to opAssign
}

pragma(msg, is(typeof(null) : S)); // FALSE because this check 
only looks for implicit conversion, not user-defined assign 
overloads or constructors.


The traits compiles check will allow this, since it is looking at 
assign... but the traits compiles will say false if it gets a 
`const` type since obviously then assign is not allowed, even if 
implicit conversion would be.


Depending on your needs you might use one of these, or perhaps 
both.


Re: byKeyValue is not available at compilation-time right ?

2021-07-25 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 25 July 2021 at 05:10:32 UTC, someone wrote:
As you can see in the following code I cannot avoid to type the 
public immutable enum structureLocations = [

   r"BUE"d : typeLocation(r"arg"d, r"Buenos Aires"d, r"ART"d),
   r"GRU"d : typeLocation(r"bra"d, r"São Paulo"d, r"BRT"d),


Coudln't you instead just do like

enum structureLoctations = {
   BUE = typeLocation..,
   GRU = typeLiocation
}


?


Then you can build a runtime hash map if you need it in a static 
constructor or use a binary search switch to convert from strings 
and look up the id from reflection. It depends on the usage.




static foreach(
   structureExchange sudtExchange;
   structureExchanges.byKeyValue
   ) {


You can also try a normal foreach


foreach(k, v; structureExchanges) {
// use k and v
}


static foreach might work too but assocative arrays are weird 
beasts that needs to exist all at run time or all at compile 
time; they cannot cross the barrier and use one item for both.


But the built-in k,v instead of byKeyValue might help anyway.


Re: Traceinfo gone

2021-07-25 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 24 July 2021 at 08:41:20 UTC, frame wrote:
I recently discovered that my exceptions do not show a trace 
anymore.

How can this happen?


It needs to load the .pdb file at runtime, so make sure it stays 
next to your exe.




Re: Build time

2021-07-24 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 24 July 2021 at 09:12:15 UTC, JG wrote:

the slowest parts are in library code


This is really common. A lot of libraries do really weird 
things... if you want to keep quick builds it is best to use the 
language directly.


Re: how to test (at compilation-time) for existence of an already-declared object ?

2021-07-23 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 24 July 2021 at 01:29:26 UTC, someone wrote:

Suppose I have a module with the following:



static if(is(classTickerCustomNYSE))
   // it was a valid type
else
  // it was not a valid type


can use that to test. you can also mix in the name


static if(is(mixin("class name here"))) {}



There's similar tricks for testing for imports and such too but i 
recommend here you import the module first then can test it.


Re: Build time

2021-07-23 Thread Adam D Ruppe via Digitalmars-d-learn

On Friday, 23 July 2021 at 19:09:02 UTC, JG wrote:
I am not sure how relevant it is but it is a compiler that I 
have been writing, not something serious (yet - if ever).


Compile time optimizations are a bit weird compared to runtime 
ones - things that would fast at run time may actually be very 
slow at compile time, so eyeballing the code might help me point 
out something to consider.


Re: Build time

2021-07-23 Thread Adam D Ruppe via Digitalmars-d-learn

On Friday, 23 July 2021 at 19:32:08 UTC, H. S. Teoh wrote:
And avoid doing too much work in CTFE, which is known to be 
slow.


Well it very much depends HOW you do it. Like the ~= operation in 
ctfe is awfully slow and wastes a lot of memory depending on the 
size of the string, but if you preallocate and copy memory in 
chunks it isn't too bad at all.


And if you use format!"str"() in ctfe that alone can be a real 
speed killer.


That's why I wanna see the code, it is possible there's a fairly 
simple bottleneck to look at.


Re: Build time

2021-07-23 Thread Adam D Ruppe via Digitalmars-d-learn

On Friday, 23 July 2021 at 18:53:06 UTC, JG wrote:

The program I writing is around 3000 loc


what's the code?


Re: Generate docs for generated code?

2021-07-23 Thread Adam D Ruppe via Digitalmars-d-learn

On Friday, 23 July 2021 at 10:04:55 UTC, wjoe wrote:
Is there a way for the compiler to consider doc comments in 
auto generated, mixed in code?


If you use my adrdox generator (which runs on the dpldocs.info 
website), it handles mixin templates. See


http://dpldocs.info/experimental-docs/std.net.curl.HTTP.html#mixed-in-members

for example.

Mine also actually handles

mixin(q{
/// code
void foo() {}
});

as if it was direct.


But my generator only handles mixin templates and mixin string 
literals, not actually generated code returned from a function.


Re: Do static variables in class consume memory of instances?

2021-07-22 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 22 July 2021 at 15:50:59 UTC, Mark Lagodych wrote:
Do static variables consume *any* memory in instances, perhaps 
just for pointers to the variables?


nope

Or does compiler automatically convert 
`someObject.someStaticMember` to `SomeClass.someStaticMember`?


right


Re: catching segfault using try_ catch

2021-07-22 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 22 July 2021 at 14:19:37 UTC, seany wrote:

how can I specify this with dub ?


Probably easiest to just copy the memoryerror.d file from 
druntime into your dub source dir.


Re: Method definition

2021-07-21 Thread Adam D Ruppe via Digitalmars-d-learn

On Wednesday, 21 July 2021 at 13:56:11 UTC, Tim Gunesh wrote:
This allows you to quickly understand the content of the class, 
especially in large undocumented projects.


The D compiler can auto-generate listings like that. dmd -H makes 
a .di file with the bodies stripped out you can pursue, or a 
documentation generator like my adrdox can make HTML files with 
the method prototypes, even if there are no other docs (use 
--document-undocumented arg to adrdox for that).


So you can accomplish these goals through other means.


Re: Including a file

2021-07-18 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 18 July 2021 at 17:28:07 UTC, Vindex wrote:
Error: file "thing.json" cannot be found or not in a path 
specified with -J


You need to specify the path where it is found with the -J switch 
to the compiler. Like `ldc2 -J. yourfile.d


Re: interface function member declarations needing parameter attributes ?

2021-07-18 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 18 July 2021 at 03:27:04 UTC, someone wrote:
I have an interface with function members *not* being declared 
@safe (at first glance it seemed irrelevant to me just to mark 
@safe a declaration and not an actual implementation).


Yeah, that'd be promising all child implementations are @safe.

Fun fact there: any child implementations do NOT need to specify 
the attribute there; the compiler will copy it from the interface 
for you.


interface I {
   @safe void foo();
}

class C : I {
   void foo(); // automatically copies @safe from interface
}


If you leave it off, you are not promising safe, but children are 
still allowed to use it anyway. The general rule is child classes 
can be stricter than the parent if they want to be, but they 
don't have to be.



interface I {
   void foo(); // not safe
}

class C : I {
void foo(); // OK, not safe, interface didn't force it
}

class C2 : I {
@safe void foo(); // OK, class can be stricter than parent. 
now safe if used through C2, but the interface can't promise it 
is C2 instead of C, so it still follows @system rules.

}

class C3 : C2 {
override void foo(); // automatically @safe since it picks it 
up from parent C2

}


Re: function parameters: is it possible to pass byref ... while being optional at the same time ?

2021-07-18 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 18 July 2021 at 03:52:30 UTC, someone wrote:
That being said, one of the things that I felt in love with 
when I was first exposed to was OOP; with all its pros and cons.


Yeah I like a lot about OOP and D's implementation is one of the 
nicest out there. Very competent implementation with a lot of 
nice bits. Totally underrated by most the D luminaries imo.


Re: interface function member declarations needing parameter attributes ?

2021-07-17 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 17 July 2021 at 22:43:15 UTC, someone wrote:
So the lesson learned is that interfaces can also mandate 
member function's parameter attributes then ... right ?


A subclass must accept anything the parent class can, but it can 
also make it stricter if you want.


class Base {
void foo(Object o) {}
}

class Derived : Base {
override void foo(const Object o) {}
}


That's legal because const also accepts mutable. Derived is 
stricter than Base which is permitted. But the other way around:


class Base {
void foo(const Object o) {}
}

class Derived : Base {
override void foo(Object o) {}
}

is NOT allowed because the mutable thing in derived cannot be 
passed back to the base interface implicitly.




Re: How to create friends of a class at compile time?

2021-07-15 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 15 July 2021 at 17:21:45 UTC, Tejas wrote:

I can do it like this in C++:


You don't just put class def and class abc in the same module and 
you get the same effect though.


Re: catching segfault using try_ catch

2021-07-13 Thread Adam D Ruppe via Digitalmars-d-learn

On Tuesday, 13 July 2021 at 16:52:43 UTC, seany wrote:

What will it return to me?


true if it succeeded.

I want to catch the segfault and a segfault has occured, I want 
run a different code at that point.


You mean transparently rerun some code? That's better done with 
the lowlevel sigaction handler.


But if you just want to standard try/catch then do something in 
the catch block, this is fine.



import etc.linux.memoryerror;

void main() {
registerMemoryErrorHandler();
int* a;
try {
*a = 4;
} catch(Throwable e) {
import std.stdio;
writeln("Caught");
}
}


It can be used on ldc2 too but it isn't as reliable since ldc 
considers null access to be undefined behavior anyway and thus 
may optimize out your catch


You also need to compile in the module with ldc so build it like

$ ldc2 seg.d ~/d/ldc/import/etc/linux/memoryerror.d

just including the memoryerror file o the command line lets it 
link.


Re: catching segfault using try_ catch

2021-07-11 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 11 July 2021 at 23:34:38 UTC, seany wrote:

Is there an example i can use Thank you.


You just call the registerMemoryHandler() function from that 
module at some point in your main function before doing other 
work.


Re: catching segfault using try_ catch

2021-07-11 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 11 July 2021 at 21:15:30 UTC, Paul Backus wrote:
I know it's possible on Linux using the `etc.linux.memoryerror` 
module [1]. Not sure about Windows.


With -m32, it just works. With -m32mscoff I'm not sure. With 
-m64, as far as I know, there is no way.


Re: mixin template's alias parameter ... ignored ?

2021-07-11 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 11 July 2021 at 13:30:27 UTC, zjh wrote:

Could you explain more detail?


It is just normal code with a normal name. The fact there's 
another variable with the same name doesn't change anything.


Re: Scope of enum

2021-07-11 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 11 July 2021 at 13:21:35 UTC, DLearner wrote:

Is there a 'D' way of avoiding the issue?


Pass the size as a parameter to the thing instead of trying to 
combine things. like



mixin template Thing(size_t size) {
   ubyte[size] pool;
}


and where you want it like


mixin Thing!(100);


and somewher eelse

mixin Thing!(500);


and they're separate variables with different sizes that you can 
use.



idk what your thing needs in context though


Re: mixin template's alias parameter ... ignored ?

2021-07-11 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 11 July 2021 at 05:20:49 UTC, someone wrote:

```d
mixin template templateUGC (
   typeStringUTF,
   alias lstrStructureID
   ) {

   public struct lstrStructureID {

  typeStringUTF whatever;

   }



This creates a struct with teh literal name `lstrStructureID`. 
Just like any other name. So it is NOT the value of the variable.



```d
   public struct mixin(lstrStructureID) { ... }
```

because the argument seems to require a complete statement.


Indeed, you'd have to mixin the whole thing like

mixin("public struct " ~ lstrStructureId ~ " { ... } ");


Re: Scope of enum

2021-07-11 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 11 July 2021 at 12:37:20 UTC, DLearner wrote:

C:\Users\SoftDev\Documents\BDM\D\Examples\CTFE\T2>type k_mod.d
// k_mod.d


ubyte[MemSiz]  MemPool;


You didn't import the other module here.


D's imports aren't like C's includes. Each module is independent 
and can only see what it itself imports.


A module has no idea who is importing it. It only knows what it 
imports inside itself.


Re: Error: Outside Unicode code space

2021-07-08 Thread Adam D Ruppe via Digitalmars-d-learn

On Friday, 9 July 2021 at 03:09:52 UTC, Tony wrote:
The editor I am using (Code::Blocks) displays the characters 
just fine. So it seems that the error message should be "Error: 
Outside the ASCII code space".


D supports stuff outside the ASCII code space just fine.

Are you sure the file is saved as utf 8? if it is something like 
Windows 1252 it can still often be displayed but dmd won't know 
what to make of it.


Re: Trivial simple OpenGl working example

2021-07-08 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 8 July 2021 at 13:51:51 UTC, Виталий Фадеев wrote:

It may be based on any library: SDL, GLFW, Derelict, etc.


my library

http://arsd-official.dpldocs.info/arsd.simpledisplay.html#topic-modern-opengl

arsd-official:simpledisplay dependency on dub, or just download 
color.d and simpledisplay.d from 
https://github.com/adamdruppe/arsd and compile them with your 
sample program (dmd yourprog.d color.d simpledisplay.d) and you 
should be able to run with it.


but my bindings aren't as complete as the others suggested here.


Re: How to disable assigning a value to a property?

2021-07-06 Thread Adam D Ruppe via Digitalmars-d-learn

On Tuesday, 6 July 2021 at 10:06:11 UTC, Jack Applegame wrote:

How to disable `register.clock = 10;`


You don't. The language always allows `a = b;` to be rewritten as 
`a(b);`.


Best you can do is use different types for the two arguments. 
Maybe clock could take a struct Clock { int x; } or something 
instead.


Re: Are D classes proper reference types?

2021-06-24 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 24 June 2021 at 07:28:56 UTC, kinke wrote:
*scope* classes are deprecated (I don't think I've ever seen 
one);


I used it for my database thing where it is supposed to be 
destroyed reliably but also uses runtime polymorphism.


I now suggest people just stick `scope(exit) .destroy(obj);` any 
time you use it.


Re: List of Dynamic Arrays

2021-06-17 Thread Adam D Ruppe via Digitalmars-d-learn

On Thursday, 17 June 2021 at 15:57:46 UTC, Justin Choi wrote:

I want to write something like `DList!int[]()`


DList!(int[])() ?


Re: Class member initialization with new points to a single instance?

2021-06-09 Thread Adam D Ruppe via Digitalmars-d-learn

On Wednesday, 9 June 2021 at 17:56:24 UTC, Gregor Mückl wrote:

class Bar { Foo foo = new Foo(); }


This is a static initialization

The assert fails. This is completely surprising to me. Is this 
actually expected?


Yes, it is expected if you are familiar with the spec.

All member = x things inside a class or struct declaration are 
static initializers. These are CTFE'd into the same form as a 
literal. So there's one instance of Foo there which is assigned 
to the object member before the constructor runs.


For a runtime initialization, you use a constructor.


Re: regarding what seems (to me) unnecessary casts on integer expressions

2021-06-04 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 5 June 2021 at 02:38:50 UTC, someone wrote:
Furthermore, if, **at least**, there was a way to clearly state 
what is short (eg 1S a la 1L) things will improve a lot


That actually doesn't matter. The compiler actually will 
automatically type it to the most narrow thing it fits. The 
implementation there is actually quite nice - inside a single 
expression, the compiler tracks the possible range.


Try:

ubyte a = 5 + 3; // just works, it knows 5+3 = 8 which can be byte

ubyte b = a & 0xff; // also works, it knows the mask limits the 
range



And various combinations like that. It works as long as it is in 
one statement.


But if you cross them:

ubyte c = a + 1;

Now it assumes a might be the maximum of 255... and 255 + 1 
doesn't fit in the ubyte, thus it will not allow the assignment 
back.



Masking operations are a possible alternative to casting btw... 
but still a hassle.



ushort a; a = cast(ushort) a - cast(ushort) 1;


This doesn't do anything since the promotion is done  anyway. The 
compiler already knows both are ushort there, it is the 
arithmetic that assumes worst case scenario.




ushort a; a = cast(ushort)(a - 1);


this does work though.

a -= 1; // also works

At first sight I got the impression that any type could be 
unambiguously stated in D. Seems I was wrong.


Again, it is not the type of the literal that is the problem.

D follows the C rule that all arithmetic promotes. (Seriously, 
try it in C, it also converts to int first before doing an add or 
subtract.)


But C allows it to convert back without hassle, so it is easy to 
ignore this annoying promotion thing. D only allows the 
conversion back if the compiler can prove it fits.



for a string "" should not the same as null


its not. They're very similar but it is NOT the same. Check the 
.ptr property.



and 0 for a integer should not be the same as null


Its not. The type system won't allow those to cross.

and this is obviously **really useful** for database apps and a 
lot of other things.


I've done tons of database apps in D, I've been doing web stuff 
with D since 2008. There's various ways to do nullable sql, it 
isn't a big problem.


Re: regarding what seems (to me) unnecessary casts on integer expressions

2021-06-04 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 5 June 2021 at 01:46:45 UTC, someone wrote:
What's the point of declaring, for instance ushort's if then 
nothing will treat them as ushort's and I have to manually 
cast() everything to ushort() all the time ?


Yeah, it totally sucks.

D inherited a silly rule from C - the promote rules actually come 
from there - but then added a well-intentioned but pretty 
annoying in practice rule that discarding bits from arithmetic 
require an implicit cast. (Unless it is the bits over 32... then 
who cares. lol)


The += operator is exempt from it so use it where you can. But 
otherwise your only real hope is to do a user defined type with 
op overloads and what an enormous hassle.


This design was a mistake.


Re: Is it possible to set function attributes conditionally?

2021-06-04 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 4 June 2021 at 11:33:32 UTC, wjoe wrote:
This is a contrived example. In reality I would use this with 
custom array, hash map and other container implementations so I 
could use them in @nogc territory by just switching out the 
allocator.


If they are templates, just don't specify attributes and the 
compiler will infer them for you.


If they are not templates, you have to do a separate copy under 
version or static if.


Re: Question about initialization

2021-06-04 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 4 June 2021 at 11:27:05 UTC, seany wrote:
In my untrained eye, this seems haphazard. One requires the use 
of the new keyword


It doesn't.

T[] a;

just works. it is an array of length 0.


Re: wanting to try a GUI toolkit: needing some advice on which one to choose

2021-06-01 Thread Adam D. Ruppe via Digitalmars-d-learn
On Tuesday, 1 June 2021 at 22:39:08 UTC, Ola Fosheim Grøstad 
wrote:
My understanding is that dropping OS icons onto the web view is 
problematic


You have to subscribe to the particular content type so it 
doesn't always work but it is totally doable.


You can play with it using simpledisplay.d's drag and drop 
support.


but maybe they have overcome this because of Chrome OS. No, I 
don't use it, except for file uploads.


Web drag and drop actually dates back to IE 5. It has been 
cleaned up a little since then and now prefers mime types to the 
type enum it used to do (though both still tend to work) but it 
isn't actually all that different.


Yes, it does. You get lots of graphic context switches and poor 
render-caching performance.


no not really.

you'd have to be really incompetent to do it this wrong.


Re: wanting to try a GUI toolkit: needing some advice on which one to choose

2021-06-01 Thread Adam D. Ruppe via Digitalmars-d-learn
On Tuesday, 1 June 2021 at 21:47:38 UTC, Ola Fosheim Grøstad 
wrote:
Right now, drag-and-drop is not as easily supported in browser 
UIs though. That is an argument for using native UI.


eh web drag and drop isn't half bad at all. Have you ever used it?

Note: Many simple GUI toolkits are horribly inefficient as they 
let each object render themselves.


That doesn't make any sense.


Re: wanting to try a GUI toolkit: needing some advice on which one to choose

2021-06-01 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 1 June 2021 at 03:36:00 UTC, someone wrote:

On Sunday, 30 May 2021 at 07:03:38 UTC, Chris Piker wrote:

Of the 107 forks of dlangui last seen on github ...


I can't believe it. What a waste of time/resources. It is like 
if I forked MATE, changed the title, made 10/20/or-so changes 
here and there and then dropped out of sight. Pointless.


Have you ever used github before?

That's how it works. You "fork" something, do a small change, 
then open a pull request back to the original. Then your "fork" 
gets abandoned until next time you want to do a PR.


This is perfectly normal on that site. Number of forks is 
actually correlated with number of *contributors*, not 
abandonment.


Re: wanting to try a GUI toolkit: needing some advice on which one to choose

2021-06-01 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 1 June 2021 at 05:26:47 UTC, Mike Parker wrote:
But have you actually investigated it? It's being actively 
maintained.


https://github.com/d-widget-toolkit/dwt


Yeah, DWT is solidly OK. I'd pick it over gtkd if you wanted to 
target Windows since it doesn't use gtk there.


Re: How to compile Phobos with other D code to create a shared library?

2021-05-31 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 31 May 2021 at 21:46:09 UTC, data pulverizer wrote:
Something interesting is using arrays. I can see that if you 
instantiate an array within the D function using `new`, for 
instance


Passing one of those to a C function is iffy anyway because the C 
function can hide it from the garbage collector.


What happens here is Runtime.terminate does a final GC sweep to 
gracefully clean up as much as it can before it close... and it 
thinks that array is unused, so it frees it.


GC.addRoot(array) can tell it not to free normally... but i'm not 
sure if runtime.terminate cares about that since terminate makes 
it think the whole thing is going down anyway.



auto result = cast(double*)malloc(double.sizeof * n);
```

you're left with the issue of not being able to free the memory 
since you've terminated the runtime.


You can free still, malloc and free are not controlled by the D 
runtime.


This is sometimes a better idea anyway when passing it to C 
function. I don't know how Julia does it though.


So I guess you have to appropriately pair up `initialize` and 
`terminate` as appropriate to kind of *startup* and and 
*shutdown* the connection with druntime with your allocations 
if they stay within D?


But yeah the best way to do it is to initialize once on library 
load then terminate once when the whole thing is finished.


Typically C plugin systems have some hook for this you can use. 
Like some onload function they define you can use or something.


Re: Naming issue importing different function overloads

2021-05-30 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 30 May 2021 at 18:42:34 UTC, data pulverizer wrote:

I wonder if it is a purposeful design


It is by design: https://dlang.org/articles/hijack.html

Basically the idea behind it is to make sure that a change in a 
lib you import doesn't change your existing code without you 
realizing it.


You can merge them at the import site by doing

import funs;

doubme myfun(...) {}

alias myfun = funs.myfun; // this line merges them


You can also use a selective import to merge them:


import funs : myfun;
double myfun(...) {}


now both myfuns work.


this works because the `import x : y;` is actually shorthand for 
a local alias.


Re: How long does the context of a delegate exist?

2021-05-30 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 30 May 2021 at 09:39:28 UTC, cc wrote:
Is there any way to enforce at compile time that we're not 
accidentally allocating when creating a delegate, other than 
being carefully aware of what variables are referenced inside 
the body?


Use `function` instead of `delegate`. Then it doesn't have a 
context pointer at all.


Re: wanting to try a GUI toolkit: needing some advice on which one to choose

2021-05-29 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 29 May 2021 at 10:51:47 UTC, btiffin wrote:
Will politely disagree about 1 or 2 can't do by themselves...  
Yeah, yeah they can.


Well, I've actually done it.

My minigui has its quirks I'm slowly working through, but it 
clearly isn't impossible.


Re: How long does the context of a delegate exist?

2021-05-27 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 27 May 2021 at 20:44:21 UTC, frame wrote:

Did you mean to add the delegate as GC root or the data?


The delegate.ptr property.



Re: How long does the context of a delegate exist?

2021-05-27 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 27 May 2021 at 12:59:02 UTC, frame wrote:

But what about the data used in the context of the delegate?


If the delegate is created by the GC and stored it will still be 
managed by the GC, along with its captured vars.


As long as the GC can see the delegate in your example you should 
be OK. But if it is held on to by a C or OS lib, the GC might not 
see it and you'd have to addRoot or something to ensure it stays 
in.


Re: wanting to try a GUI toolkit: needing some advice on which one to choose

2021-05-26 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 27 May 2021 at 01:17:44 UTC, someone wrote:

- like a simple classical UI: favored over any modern one:


My minigui is a thing of beauty. Behold:

http://arsdnet.net/minigui-linux.png
http://arsdnet.net/minigui-sprite.png

its docs:
http://dpldocs.info/experimental-docs/arsd.minigui.html

It is quick to open and quick to compile (the whole thing is 
contained in just three source file that build from scratch in 
about a quarter second).


Pity is sucks. More below.

- using the GTK toolkit within D requires the GTKD binding 
library.


Well, you don't strictly have to use gtkd, you can always just 
extern(C) define the stuff yourself (or only use them from gtkd's 
generated files) and call them. But if you do use gtk, I'd 
suggest just sticking to the gtkd wrapper.


But I hate gtk so I made my own thing from scratch on nothing but 
X which might amuse you. (I loathe that useless wayland trash and 
will never support it. X is so much better.)


However my text edit widget sucks. It barely works. Really slow 
and buggy. I've been slowly trying to find the time to rewrite it 
but wow the text edit is harder than literally everything else 
combined and since I have so many other things to do it still 
sucks. I have been doing a bit of a 2.0 rewrite lately (recent 
blogs: 
http://dpldocs.info/this-week-in-d/Blog.Posted_2021_05_03.html 
and 
http://dpldocs.info/this-week-in-d/Blog.Posted_2021_05_17.html 
describe it, though the doc link from before has info too)


So I probably wouldn't seriously recommend my thing right now on 
Linux. (On Windows, it is fine - it just uses the OS-provided 
control. But you don't care about Windows so that's irrelevant... 
unless you wanna wine.)



Ironically the biggest thing I changed in the recent 2.0 work is 
a style delegate that makes it possible to do some kind of css 
thing. But since I also think css is bloated I'm just putting 
hooks in the core so it is an optional feature, just like the xml 
and script stuff. Turns out that was easier than fixing text 
bugs. And besides people love their dark modes lol.



Anyway if you can stand gtk, gtkd really isn't bad and its text 
widget works. Just don't use the file dialog lololol.


Re: How does inheritance and vtables work wrt. C++ and interop with D? Fns w/ Multiple-inheritance args impossible to bind to?

2021-05-25 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 25 May 2021 at 17:52:14 UTC, Gavin Ray wrote:

  void takesADerived(Derived derived);

  extern class Derived : Base1, Base2


Like I said in chat, these are NOT the same thing. The C++ 
Derived is a *sibling* class, not a parent, child, nor binding to 
the D Derived.


All your attempts to pass one off as the other is problematic. If 
you want to bind to the C++ derived, don't provide any bodies for 
the functions - just bind. If you want to inherit from it, you 
must first declare the binding in D, then inherit.




Re: Formatted output not on screen but in a string

2021-05-21 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 21 May 2021 at 22:37:34 UTC, Alain De Vos wrote:

Next code format a string and prints it.
But I want the formatted string stored in a string
```
//Decimal place separator %,
writefln!"%,s"(123456789);
//123,456,789
```


std.format.format


Re: State of D for webassembly

2021-05-17 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 17 May 2021 at 20:38:12 UTC, Chris Piker wrote:
What is the general state of support for GC dependent D-code 
running as webassembly?  If the runtime is not ready that's 
okay, just wanted to inquire about the state of things.


There is a partial port of full runtime but it is still alpha 
level. A couple people have tried it and there's various bugs 
they worked through but some remained.


I wouldn't recommend using it at this time unless you're prepared 
to do some specific webassembly hacks and maybe dive into the 
druntime a bit yourself.



on the other hand if you are willing to dive in yourself a bit, 
you totally can make it work.


I have a little website where I compiled some of my games to 
webasm:


http://webassembly.arsdnet.net/

http://webassembly.arsdnet.net/tetris


I did it with a custom runtime and a specialized port of my 
relevant D libraries.


Re: Encryption

2021-05-17 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 17 May 2021 at 16:54:18 UTC, noid wrote:
Hi! I am pretty new on Dlang and I wanted to make a small 
password manager that used some sort of encryption on a file 
(for example AES256) and save a password to decrypt it later, 
so you can copy the password.


I haven't done this specifically myself, but if I did, I'd 
probably use bindings to the C libsodium and let it do most the 
encryption work.


this looks reasonable but again I haven't used myself.

https://code.dlang.org/packages/libsodiumd


Since it is a simple binding to the C library, you'd use it the 
same way you would from C itself and can look up tutorials for 
that and easily translate to D.


I also don't know how I could store the password (for example 
in a SHA256 hash) for decrypting the files.


Now this I have done myself, and again, I delegated the real work 
to a C library called argon2.


My library is 
https://code.dlang.org/packages/arsd-official%3Aargon2


And my docs:
http://arsd-official.dpldocs.info/arsd.argon2.html


If you get the C argon2 library installed, then you can use that 
D module easily with the `encode` and `verify` functions.


Re: property functions

2021-05-17 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 17 May 2021 at 14:56:21 UTC, Steven Schveighoffer 
wrote:
It used to be required, but we removed that requirement a long 
time ago.


yeah i remember ElementType required it last time i checked but 
that was a while ago


indeed it is all fixed now


  1   2   3   4   5   6   7   8   9   10   >