Re: How often I should be using const? Is it useless/overrated?

2022-11-22 Thread Kagamin via Digitalmars-d-learn

On Friday, 18 November 2022 at 17:57:25 UTC, H. S. Teoh wrote:
You're looking at it the wrong way.  The kind of issues having 
const
would solve is like when your function takes parameters x, y, 
z, and
somewhere deep in the function you see the expression `x + 
y*z`. If x,
y, and z are const, then you immediately know what the value of 
this
expression is.  However, if they were not, then you'd have to 
trace
through all of the preceding code to figure out whether their 
values
have changed, and how they have changed.  The former makes the 
code
easier to understand, the latter adds complexity to 
understanding the

code.


AFAIK Rust allows shadowing (intentionally) to solve usability 
problems with immutable variables, so when deep in the function 
you see `x+y*z`, you can't immediately tell its value, because 
the variables could be previously shadowed and you have to trace 
through all of the preceding code to figure it out :)


Re: aa.keys, synchronized and shared

2022-11-14 Thread Kagamin via Digitalmars-d-learn

This works for me:
```
synchronized final class SyncAA(K, V)
{
this(K key, V val) { sharedTable[key]=val; }
V opIndex(K key) { return sharedTable[key]; }
	V opIndexAssign(V value, K key) { return sharedTable[key]=value; 
}

const(K[]) keys() const { return unsharedTable.keys; }
void remove(K key) { sharedTable.remove(key); }
V get(K key, lazy V defaultValue=V.init)
{
auto p = key in sharedTable;
return p ? *p : defaultValue;
}
private:
V[K] sharedTable;
inout(V[K]) unsharedTable() inout
{
return cast(inout(V[K]))sharedTable;
}
}
shared SyncAA!(string,string) saa;
void f()
{
saa=new shared SyncAA!(string,string)("1","2");
saa.keys();
saa["12"]="34";
saa.remove("12");
}
```


Re: aa.keys, synchronized and shared

2022-11-11 Thread Kagamin via Digitalmars-d-learn

With allocation:
```
synchronized final class SyncAA(K, V)
{
V opIndex(K key) { return sharedTable[key]; }
	V opIndexAssign(V value, K key) { return sharedTable[key]=value; 
}

const(K[]) keys() const { return unsharedTable.keys; }
void remove(K key) { sharedTable.remove(key); }
V get(K key, lazy V defaultValue=V.init)
{
auto p = key in sharedTable;
return p ? *p : defaultValue;
}
private:
V[K] sharedTable;
ref inout(V[K]) unsharedTable() inout
{
return *cast(inout(V[K])*)
}
}
shared SyncAA!(string,string) saa;
void f()
{
saa=new shared SyncAA!(string,string);
saa.keys();
saa["12"]="34";
saa.remove("12");
}
```


Re: aa.keys, synchronized and shared

2022-11-11 Thread Kagamin via Digitalmars-d-learn

Try this:
```
synchronized final class SyncAA(K, V)
{
V opIndex(K key) { return sharedTable[key]; }
	V opIndexAssign(V value, K key) { return sharedTable[key]=value; 
}

const(K[]) keys() const { return unsharedTable.keys; }
void remove(K key) { sharedTable.remove(key); }
V get(K key, lazy V defaultValue=V.init)
{
auto p = key in sharedTable;
return p ? *p : defaultValue;
}
private:
V[K] sharedTable;
ref inout(V[K]) unsharedTable() inout
{
return *cast(inout(V[K])*)
}
}
void f(shared SyncAA!(string,string) a)
{
a.keys();
a["12"]="34";
a.remove("12");
}
```


Re: Make IN Dlang

2022-11-02 Thread Kagamin via Digitalmars-d-learn
Another idea is to separate the script and interpreter then 
compile them together.

```
--- interp.d ---
import script;
import ...more stuff
...boilerplate code
int main()
{
  interpret(script.All);
  return 0;
}

--- script.d ---
#! ?
module script;
import mind;

auto All=Task(...);
...more declarative tasks

--- run ---
dmd /usr/local/interp.d /path/to/script.d
```


Re: Make IN Dlang

2022-11-02 Thread Kagamin via Digitalmars-d-learn
But embedded sdl is likely to be dwarfed by the actual code 
anyway.


Re: Make IN Dlang

2022-11-02 Thread Kagamin via Digitalmars-d-learn
On Tuesday, 1 November 2022 at 23:40:22 UTC, Christian Köstlin 
wrote:

I am still trying to find answers to the following questions:
1. Is it somehow possible to get rid of the dub single file 
scheme, and

   e.g. interpret a full dlang script at runtime?


If there was an interpreter like
```
#!/bin/mind
...code
```
maybe it could run dub with right options and thus won't need a 
build script.


Re: dub ldc2 static linking

2022-10-28 Thread Kagamin via Digitalmars-d-learn

On Friday, 28 October 2022 at 02:46:42 UTC, ryuukk_ wrote:
I'm just right now having an issue with glibc version mismatch 
for my server


Just compile with an old enough glibc, 2.14 works for me.


Re: Is "auto t=T();" not the same as "T t;"?

