Re: Make sure lifetime of helper structs is less than owning struct

2021-11-15 Thread Sebastiaan Koppe via Digitalmars-d-learn

On Monday, 15 November 2021 at 15:56:57 UTC, WebFreak001 wrote:

is this currently possible or maybe possible with DIP1000?


Yes it is. But besides `-dip1000` and `@safe`, it requires the 
use of a pointer:


```D
@safe:

struct ByChunk {
FileReader* r;
void popFront() {}
}

struct FileReader {
ByChunk byChunk() return scope {
return ByChunk();
}
}

void main() {
ByChunk helper;
{
auto file = FileReader();
	helper = file.byChunk;  // Error: address of variable `file` 
assigned to `helper` with longer lifetime

}
helper.popFront;
}
```


Re: Make sure lifetime of helper structs is less than owning struct

2021-11-15 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Nov 15, 2021 at 03:56:57PM +, WebFreak001 via Digitalmars-d-learn 
wrote:
> I have an API with some struct like a file reader. I want to add
> byChunks-like functionality to it, so I'm trying to implement it with
> a helper struct that implements opApply. I have disabled copying the
> file reader struct because it cleans up the resources once it goes out
> of scope, however now I need to temporarily save the resources in the
> helper struct to be able to read from it.
> 
> How can I make sure that the foreach helper struct (and with that the
> copies of the resources) cannot be used once the owning struct goes
> out of scope?
> 
> ```d
> ByChunk helper;
> {
> auto file = FileReader(x);
> helper = file.byChunk;
> }
> helper.popFront; // crash - I want the compiler to disallow this
> ```
> 
> is this currently possible or maybe possible with DIP1000?

What about make ByChunk do the construction of the File in its ctor
instead?  The problem with constructing it separately is that you can't
tie the two lifetimes together.  But if you create the File while
initializing the object, you ensure that the two lifetimes are tied
together, and with @disable this() you can make sure that ByChunk is not
constructible unless the code that opens the File also runs.


T

-- 
Computers shouldn't beep through the keyhole.


Re: Make sure lifetime of helper structs is less than owning struct

2021-11-15 Thread Steven Schveighoffer via Digitalmars-d-learn

On 11/15/21 10:56 AM, WebFreak001 wrote:
I have an API with some struct like a file reader. I want to add 
byChunks-like functionality to it, so I'm trying to implement it with a 
helper struct that implements opApply. I have disabled copying the file 
reader struct because it cleans up the resources once it goes out of 
scope, however now I need to temporarily save the resources in the 
helper struct to be able to read from it.


How can I make sure that the foreach helper struct (and with that the 
copies of the resources) cannot be used once the owning struct goes out 
of scope?


```d
ByChunk helper;
{
     auto file = FileReader(x);
     helper = file.byChunk;
}
helper.popFront; // crash - I want the compiler to disallow this
```

is this currently possible or maybe possible with DIP1000?


Or maybe just use reference counting to avoid the problem altogether. 
That's what iopipe/std.io does.


-Steve


Make sure lifetime of helper structs is less than owning struct

2021-11-15 Thread WebFreak001 via Digitalmars-d-learn
I have an API with some struct like a file reader. I want to add 
byChunks-like functionality to it, so I'm trying to implement it 
with a helper struct that implements opApply. I have disabled 
copying the file reader struct because it cleans up the resources 
once it goes out of scope, however now I need to temporarily save 
the resources in the helper struct to be able to read from it.


How can I make sure that the foreach helper struct (and with that 
the copies of the resources) cannot be used once the owning 
struct goes out of scope?


```d
ByChunk helper;
{
auto file = FileReader(x);
helper = file.byChunk;
}
helper.popFront; // crash - I want the compiler to disallow this
```

is this currently possible or maybe possible with DIP1000?


Re: How to deploy single exe application (?)

2021-11-15 Thread Imperatorn via Digitalmars-d-learn

On Monday, 15 November 2021 at 22:28:41 UTC, russhy wrote:

