Re: Explicitly avoid GC of objects?

2019-05-21 Thread Benjamin Schaaf via Digitalmars-d-learn

On Tuesday, 21 May 2019 at 11:54:08 UTC, Robert M. Münch wrote:
Is there a trick to accomplish 2 when objects are created from 
different scopes which need to be kept? So, I have one function 
creating the objects and one using them. How can I keep things 
on the stack between these two functions?


How is 3 done? Is this only useful for static variables?


I'll try to describe rules 2 and 3 as simply as possible: As long 
as you can access the pointer to gc allocated memory in D it will 
not be freed.


So whether that pointer lives on the stack:

int* foo() {
return new int;
}
void bar() {
int* a = foo();
c_fn(a);
}

In static or thread local memory:

int* a;
__gshared int* b;
void bar() {
a = new int;
c_fn(a);
b = a;
c_fn(b);
}

Or on the heap:

class D {
int* a;
}
void bar() {
D d = new D(new int);
c_fn(d.a);
}

Doesn't really matter.


Re: "if" statement

2019-03-25 Thread Benjamin Schaaf via Digitalmars-d-learn

On Sunday, 24 March 2019 at 12:45:13 UTC, Francesco Mecca wrote:

https://run.dlang.io/is/zRcj59

```
alias Alg = Algebraic!(int, string);

void main()
{
int n = 2;
Alg value;

value = n == 2 ? 2 : "string";
}
```

The original code used SumType but the effect is the same.

I suppose that I could write the following:

```
if(n == 2) value = 2;
else value = "string";
```

Is there a workaround for this that maintains a similar 
syntactic structure?
is this behaviour accepted or should the compiler translate the 
first case in the second?


You can achieve the same thing by just constructing your 
algebraic type earlier:


  value = n == 2 ? Alg(2) : Alg("string");


Re: How to pass variables to string mixins?

2019-02-25 Thread Benjamin Schaaf via Digitalmars-d-learn

On Tuesday, 26 February 2019 at 00:07:54 UTC, Victor Porton wrote:
I want to create a string mixin based on a supplementary 
variable (name2 below):


Let we have something like:

mixin template X(string name) {
  immutable string name2 = '_' ~ name;
  mixin("struct " ~ name2 ~ "{ int i; }");
}

But it would create variable name2 inside X, which should not 
be created. How to solve this problem?


Your best bet is to just not declare anything in a mixin template 
that you don't want included:


mixin template X(string name) {
  mixin("struct _" ~ name ~ "{ int i; }");
}

Or don't use mixin templates:

template X(string name) {
  immutable string name2 = '_' ~ name;
  enum X = "struct " ~ name2 ~ "{ int i; }";
}
mixin(X!("foo"));


Re: Disable dub from checking internet before building

2019-02-24 Thread Benjamin Schaaf via Digitalmars-d-learn

On Sunday, 24 February 2019 at 22:51:45 UTC, 0x wrote:
How to disable dub from checking internet before building, it's 
slowing down build whenever it does this.


Its not too well described but --nodeps skips dependency 
resolution (just uses the locked dependencies). Since dub only 
does requests for dependency resolution this stops any requests.


Re: How to create a class-valued variable?

2019-02-19 Thread Benjamin Schaaf via Digitalmars-d-learn

On Tuesday, 19 February 2019 at 22:04:58 UTC, Victor Porton wrote:
What is the right way to store in a structure a class (not an 
instance) derived from a given interface(s)?


What are you trying to do with the "class"? If you just want a 
static "reference" to it you can use an `alias`:


  class A {}
  struct B {
alias C = A;
  }
  new B.C();

If you want dynamic information on the type you can use TypeInfo:

  class A {}
  struct B {
TypeInfo i;
  }
  B b;
  b.i = typeid(A);
  b.i.factory();

Or more simply if you just want to construct instances, just use 
a delegate:


  interface I {}
  class A : I {}
  struct B {
I delegate() factory;
  }
  B b;
  b.factory = () => new A();
  b.factory();

You *can't* do something like the following because types need to 
be validated at compile time:


  struct A {
Class cls;
  }
  A a;
  a.cls b;
  b.run();


Re: Should D file end with newline?

2019-02-10 Thread Benjamin Schaaf via Digitalmars-d-learn
On Sunday, 10 February 2019 at 02:12:43 UTC, Jonathan M Davis 
wrote:
On Saturday, February 9, 2019 2:19:27 PM MST Victor Porton via 
Digitalmars- d-learn wrote:

ISO C++ specifies that the C++ file must end with a newline.

Should D file end with newline, too?


No, there is no need to end D files with a newline. I would 
guess that the vast majority of D files end with a closing 
brace. I just looked at a bunch of files in the standard 
library for the heck of it, and almost all of the ones I looked 
at ended with a closing brace. And those that didn't ended with 
something like an enum declaration and not a newline. 
Personally, I don't leave newlines at the end of files, because 
it looks messy. I don't even recall doing that in C++, though I 
do recall that there supposedly be a rule about it. It seems 
like a pretty bizarre requirement to me, but regardless, I'm 
quite sure that D does not have that requirement.


- Jonathan M Davis


Doing a quick a quick tail on all the source files for dmd, 
druntime and phobos, I only found 6 source files that ended in a 
curly brace and 2 ending in a 'g'. All others ended with a 
newline. Its certainly not required but it is common in the style 
guides I've seen and I personally have my editor automatically 
insert a newline.