2022-10-26 Thread Kagamin via Digitalmars-d-learn
Looks like explicitly initialized variable in this case allocates 
array literal. Uninitialized variable is initialized with init 
pattern. This may be correct as uninitialized variable isn't 
guaranteed to hold a value most useful for you, it's only 
guaranteed to hold a defined value.


Re: Static executable (ldc, linux)

2022-10-25 Thread Kagamin via Digitalmars-d-learn

ldc2 -link-defaultlib-shared=false or something like that


Re: How to workaround on this (bug?)

2022-09-23 Thread Kagamin via Digitalmars-d-learn

Provide two functions and let the caller choose
```
void fun(ref Variant v) nothrow
{
}

void fun2(Variant v)
{
fun(v);
}
```


Re: toString doesn't compile with -dip1000 switch

2022-08-01 Thread Kagamin via Digitalmars-d-learn

Bar.toString is typed `@system`.


Re: char* pointers between C and D

2022-07-25 Thread Kagamin via Digitalmars-d-learn

This is how to do it the D way:
```
int main(string[] args)
{
string ch1 = "Hello World!";
char[] ch2="Hello World!".dup;

string s1=ch1[1..$];
char[] s2=ch2[1..$];

writeln(s1);
writeln(s2);

return 0;
}
```


Re: null == "" is true?

2022-07-20 Thread Kagamin via Digitalmars-d-learn

On Tuesday, 19 July 2022 at 18:05:34 UTC, Antonio wrote:
In a relational database, `NULL` is not the same that `""`... 
and `NULL` is not the same that `0`.  Are semantically 
different and there are database invariants (like foreign keys) 
based on it.   Trying to "mix" this concepts in a database is a 
mistake.


So, it's an implementation detail or a relational database that 
leaks into business logic because nobody thought about it? Just 
because a relational database has many features, it doesn't mean 
business logic must use them all, it must use only what makes 
sense for business logic.


When you treat with Domain Models, you try to represent this 
semantics in all levels of your software... including APIs


What semantics your domain models implement? Is it semantics of 
all features of a relational database or is semantics of business 
logic?


Re: null == "" is true?

2022-07-19 Thread Kagamin via Digitalmars-d-learn

Also what's the difference between null and empty phone number?


Re: null == "" is true?

2022-07-19 Thread Kagamin via Digitalmars-d-learn

On Tuesday, 19 July 2022 at 10:29:40 UTC, Antonio wrote:
The summary is that a DTO that works like a Map needs to 
represent the absent key ant this is not the same that the Null 
value


Example:
```d
struct Null { /*...*/ }
struct Undefined { /*...*/ }
struct ContactDto {
 DtoVal!(Undefined, string) name
 DtoVal!(Undefined, Null, string) phonenumber,
 DtoVal!(Undefined, AddressDto) address
}
// ...
ContactDto data = {phonenumber:Null(), 
address:{city:{code:"BCN"}}};

updateContact(id, data);

```


As I understand, in your scenario there's no difference between 
null string and empty string, they both work like empty string, 
and D treats them as empty string. That's what I mean when I said 
that distinction between null and empty is meaningless.


Re: null == "" is true?

2022-07-19 Thread Kagamin via Digitalmars-d-learn

On Monday, 18 July 2022 at 21:23:32 UTC, Antonio wrote:
I will study it in detail and report (if required). May be, I 
will write the DTO problem with D article if I find time in 
august.


In my experience null and empty in DTOs usually play the same 
logical role. It's a very contrived technical difference without 
practical usage, such distinction is way beyond any business 
logic. Even if you implement this distinction, I'm not sure 
anybody will carefully pay attention to it. In languages that 
make difference between null and empty, null is often replaced 
with empty to work around problems with null, such codebase can't 
properly preserve null values.


Re: null == "" is true?

2022-07-18 Thread Kagamin via Digitalmars-d-learn

On Tuesday, 12 July 2022 at 20:36:03 UTC, Antonio wrote:
Honestly, it is difficult to understand for newcomers... there 
is a reason, but there is a reason in javascript for `0 == ''` 
too


People would have different preferences there. Difference between 
null and empty is useless. D does the right thing here, what 
other languages do, is a mistake. If you want such difference, 
use the Nullable wrapper or Algebraic.


Re: How to debug thread code

2022-07-12 Thread Kagamin via Digitalmars-d-learn

On Sunday, 10 July 2022 at 21:27:08 UTC, Hipreme wrote:
"Your app has entered a break state, but there is no code to 
show because all threads were executing external code 
(typically system or framework code)."


Open the threads window and click on threads there, their stack 
will be in the stack window.


Re: freebsd dub linker error

2022-06-01 Thread Kagamin via Digitalmars-d-learn

Try to run clang with -v option and compare with gcc.


Re: Is there a way to not escape slashes when parsing JSON?

2022-02-21 Thread Kagamin via Digitalmars-d-learn

On Monday, 21 February 2022 at 09:04:06 UTC, bauss wrote:
Why are we even escaping them by default, it should be the 
other way around, that slashes are only escaped if you ask for 
it; that's how it literally is in almost every JSON library.


Really? I always see escaped slashes in JSON, e.g. wikipedia does 
this, but everything else too.


Re: How to verify DMD download with GPG?

2022-02-14 Thread Kagamin via Digitalmars-d-learn