On Monday, 15 November 2021 at 21:16:14 UTC, Willem wrote:
Using D -- I have created a simple command line utility that 
download some info via a https API and save it in a sqlite3 
database file. To use the exe on another windows machine, I 
need to copy over the relevant sqlite3 and curl DLL files.


Question:  Is there a way to create a single .exe with the 
relevant windows DLL info in there? Can this be done with 
static linking?


Any feedback / pointers on where to start would be greatly 
appreciated.


Many Thanks, Willem


you can statically link libraries just like in other languages

if you use dub it's as easy as this:

```json
"sourceFiles-windows": [
"../_clibs/glfw3.lib",
]
```

if you use plain dmd, then it's simple

```
dmd main.d mylib.lib
```


You are correct. But that only works if you got the static libs. 
If you only have the dynamic libs you have to resort to embedding 
them (if you don't want to ship them separately).


Re: Make sure lifetime of helper structs is less than owning struct

2021-11-15 Thread WebFreak001 via Digitalmars-d-learn
On Monday, 15 November 2021 at 19:24:56 UTC, Sebastiaan Koppe 
wrote:

On Monday, 15 November 2021 at 15:56:57 UTC, WebFreak001 wrote:

is this currently possible or maybe possible with DIP1000?


Yes it is. But besides `-dip1000` and `@safe`, it requires the 
use of a pointer:


```D
@safe:

struct ByChunk {
FileReader* r;
void popFront() {}
}

struct FileReader {
ByChunk byChunk() return scope {
return ByChunk();
}
}

void main() {
ByChunk helper;
{
auto file = FileReader();
	helper = file.byChunk;  // Error: address of variable 
`file` assigned to `helper` with longer lifetime

}
helper.popFront;
}
```


awesome, such a simple solution! Also saves me the pain of 
copying the struct data and avoiding copy constructor and stuff 
by using a pointer.


Re: Exceptions names list

2021-11-15 Thread Ali Çehreli via Digitalmars-d-learn

On 11/15/21 2:28 PM, pascal111 wrote:
> I want to know where the list of exceptions names that "throw" statement
> uses.

Such a list is not very useful. Normally all we need is Exception and 
occasionally one or two other specific types.


There can be hundreds of specific types of Exception a program may 
throw, sometimes unknowingly by calling a library that it uses. That 
list can change dramatically simply by adding a single line to the 
program. (That line may call a new module and that module may throw 
dozens of different Exception types.)


What matters is they are all Exceptions. So, most of the time all one 
needs to do is to catch Exception in main and be done with it:


int main() {
  try {
work();

  } catch (Exception ex) {
stderr.writefln!"ERROR: %s"(ex.msg);
return 1;
  }

  return 0;
}

All of my programs use that idiom. (Also note: Error is not being 
caught; just Exception.)


That try-catch block covers all Exception types that may be thrown. The 
user gets a useful message and we are done.


Except, in some cases a specific exception type is useful. For example, 
I needed to catch ConvException that I know std.conv may throw. (This is 
documented on std.conv documentation.)


In that case, I catch that exception in a lower-level function to give 
the user an understandable error message like "Failed to convert 'hello' 
to an integer" so that the user can try entering new data instead of 
exiting the whole program.


Ali



Re: Exceptions names list

2021-11-15 Thread Imperatorn via Digitalmars-d-learn

On Monday, 15 November 2021 at 22:55:55 UTC, pascal111 wrote:

On Monday, 15 November 2021 at 22:39:36 UTC, Imperatorn wrote:

On Monday, 15 November 2021 at 22:28:28 UTC, pascal111 wrote:
I want to know where the list of exceptions names that 
"throw" statement uses. I searched in dlang site, and google 
but couldn't reach it.


Just so I understand, do you mean a list similar to this?
https://www.completecsharptutorial.com/basic/complete-system-exception.php


Exactly!


You can begin here:
https://dlang.org/phobos/core_exception.html


Re: Exceptions names list

2021-11-15 Thread Imperatorn via Digitalmars-d-learn

On Tuesday, 16 November 2021 at 00:05:45 UTC, Imperatorn wrote:

On Tuesday, 16 November 2021 at 00:03:14 UTC, Imperatorn wrote:

On Monday, 15 November 2021 at 22:55:55 UTC, pascal111 wrote:

On Monday, 15 November 2021 at 22:39:36 UTC, Imperatorn wrote:

On Monday, 15 November 2021 at 22:28:28 UTC, pascal111 wrote:
I want to know where the list of exceptions names that 
"throw" statement uses. I searched in dlang site, and 
google but couldn't reach it.


Just so I understand, do you mean a list similar to this?
https://www.completecsharptutorial.com/basic/complete-system-exception.php


Exactly!


You can begin here:
https://dlang.org/phobos/core_exception.html


If there is no such similar list we should create one.


I just did a quick grep of phobos and matched the derived 
exceptions and got this:

Base64Exception
MessageMismatch
OwnerTerminated
LinkTerminated
PriorityMessageException
MailboxFull
TidMissingException
ConvException
CSVException
EncodingException
MeaCulpa
E2
E3
E
ErrnoException
TestException
FileException
GetOptException
MyEx
JSONException
ProcessException
StdioException
StringException
MatchException
URIException
UUIDParsingException
VariantException
XMLException
ZipException
ZlibException
CheckFailure
FormatException
CurlException
RegexException
WindowsException


Re: Completing C code with D style

2021-11-15 Thread pascal111 via Digitalmars-d-learn

On Sunday, 14 November 2021 at 04:29:53 UTC, forkit wrote:

On Saturday, 13 November 2021 at 23:02:15 UTC, pascal111 wrote:


I touch that D is big language, it's not small like standard 
C. This will cost me much studying, so I think I need slow 
down and learn it step by step.


Yes. C is so much smaller, and thus simpler (till you wanna do 
something complex)


But C also makes it 'simpler' to shoot yourself in the foot ;-)