3AAF1A18E61F6FAA3B7193E4DB8C5218B9329CF8 is 0xDB8C5218B9329CF8
This shortening was supposed to improve user experience.


Re: Cross Compile to Linux from Windows using LDC?

2022-02-14 Thread Kagamin via Digitalmars-d-learn
Isn't cross-linker enough? My regular mingw build of ld says it 
supports elf64-x86-64 traget, so I assume something like this 
should be enough:
ld -b elf64-x86-64 -L lib --dynamic-linker 
/lib64/ld-linux-x86-64.so.2 --as-needed --gc-sections -s 
lib/crt1.o lib/crti.o my.o -lc lib/crtn.o


Re: How to print unicode characters (no library)?

2021-12-27 Thread Kagamin via Digitalmars-d-learn

On Monday, 27 December 2021 at 11:21:54 UTC, rempas wrote:
So should I just use UTF-8 only for Linux? What about other 
operating systems? I suppose Unix-based OSs (maybe MacOS as 
well if I'm lucky) work the same as well. But what about 
Windows? Unfortunately I have to support this OS too with my 
library so I should know. If you know and you can tell me of 
course...


https://utf8everywhere.org/ - this is an advise from a windows 
programmer, I use it too. Windows allocates a per thread buffer 
and when you call, say, WriteConsoleA, it first transcodes the 
string to UTF-16 in the buffer and calls WriteConsoleW, you would 
do something like that.


Re: How to print unicode characters (no library)?

2021-12-27 Thread Kagamin via Digitalmars-d-learn

On Monday, 27 December 2021 at 07:29:05 UTC, rempas wrote:
How can you do that? I'm trying to print the codes for them but 
it doesn't work. Or you cannot choose to have this behavior and 
there are only some terminals that support this?


Try it on https://en.wikipedia.org/wiki/Teletype_Model_33


Re: How to print unicode characters (no library)?

2021-12-27 Thread Kagamin via Digitalmars-d-learn
D strings are plain arrays without any text-specific logic, the 
element is called code unit, which has a fixed size, and the 
array length specifies how many elements are in the array. This 
model is most adequate for memory correctness, i.e. it shows what 
takes how much memory and where it will fit. D doesn't impose 
fixed interpretations like characters or code points, because 
there are many of them and neither is the correct one, you need 
one or another in different situations. Linux console one example 
of such situation: it doesn't accept characters or code points, 
it accepts utf8 code units, using anything else is an error.


Re: French and Greek strings

2021-11-17 Thread Kagamin via Digitalmars-d-learn
You can use the string type to hold non-ascii characters. Just a 
substring of another string.


Re: abs and minimum values

2021-10-29 Thread Kagamin via Digitalmars-d-learn

Unsigned integers aren't numbers.
assert(-abs(1)<0);


Re: Analyze debug condition in template

2021-10-27 Thread Kagamin via Digitalmars-d-learn

You can do something like
```d
enum LogSettings
{
  func1,func2,func3
}

alias logger!LogSettings logf;

void func1()
{
  logf(...);
}
```

Then the logger can inspect symbols in the template argument and 
compare their names to the function name.


Re: Analyze debug condition in template

2021-10-26 Thread Kagamin via Digitalmars-d-learn

`debug(func1)writefln(...)`
But specify a global debug version for the compiler:
`dmd -debug=func1 app.d`


Re: Why do we have Dmain?

2021-10-22 Thread Kagamin via Digitalmars-d-learn

Actually C runtime is many megabytes in size.


Re: How to test if a string is pointing into read-only memory?

2021-10-12 Thread Kagamin via Digitalmars-d-learn

On Tuesday, 12 October 2021 at 08:19:01 UTC, jfondren wrote:
and string literals weren't reliably in read-only memory as 
recently as early 2017: 
https://github.com/dlang/dmd/pull/6546#issuecomment-280612721


Sometimes sections have defined symbols for start and end, you 
can check if the string is in rdata section. On windows you can 
test it generically with IsBadWritePtr function.


Re: Python's list equivalent with std.variant?

2021-10-05 Thread Kagamin via Digitalmars-d-learn

On Sunday, 3 October 2021 at 22:22:48 UTC, rjkilpatrick wrote:

```d
import std.stdio : writeln;
import std.variant;
import std.conv;

// Arbitrary super class
class SuperClass {
this() {
}
}

// Derived class with members
class DerivedClass : SuperClass {
public:
this(float a) {
this.a = a;
}
float a;
}

class OtherDerivedClass : SuperClass {}

void main() {
// When we use `SuperClass[] list;` here, we find 'a' is 
hidden by the base class

Variant[] list;

// Attempting to append derived class instances to list
list ~= new DerivedClass(1.0f);
list ~= new OtherDerivedClass;

list[0].a;
list[0].to!(get!(list[0].type)).a.writeln;
}
```


Looks like you want full duck typing. Dynamic objects are just 
hashtables of properties, so an array of them is something like 
this:

Variant[string][] list;
Variant[string] obj;
obj["a"]=Variant(1.0f);
list[0]["a"].get!float.writeln;


Re: better c fibers

2021-09-28 Thread Kagamin via Digitalmars-d-learn

On Tuesday, 21 September 2021 at 09:37:30 UTC, Abby wrote:

Hi there,
I'm new in dlang I specially like betterC. I was hoping that d 
fibers would be implemented in without using classes, but there 
are not.


On windows you can use the fiber api 
https://docs.microsoft.com/en-us/windows/win32/procthread/fibers 
just as you would do in C.


Re: Scope with owner types

2021-09-23 Thread Kagamin via Digitalmars-d-learn
Yes, the `return` attribute is what should do it. You also need 
to compile the code with -dip1000 option.


Re: Run-time setting of immutable variable?

2021-09-02 Thread Kagamin via Digitalmars-d-learn

If you want only address, you can keep it as size_t:

ubyte[10] Arr;
immutable size_t Address;
static this() {
Address = cast(size_t)([0]);
}


Re: Error load: QtE5Widgets64.dll

2021-08-31 Thread Kagamin via Digitalmars-d-learn
Maybe you're trying to load a 32-bit library into a 64-bit 
process.


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

2021-08-20 Thread Kagamin via Digitalmars-d-learn
On Wednesday, 18 August 2021 at 17:56:53 UTC, Ruby The Roobster 
wrote:
When I removed those two lines of code, the program ran 
perfectly without displaying any error or throwing any 
exception...


The errors aren't always nicely located and can be elsewhere. Try 
to write a minimal runnable example. Also that cast won't work, 
you don't really know what you're doing there, use toStringz 
instead.


Re: Shift operator, unexpected result

2021-06-10 Thread Kagamin via Digitalmars-d-learn

On Wednesday, 9 June 2021 at 19:13:10 UTC, JG wrote:

produces:

123

I would expect 0.

What is the rationale for this behaviour or is it a bug?


Processor just takes lower 6 bits for the shift amount and those 
hold zero in your case, shifting by 65 will shift by 1.


Re: How to cross build a RISC-V target betterC code using LDC on x86-64 machine

2021-06-07 Thread Kagamin via Digitalmars-d-learn

https://forum.dlang.org/post/koxqrqqzadfefbgkd...@forum.dlang.org


Re: How do I create classes dynamically?

2021-04-16 Thread Kagamin via Digitalmars-d-learn

On Friday, 16 April 2021 at 08:31:27 UTC, Imperatorn wrote:

One example would be a repl


That has little to do with what OP meant.


Re: How do I create classes dynamically?

2021-04-15 Thread Kagamin via Digitalmars-d-learn

On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:
Maybe I am just too short in D, but I wanted to find out if it 
is possible to create classes dynamically. My problem is, I 
just don't know where to start reading. Maybe at mixin 
templates?


CreateClassWithName!("MyDynamicClassName");

should create the following class to work with dynamically:

class MyDynamicClassName {
this() { writeln("I was not written, but still I exist!"); }
}

So that I in the end by means of

MyDynamicClassName cls = new MyDynamicClassName;


String mixins is D replacement of macros for code generation.
Works like this:
```d
mixin("class MyDynamicClassName { }");
MyDynamicClassName cls = new MyDynamicClassName;
```


Re: "this" as default parameter for a constructor.

2021-04-12 Thread Kagamin via Digitalmars-d-learn

class foo {
this ( foo p /* , other params */ ) {
parent = p;
}
foo create() {
return new foo(this);
}
void use() {
foo f = create();
}

foo parent;
}


Re: Why I need DUB? Will never DMD don't just use import for import packages?

2021-04-08 Thread Kagamin via Digitalmars-d-learn

On Monday, 29 March 2021 at 19:06:33 UTC, Marcone wrote:
Why can't I just use: import vibe.vibe; for import packages 
like Nim or Python? Why I still use DUB?


Theoretically an rdmd-like tool can automatically infer 
dependencies from imports (autodub?). But it can also easily 
expose you to a supply chain attack.


Re: Don't allow to reassign, but content is editable

2021-04-07 Thread Kagamin via Digitalmars-d-learn

struct A
{
private int[] a;
this(int[] b){a=b;}
int[] c(){ return a; }
@disable void opAssign();
}
struct B
{
A a;
this(int){ a=new int[5]; }
int[] b(){ return a.c; }
void f(){ a=new int[5]; }
}


Re: Can I make this work?

2021-03-20 Thread Kagamin via Digitalmars-d-learn

struct S
{
this(string s)
{
type = Type.type1;
}

this(int n)
{
type = Type.type2;
}

Type type;
int n;
}

int value(S s)()
{
static if(s.type == Type.type2) {
// do something
}

return n;
}



Re: How to delete dynamic array ?

2021-03-19 Thread Kagamin via Digitalmars-d-learn
On Thursday, 18 March 2021 at 17:57:30 UTC, Patrick Schluter 
wrote:
It's important to understand that [] is just a practical syntax 
for a fat pointer.


Thinking of [] just as a fancy pointer helps imho to clarify 
that the pointed to memory nature is independant of the pointer 
itself.


I think they are arrays alright. What's missing is expression of 
ownership, because D follows traditional language design 
approach, and traditionally ownership wasn't expressed in 
language and was done by convention.


Re: tiny alternative to std library

2021-03-06 Thread Kagamin via Digitalmars-d-learn
I suppose commercial vendors aren't interested in suckless 
paradigm, so this library is for people and only for people.


Re: tiny alternative to std library

2021-03-05 Thread Kagamin via Digitalmars-d-learn

On Wednesday, 3 March 2021 at 20:54:43 UTC, Anthony Quizon wrote:
I'm having some success pulling out small bits of code from 
other libraries and keeping things minimal and c-style-ish.


If you're really ok with minimalism, I'm writing such a library 
https://filebin.net/7gtyh5j01gk1ofly
I didn't publish it anywhere yet, feel free to do so. Not 
everything is finished there yet.


Re: Name mangling problem with tiny Windows 10 load-time DLL example

2021-02-28 Thread Kagamin via Digitalmars-d-learn

mangleof should give _D4file6addOneFiZi, not _D6patron6addOneFiZi


Re: Real simple unresolved external symbols question...

2021-02-10 Thread Kagamin via Digitalmars-d-learn

Add libraries that provide missing symbols.


Re: Compile time check for GC?

2021-01-28 Thread Kagamin via Digitalmars-d-learn

You can make it opt in, it's insurance.


Re: Compile time check for GC?

2021-01-27 Thread Kagamin via Digitalmars-d-learn
You can define a symbol that will conflict with GC and prevent 
linking with it.


Re: Surprising behaviour of std.experimental.allocator

2020-12-26 Thread Kagamin via Digitalmars-d-learn

Try to compile in debug mode, maybe you breach some contract.


Re: Updating to newer files with different disk formats

2020-12-16 Thread Kagamin via Digitalmars-d-learn
You can compare like time1 > time2 + 100.msec, though posix 
specifies only second precision.


Re: CMD && comand not work

2020-12-11 Thread Kagamin via Digitalmars-d-learn

On Thursday, 10 December 2020 at 21:01:30 UTC, Marcone wrote:
In this very generic example && not work to finalize the 
instruct and start a new instruct. Yes, I know dmd can build 
and run without it, but this is only a example.


execute(["cmd", "/c", "dmd test.d", "&&", "start test.exe"]);

How can I substitute && ?


Try other variants:

execute(["cmd", "/c", "dmd test.d && start test.exe"]);
execute(["cmd", "/c", "dmd", "test.d", "&&", "start", 
"test.exe"]);


Re: Is garbage detection a thing?

2020-12-01 Thread Kagamin via Digitalmars-d-learn

On Sunday, 29 November 2020 at 19:09:07 UTC, Mark wrote:

Looking at Ada now.


I found: Ada is not good for me. It has no augmented 
assignment. It's just that I want DRY because I use very 
verbose variable names


Using a reasonable naming convention should be much easier than 
looking for a perfect custom language. Well, another variant is 
zig, which was supposed to be C but safe.


Re: Is garbage detection a thing?

2020-11-29 Thread Kagamin via Digitalmars-d-learn

Maybe Ada.


Re: Function Pointer Not Working

2020-11-19 Thread Kagamin via Digitalmars-d-learn
The delegate is stored on the stack of the calling thread, the 
created thread loads it from there, but the calling thread 
doesn't wait for that and clobbers the stack right away. If you 
were lucky your code would crash.


Re: magically a static member on init?

2020-11-17 Thread Kagamin via Digitalmars-d-learn
On Saturday, 14 November 2020 at 23:30:58 UTC, Adam D. Ruppe 
wrote:

On Saturday, 14 November 2020 at 23:20:55 UTC, Martin wrote:

Is this intentional?


In the current language design, yes.


It's a bug, it breaks data sharing guarantees.


Re: DMD: invalid UTF character `\U0000d800`

2020-11-08 Thread Kagamin via Digitalmars-d-learn

On Sunday, 8 November 2020 at 10:47:34 UTC, Per Nordlöw wrote:

dchar


Surrogate pairs are used in rules because java strings are utf-16 
encoded, it doesn't make much sense for other encodings.


Re: is type checking in D undecidable?

2020-10-23 Thread Kagamin via Digitalmars-d-learn

On Thursday, 22 October 2020 at 18:24:47 UTC, Bruce Carneal wrote:
Per the wiki on termination analysis some languages with 
dependent types (Agda, Coq) have built-in termination checkers.


What they do with code that does, say, a hash preimage attack?


Re: It is possible to substract 5 from 3 unsigned integer

2020-10-08 Thread Kagamin via Digitalmars-d-learn

On Tuesday, 6 October 2020 at 18:24:14 UTC, Alaindevos wrote:

A logical one. For the last one higher classes might be needed.


Also assert(5/3==1);


Re: How to spawn a thread within a GtkD button event handler

2020-10-08 Thread Kagamin via Digitalmars-d-learn
See 
https://forum.dlang.org/post/kqvpjwbkpravywald...@forum.dlang.org


Re: Cannot call @system funciton (stdout)

2020-09-19 Thread Kagamin via Digitalmars-d-learn

if(GetFileType(GetStdHandle(STD_OUTPUT_HANDLE))==FILE_TYPE_PIPE)setvbuf()


Re: Cannot call @system funciton (stdout)

2020-09-19 Thread Kagamin via Digitalmars-d-learn
That said, full buffering for pipes may be not all that 
profitable, so it makes sense to always set line buffering for 
pipes and leave full buffering only for file.


Re: Since DMD 2.089.0 and later, compiled .exe showing SFX zip and opening with winRar when use resource.

2020-08-19 Thread Kagamin via Digitalmars-d-learn

On Tuesday, 18 August 2020 at 19:01:17 UTC, Marcone wrote:

SFX zip in it is properties: https://i.imgur.com/dH7jl5n.png
Opening with winRar: https://i.imgur.com/s7C9mZn.png


Probably winrar messing with your file manager. Try to uninstall 
ungerister winrar from your file manager or try a different file 
manager.


Re: Cannot call @system funciton (stdout)

2020-08-18 Thread Kagamin via Digitalmars-d-learn

On Sunday, 16 August 2020 at 18:13:07 UTC, Anonymouse wrote:
Just as a drive-by comment, the main stdio thing I came across 
that I couldn't do from within @safe was stdout.flush(), which 
I need to call manually for Cygwin terminals and some terminals 
embedded in editors (vscode). If someone knows why, please tell 
me.


Cygwin terminals are not recognized as terminals, you should set 
line buffered mode, then it will flush every line.


Re: Why is time_t defined as a 32-bit type on Windows?

2020-08-07 Thread Kagamin via Digitalmars-d-learn
Because it's used with C `time` function 
https://github.com/dlang/druntime/blob/master/src/core/stdc/time.d#L37 which is provided by msvcrt as 32-bit function. 64-bit variant has a different name.


Re: Good way to send/receive UDP packets?

2020-07-28 Thread Kagamin via Digitalmars-d-learn

On Monday, 27 July 2020 at 09:41:44 UTC, wjoe wrote:
But it's possible when bound with the socket option 
SO_REUSEPORT (at least that's the name of the flag on linux 
since 3.9).


The docs say it can't be used to hijack an address.
This option must be set on each socket (including the first 
socket) prior to calling bind(2) on the socket.


Re: Good way to send/receive UDP packets?

2020-07-23 Thread Kagamin via Digitalmars-d-learn

On Wednesday, 22 July 2020 at 16:14:24 UTC, wjoe wrote:
If you send a UDP datagram to a single address, however, it 
will still be delivered to every program on that PC which 
receives UDP datagrams from that port.


Normally binding two sockets to the same port is not allowed.


Re: how to assign to shared obj.systime?

2020-07-14 Thread Kagamin via Digitalmars-d-learn
Yes, all the synchronization and casting pretty much mandates 
that shared data must be behind some kind of abstraction for 
better ergonomics and better correctness too.


Re: how to assign to shared obj.systime?

2020-07-14 Thread Kagamin via Digitalmars-d-learn

---
import std;

shared class TimeCount {
synchronized void startClock() {
auto me = cast()this;
me.startTime = Clock.currTime;
}
synchronized void endClock() {
auto me = cast()this;
me.endTime = Clock.currTime;
}
synchronized void calculateDuration() {
auto me = cast()this;
me.elapsed = me.endTime - me.startTime;
}

private:
SysTime startTime;
SysTime endTime;
Duration elapsed;
}
---
forgot synchronized attribute


Re: how to assign to shared obj.systime?

2020-07-14 Thread Kagamin via Digitalmars-d-learn

---
import std;

shared class TimeCount {
void startClock() {
auto me = cast()this;
me.startTime = Clock.currTime;
}
void endClock() {
auto me = cast()this;
me.endTime = Clock.currTime;
}
void calculateDuration() {
auto me = cast()this;
me.elapsed = me.endTime - me.startTime;
}

private:
SysTime startTime;
SysTime endTime;
Duration elapsed;
}
---
And this is shorter than your unshared member specification.


Re: how to assign to shared obj.systime?

2020-07-14 Thread Kagamin via Digitalmars-d-learn

On Monday, 13 July 2020 at 07:26:06 UTC, Arafel wrote:
That's exactly why what I propose is a way to *explicitly* tell 
the compiler about it, like @system does for safety.


With __gshared you can opt out from sharing safety, then you're 
back to old good C-style multithreading.


Re: How can I make executeShell ask for Admin Elevation?

2020-07-12 Thread Kagamin via Digitalmars-d-learn

I mean runas your own program.


Re: How can I make executeShell ask for Admin Elevation?

2020-07-12 Thread Kagamin via Digitalmars-d-learn
Well, you can elevate your own program and tell it to run that 
command, collect the result and send it back through e.g. shared 
memory.


Re: How can I make executeShell ask for Admin Elevation?

2020-07-12 Thread Kagamin via Digitalmars-d-learn
You call ShellExecute with "runas" verb: 
https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea


Re: how to assign to shared obj.systime?

2020-07-10 Thread Kagamin via Digitalmars-d-learn

On Friday, 10 July 2020 at 17:18:25 UTC, mw wrote:

On Friday, 10 July 2020 at 08:48:38 UTC, Kagamin wrote:

On Friday, 10 July 2020 at 05:12:06 UTC, mw wrote:

looks like we still have to cast:
as of 2020, sigh.


Why not?


Because cast is ugly.


Implicitly escaping thread local data into shared context is much 
uglier than a cast. D disallows such implicit sharing, and thus 
ensures existence of thread local data on the language level. 
SysTime wasn't designed to be shared and due to this is 
incompatible with sharing by default, which enforces the promise 
that SysTime must be thread local, because it wasn't designed to 
be shared.


synchronized setTime(ref SysTime t) shared {
(cast()this).time = t;
}
Steven's solution isn't good in the general case, because it 
still puts thread local data in shared context, which itself is a 
problem, because it makes thread local data implicitly shared, 
and when you work with such implicitly shared thread local data, 
you can't assume it's thread local, because it might be escaped 
into shared context. In this case the language prevented implicit 
sharing of thread local data (this is what shared does and does 
it well contrary to the popular myth that shared is broken).


Re: how to assign to shared obj.systime?

2020-07-10 Thread Kagamin via Digitalmars-d-learn

On Friday, 10 July 2020 at 05:12:06 UTC, mw wrote:

looks like we still have to cast:
as of 2020, sigh.


Why not?


Re: App/lib config system

2020-07-10 Thread Kagamin via Digitalmars-d-learn

Without contradictions the solution is trivial:

module config;

version(LogEnabled) enum isEnabled=true;
else enum isEnabled=false;

shared int level;


Re: App/lib config system

2020-07-10 Thread Kagamin via Digitalmars-d-learn

Contradictions that don't let you glue it all together.


Re: App/lib config system

2020-07-09 Thread Kagamin via Digitalmars-d-learn
If you suspect there's a contradiction in requirements, you need 
to specify them with better precision.


Re: Catching OS Exceptions in Windows using LDC

2020-07-04 Thread Kagamin via Digitalmars-d-learn
try 
https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-setunhandledexceptionfilter


Re: Delegates and C++ FFI lifetimes

2020-07-03 Thread Kagamin via Digitalmars-d-learn

You can store the delegate in an array and invoke it by index.


Re: Program exited with code -11 when calling

2020-06-30 Thread Kagamin via Digitalmars-d-learn
bson_t* bson_new_from_json(in char* data, long len, bson_error_t* 
error);


string str_utf8 = "{\"a\":1}";
bson_error_t error;

auto bson = bson_new_from_json(str_utf8.ptr, str_utf8.length, 
);


You have a wrong declaration for bson_error_t too.


Re: Calling C functions

2020-06-30 Thread Kagamin via Digitalmars-d-learn
On Monday, 29 June 2020 at 19:55:59 UTC, Steven Schveighoffer 
wrote:
Yep, for sure. I'll file an issue. Anyone know why the calling 
convention would differ?


It's easier to enforce left to right evaluation order this way: 
arguments are pushed to stack as they are evaluated, which is 
pascal calling convention.


Re: Garbage collection

2020-06-30 Thread Kagamin via Digitalmars-d-learn

On Saturday, 27 June 2020 at 14:49:34 UTC, James Gray wrote:
I have produced something which essentially reproduces my 
problem.


What is the problem? Do you have a leak or you want to know how 
GC works?


Re: Light-weight runtime

2020-06-29 Thread Kagamin via Digitalmars-d-learn

On Sunday, 28 June 2020 at 07:09:53 UTC, Виталий Фадеев wrote:

I want light-weight runtime !

How to ?


Runtime provides language features that rely on extra code. 
Removing that code from runtime means to give up on corresponding 
language features. This way you can implement only features you 
want or even reduce runtime to almost zero. You can start by not 
linking your code with druntime and see what's missing and see if 
you can evade or implement the feature.


Re: How to send ownerTid into a parallel foreach loop?

2020-06-27 Thread Kagamin via Digitalmars-d-learn

On Saturday, 27 June 2020 at 07:51:21 UTC, adnan338 wrote:

On Saturday, 27 June 2020 at 07:31:56 UTC, Kagamin wrote:
std.concurrency is for noninteractive appications, the 
approach with gui timer was the correct one.


Thank you. That works but my progress bar is sometimes getting 
stuck because of a possible data race.
See 
https://forum.dlang.org/post/gacweulvbyorkseti...@forum.dlang.org


Sometimes? In that code you write the progress variable only 
once, so it doesn't change after that. Nothing else can possibly 
happen there with or without multithreading.


Re: How to send ownerTid into a parallel foreach loop?

2020-06-27 Thread Kagamin via Digitalmars-d-learn
std.concurrency is for noninteractive appications, the approach 
with gui timer was the correct one.


Re: Downloading files over TLS

2020-06-26 Thread Kagamin via Digitalmars-d-learn

On Friday, 26 June 2020 at 10:12:09 UTC, Jacob Carlborg wrote:
Downloading files over TLS. This seems that it's something that 
should be quite simple to do. My high level goals are 
cross-platform and easy distribution. I don't need anything 
fancy just a simple API like this:


download("https://url.com;, "/local/file");


Maybe just start wget or something like that?

* Network [6]. This is an Apple specific library provided by 
the platform. This is the recommend alternative to 
SecureTransport. The problem is that this is not just an 
alternative TLS implementation, it's a completely different 
alternative to BSD sockets. The API is completely different and 
will require some extra layers to to provide a cross-platform 
API. This means that I cannot use any of the existing library 
to just add TLS, it will be a completely different 
implementation, which might be ok. Another big problem is that 
it only available on macOS 10.14 and later. I have not decided 
yet if this is acceptable or not


Since you want the latest certificate storage, you intend to 
support only the latest system. Many root certificates will 
timeout now.


* Are there any high level APIs, like NSURLSession, on Windows 
that can be used to download files?


https://docs.microsoft.com/en-us/windows/win32/winhttp/about-winhttp


Re: How to correctly integrate D library to Swift/Obj-C mobile project?

2020-06-22 Thread Kagamin via Digitalmars-d-learn
If you want to use them from D, you need those classes and 
methods declared in the D language, in text.


Re: GtkD code review - How to update a progressbar using data sharing concurrency

2020-06-21 Thread Kagamin via Digitalmars-d-learn

Not sure how much synchronization do you want to do.

import gio.Application : GioApplication = Application;
import gtk.Application : Application;
import gtk.ApplicationWindow : ApplicationWindow;
import gtk.ProgressBar : ProgressBar;
import glib.Timeout : Timeout;
import gtkc.gtktypes : GApplicationFlags, GPriority;

shared class Downloader
{
import core.atomic;
string[] links = [`link1`, `link2`, `link3`, `link4`];
private size_t completed = 0;

size_t count()
{
return atomicLoad(completed);
}

void increment()
{
atomicOp!"+="(completed, 1);
}

static void start(shared Downloader downloader)
{
import std.parallelism : parallel;
import core.thread : Thread, seconds;

{
// emulate HTTP response overhead;
Thread.sleep(seconds(2));
}
{
// emulate random Download time
import std.random : Random, uniform;

auto rnd = Random(4361);

foreach (ref link; downloader.links.parallel())
{
Thread.sleep(uniform(0, 6, rnd).seconds());
downloader.increment();
}
}
}
}

class ProgressIndicatorBar : ProgressBar
{
this()
{
super.setShowText(true);
super.setPulseStep(0.2);
}
}

class PrimaryWindow : ApplicationWindow
{
const int width = 320, height = 100;
ProgressIndicatorBar pib;
this(Application app)
{
super(app);
super.setSizeRequest(width, height);
scope (success)
super.showAll();

pib = new ProgressIndicatorBar();
scope (success)
add(pib);

shared downloader = new shared Downloader();

import std.parallelism : task;

auto downloadTask = task!(Downloader.start)(downloader);
downloadTask.executeInNewThread();

auto timeout = new Timeout(100, delegate bool() {
			const long completed=downloader.count, 
total=downloader.links.length;

if (completed < total)
{
if (completed == 0)
{
pib.setText(`Awaiting response...`);
pib.pulse();
}
else
{
pib.setText(`Downloading...`);
double fraction=completed; 
fraction/=total;
pib.setFraction(fraction);
}
return true;
}
else
{
super.setTitle(`Downloading complete`);
// pib.setShowText(false);
pib.setVisible(false);
return false;
}
}, GPriority.HIGH);
}
}

int main(string[] args)
{
	auto application = new 
Application(`org.gitlab.helloprogressbar`, 
GApplicationFlags.FLAGS_NONE);

application.addOnActivate(delegate void(GioApplication app) {
auto appWindow = new PrimaryWindow(application);
});
return application.run(args);
}



Re: Initializing an associative array of struct

2020-06-14 Thread Kagamin via Digitalmars-d-learn

string param="aa";
parameters[param]=Parameter();
in id=parameters[param].id;


Re: Initializing an associative array of struct

2020-06-14 Thread Kagamin via Digitalmars-d-learn

that's
int id=parameters[param].id;


Re: DIP1000 spec?

2020-06-14 Thread Kagamin via Digitalmars-d-learn

Logic is apparently still in flux, too early to document.


Re: How to allocate/free memory under @nogc

2020-05-22 Thread Kagamin via Digitalmars-d-learn

On Thursday, 21 May 2020 at 17:19:10 UTC, Konstantin wrote:
Hi all! I will try to ask again(previous two posts still have 
no answers) : are there any site/page/docs somewhere to track 
actual info about @nogc support in language itself and in 
phobos library? Or, at least plans to extend such support?


https://dlang.org/spec/function.html#nogc-functions - here's 
language support. Phobos tends to infer attributes like @nogc, so 
it's context dependent, the actual info is provided by the 
compiler.


Re: Spawn a Command Line application Window and output log information

2020-05-18 Thread Kagamin via Digitalmars-d-learn

On Monday, 18 May 2020 at 17:20:17 UTC, BoQsc wrote:
It would be great if we could change/customise the icon of the 
Command line application that run the HelloWorld application. 
But I have a bad feeling that it is probably not possible 
without a GUI library.


I think the window icon is just the first icon pulled from the 
executable file.


Re: Spawn a Command Line application Window and output log information

2020-05-18 Thread Kagamin via Digitalmars-d-learn

On Monday, 18 May 2020 at 17:02:02 UTC, BoQsc wrote:
The important question is: how can we change the name/title of 
this Command Line application.


As the simplest solution, you can set the window title in 
shortcut properties.


  1   2   3   4   5   6   7   8   9   >