Even with your simple C code, which is so easily tranposed to 
D, you already benefit from:


- variables being always initialised before use
(i.e no need to initalise i in your for loop, in D)

- array indices being automatically checked for out-of-bounds
(i.e. i < 10  ... in D.. it becomes.. numbers.length )

so you can make your simple code, even 'simpler' in D ;-)


Thanks! I won't hide my real intention. Learning C to learning D 
is like the case of doctor Jekyll and Mr. Hyde, if I'm in the 
place of Jekyll, I treat Hyde as a son and I want to teach him 
programming, and we can't do that with C because it belongs to 
vertical thinking, while D belongs to Hyde or the horizontal 
thinking.


How to deploy single exe application (?)

2021-11-15 Thread Willem via Digitalmars-d-learn
Using D -- I have created a simple command line utility that 
download some info via a https API and save it in a sqlite3 
database file. To use the exe on another windows machine, I need 
to copy over the relevant sqlite3 and curl DLL files.


Question:  Is there a way to create a single .exe with the 
relevant windows DLL info in there? Can this be done with static 
linking?


Any feedback / pointers on where to start would be greatly 
appreciated.


Many Thanks, Willem





Re: Make sure lifetime of helper structs is less than owning struct

2021-11-15 Thread Imperatorn via Digitalmars-d-learn
On Monday, 15 November 2021 at 19:24:56 UTC, Sebastiaan Koppe 
wrote:

On Monday, 15 November 2021 at 15:56:57 UTC, WebFreak001 wrote:

is this currently possible or maybe possible with DIP1000?


Yes it is. But besides `-dip1000` and `@safe`, it requires the 
use of a pointer:


```D
@safe:

struct ByChunk {
FileReader* r;
void popFront() {}
}

struct FileReader {
ByChunk byChunk() return scope {
return ByChunk();
}
}

void main() {
ByChunk helper;
{
auto file = FileReader();
	helper = file.byChunk;  // Error: address of variable 
`file` assigned to `helper` with longer lifetime

}
helper.popFront;
}
```


Maybe a candidate for the "d idioms" page


Re: Make sure lifetime of helper structs is less than owning struct

2021-11-15 Thread Ali Çehreli via Digitalmars-d-learn

On 11/15/21 1:58 PM, WebFreak001 wrote:
> On Monday, 15 November 2021 at 19:24:56 UTC, Sebastiaan Koppe wrote:
>> On Monday, 15 November 2021 at 15:56:57 UTC, WebFreak001 wrote:
>>> is this currently possible or maybe possible with DIP1000?
>>
>> Yes it is. But besides `-dip1000` and `@safe`, it requires the use of
>> a pointer:
>>
>> ```D
>> @safe:
>>
>> struct ByChunk {
>> FileReader* r;
>> void popFront() {}
>> }
>>
>> struct FileReader {
>> ByChunk byChunk() return scope {
>> return ByChunk();
>> }
>> }
>>
>> void main() {
>> ByChunk helper;
>> {
>>auto file = FileReader();
>> helper = file.byChunk;  // Error: address of variable `file`
>> assigned to `helper` with longer lifetime
>> }
>> helper.popFront;
>> }
>> ```
>
> awesome, such a simple solution! Also saves me the pain of copying the
> struct data and avoiding copy constructor and stuff by using a pointer.
I don't see how it solves the problem. Sebastiaan Koppe might have 
intended to allocate the object dynamically with 'new' as well?


import std.stdio;

@safe:

struct ByChunk {
  FileReader* r;

  ~this() {
writeln(__FUNCTION__);
  }

  void popFront() {
writeln("popFront on ", r);
  }
}

struct FileReader {
  ~this() {
writeln(__FUNCTION__, " on ", );
  }

  ByChunk byChunk() return scope {
return ByChunk();
  }
}

void main() {
  ByChunk helper;
  {
auto file = new FileReader();  // <-- NOTE new
helper = file.byChunk;
  }
  writeln("back in main");
  helper.popFront;
}

This is the current output:

deneme.ByChunk.~this
back in main
popFront on 7F12B377E000
deneme.ByChunk.~this
deneme.FileReader.~this on 7F12B377E000

But without that 'new', FileReader is destroyed before popFront:

deneme.ByChunk.~this
deneme.FileReader.~this on 7FFD8231D4B8   <-- BAD
back in main
popFront on 7FFD8231D4B8
deneme.ByChunk.~this

I think I am misunderstanding something here. :)

Ali



Re: How to deploy single exe application (?)

2021-11-15 Thread russhy via Digitalmars-d-learn

On Monday, 15 November 2021 at 21:16:14 UTC, Willem wrote:
Using D -- I have created a simple command line utility that 
download some info via a https API and save it in a sqlite3 
database file. To use the exe on another windows machine, I 
need to copy over the relevant sqlite3 and curl DLL files.


Question:  Is there a way to create a single .exe with the 
relevant windows DLL info in there? Can this be done with 
static linking?


Any feedback / pointers on where to start would be greatly 
appreciated.


Many Thanks, Willem


you can statically link libraries just like in other languages

if you use dub it's as easy as this:

```json
"sourceFiles-windows": [
"../_clibs/glfw3.lib",
]
```

if you use plain dmd, then it's simple

```
dmd main.d mylib.lib
```


Exceptions names list

2021-11-15 Thread pascal111 via Digitalmars-d-learn
I want to know where the list of exceptions names that "throw" 
statement uses. I searched in dlang site, and google but couldn't 
reach it.


Re: Exceptions names list

2021-11-15 Thread Imperatorn via Digitalmars-d-learn

On Tuesday, 16 November 2021 at 00:03:14 UTC, Imperatorn wrote:

On Monday, 15 November 2021 at 22:55:55 UTC, pascal111 wrote:

On Monday, 15 November 2021 at 22:39:36 UTC, Imperatorn wrote:

On Monday, 15 November 2021 at 22:28:28 UTC, pascal111 wrote:
I want to know where the list of exceptions names that 
"throw" statement uses. I searched in dlang site, and google 
but couldn't reach it.


Just so I understand, do you mean a list similar to this?
https://www.completecsharptutorial.com/basic/complete-system-exception.php


Exactly!


You can begin here:
https://dlang.org/phobos/core_exception.html


If there is no such similar list we should create one.


Re: How to deploy single exe application (?)

2021-11-15 Thread pilger via Digitalmars-d-learn

On Monday, 15 November 2021 at 21:16:14 UTC, Willem wrote:
Any feedback / pointers on where to start would be greatly 
appreciated.


https://p0nce.github.io/d-idioms/#Embed-a-dynamic-library-in-an-executable



Re: Make sure lifetime of helper structs is less than owning struct

2021-11-15 Thread Imperatorn via Digitalmars-d-learn

On Monday, 15 November 2021 at 22:27:24 UTC, Ali Çehreli wrote:

On 11/15/21 1:58 PM, WebFreak001 wrote:
> [...]
wrote:
>> [...]
wrote:
>> [...]
the use of
>> [...]
variable `file`
> [...]
copying the
> [...]
a pointer.
I don't see how it solves the problem. Sebastiaan Koppe might 
have intended to allocate the object dynamically with 'new' as 
well?


[...]


Are you compiling with preview=dip1000?


Re: Make sure lifetime of helper structs is less than owning struct

2021-11-15 Thread Ali Çehreli via Digitalmars-d-learn

On 11/15/21 2:33 PM, Imperatorn wrote:

On Monday, 15 November 2021 at 22:27:24 UTC, Ali Çehreli wrote:

On 11/15/21 1:58 PM, WebFreak001 wrote:
> [...]
wrote:
>> [...]
wrote:
>> [...]
the use of
>> [...]
variable `file`
> [...]
copying the
> [...]
a pointer.
I don't see how it solves the problem. Sebastiaan Koppe might have 
intended to allocate the object dynamically with 'new' as well?


[...]


Are you compiling with preview=dip1000?


Trying with it produces an error when 'new' is not used:

Error: reference to local variable `file` assigned to non-scope 
parameter `p` calling deneme.ByChunk.opAssign


So, I either misunderstand or understand very well :) that a different 
way of lifetime management (like adding 'new') is needed there.


Ali



Re: Exceptions names list

2021-11-15 Thread pascal111 via Digitalmars-d-learn

On Monday, 15 November 2021 at 22:44:35 UTC, Ali Çehreli wrote:

On 11/15/21 2:28 PM, pascal111 wrote:
> [...]
"throw" statement
> [...]

Such a list is not very useful. Normally all we need is 
Exception and occasionally one or two other specific types.


[...]


What if I expect more than one exception a function can do in 
"try" block, and I need more than one "catch" block after that, 
so I'll need to define the exception, or you can turn around this 
with some way?


Re: Exceptions names list

2021-11-15 Thread Imperatorn via Digitalmars-d-learn

On Monday, 15 November 2021 at 22:28:28 UTC, pascal111 wrote:
I want to know where the list of exceptions names that "throw" 
statement uses. I searched in dlang site, and google but 
couldn't reach it.


Just so I understand, do you mean a list similar to this?
https://www.completecsharptutorial.com/basic/complete-system-exception.php


Re: Exceptions names list

2021-11-15 Thread pascal111 via Digitalmars-d-learn

On Monday, 15 November 2021 at 22:39:36 UTC, Imperatorn wrote:

On Monday, 15 November 2021 at 22:28:28 UTC, pascal111 wrote:
I want to know where the list of exceptions names that "throw" 
statement uses. I searched in dlang site, and google but 
couldn't reach it.


Just so I understand, do you mean a list similar to this?
https://www.completecsharptutorial.com/basic/complete-system-exception.php


Exactly!


arsd.simpledisplay on macos

2021-11-15 Thread Ben Jones via Digitalmars-d-learn
I'm trying to use Adam's simpledisplay on a mac with XQuartz 
which is installed + running.  When I try to create a window, it 
crashes when calling `XDisplayConnection.get()`.  I'm just 
building dub and have added `"arsd-official:simpledisplay"` as a 
dependency.  Before I installed XQuartz, it wouldn't link because 
of the X related libs missing, so the XQuartz libs did seem to 
get installed to the right place.  The native cocoa 
implementation seemed less well tested than the X version so I 
haven't looked into that too much.


Any idea what's going wrong?  Or is there an example that seems 
to work with the native cocoa version?  I'm only planning to use 
really basic functionality (display an image and maybe capture 
some key presses)


Also, Adam, if you have an idea of what needs to be done to get 
native macos support working, I'm willing to try to give it a 
shot.



```
object.Exception@../../../.dub/packages/arsd-official-10.3.8/arsd-official/simpledisplay.d(12578):
 Unable to open X display

../../../.dub/packages/arsd-official-10.3.8/arsd-official/simpledisplay.d:12578 
arsd.simpledisplay.Display* arsd.simpledisplay.XDisplayConnection.get() 
[0x10f49cc8e]
../../../.dub/packages/arsd-official-10.3.8/arsd-official/simpledisplay.d:13072 
void arsd.simpledisplay.SimpleWindow.impl.createWindow(int, int, 
immutable(char)[], in arsd.simpledisplay.OpenGlOptions, 
arsd.simpledisplay.SimpleWindow) [0x10f4882ec]
../../../.dub/packages/arsd-official-10.3.8/arsd-official/simpledisplay.d:1688 
arsd.simpledisplay.SimpleWindow arsd.simpledisplay.SimpleWindow.__ctor(int, 
int, immutable(char)[], arsd.simpledisplay.OpenGlOptions, 
arsd.simpledisplay.Resizability, arsd.simpledisplay.WindowTypes, int, 
arsd.simpledisplay.SimpleWindow) [0x10f486017]
source/app.d:869 _Dmain [0x10f47dff2]
Program exited with code 1```


Re: Exceptions names list

2021-11-15 Thread Ali Çehreli via Digitalmars-d-learn

On 11/15/21 3:00 PM, pascal111 wrote:
> On Monday, 15 November 2021 at 22:44:35 UTC, Ali Çehreli wrote:
>> On 11/15/21 2:28 PM, pascal111 wrote:
>> > [...]
>> "throw" statement
>> > [...]
>>
>> Such a list is not very useful. Normally all we need is Exception and
>> occasionally one or two other specific types.
>>
>> [...]
>
> What if I expect more than one exception a function can do in "try"
> block,

One benefit of exceptions is separating actual work from error handling. 
Code can be written as if everything will go well. If not, a higher 
level function may catch the exception and e.g. start over. So, no 
matter how many different kinds of exceptions may be thrown or no matter 
how complicated a function is, we just do the work:


void foo() {
  work();
  moreWork();
  etc();
}

Some higher level function may have a try-catch, which may catch a 
potential exception. Fine:


// An intermediate function that uses foo:
void zar() {
  foo();
}

// A higher level function that uses the intermediate function:
void bar() {

  // Try until successful
  while (true) {
try {
  zar();

  // It worked! Let's leave.
  return;

} catch (Exception ex) {
  // Something bad happened
  // Let's try again...
}
  }
}

bar() catches all exceptions without caring what exact type they are. 
(In practice, for me, bar() is usually main(), the top level function.)


> and I need more than one "catch" block after that, so I'll need
> to define the exception, or you can turn around this with some way?

Well, if you *need* to handle a particular exception specially, then you 
have to have a specific catch clause for that exception. Otherwise, a 
single catch(Exception) is sufficient for most programs.


That is possible because all exceptions inherit from Exception:

class MyException : Exception {
  // ...
}

That definition means "MyException is an Exception", which allows 
MyException objects to be caught as Exceptions as well.


Exceptions are a useful tool but they are not suitable for all cases. 
Maybe you have such a case.


Ali