Re: How Different Are Templates from Generics

2019-10-11 Thread Just Dave via Digitalmars-d-learn
Thanks for the thorough explanation. Most of that is how I was 
thinking it worked. However, that leaves me perplexed. If 
templates just generate code then how come:


Wouldnt..

class SomeClass(T) : ISomeInterface!T

and..

class SomeOtherClass(T) : ISomeInterface!T

...generate two different interfaces? Two interfaces that do the 
same thing, but two interfaces nonetheless? I assume each type in 
D has some form of type id underlying everything, which wouldn't 
that make the follow:


if (instance1 is ISomeInterface)
{
Console.WriteLine("Instance1 is interface!");
}

fail? Or is there some extra magic that is making it work with my 
experiments?


How Different Are Templates from Generics

2019-10-11 Thread Just Dave via Digitalmars-d-learn
I come from both a C++ and C# background. Those have been the 
primary languages I have used. In C# you can do something like 
this:


public interface ISomeInterface
{
 T Value { get; }
}

public class SomeClass : ISomeInterface
{
 T Value { get; set; }
}

public class SomeOtherClass : ISomeInterface
{
T Value { get; set; }
}

public static class Example
{
public static void Foo()
{
var instance1 = new SomeClass(){ Value = 4; };
var instance2 = new SomeClass(){ Value = 2; };

if (instance1 is ISomeInterface)
{
Console.WriteLine("Instance1 is interface!");
}

if (instance2 is ISomeInterface)
{
Console.WriteLine("Instance2 is interface!");
}
}
}

Expected output is both WriteLines get hit:

Instance1 is interface!

Instance2 is interface!


So now the 'D' version:

interface ISomeInterface(T)
{
T getValue();
}

class SomeClass(T) : ISomeInterface!T
{
private:
T t;

public:
this(T t)
{
this.t = t;
}

T getValue()
{
   return t;
}
}

class SomeOtherClass(T) : ISomeInterface!T
{
private:
T t;

public:
this(T t)
{
this.t = t;
}

T getValue()
{
return t;
}
}

...which seems to work the same way with preliminary testing. I 
guess my question is...templates are different than generics, but 
can I feel confident continuing forward with such a design in D 
and expect this more or less to behave as I would expect in C#? 
Or are there lots of caveats I should be aware of?


Re: Difference between template and mixin template

2019-10-10 Thread Just Dave via Digitalmars-d-learn

On Thursday, 10 October 2019 at 15:56:36 UTC, Just Dave wrote:
I'm trying to get my head around mixing templates. I'm using it 
as kind of a replacement for class inheritance as it seems to 
fit better composition over inheritance. So I do something like:


mixin template NumberTemplate()
{
private:
int number = 0;
public:
int getNumber(int number)
{
return number;
}
}

interface INumber
{
getNumber(int number);
}

class Number : INumber
{
template NumberTemplate;
};

So two questions:

a) Is this correct usage?

b) It compiles if I just do:

template NumberTemplate()
{
private:
int number = 0;
public:
int getNumber(int number)
{
return number;
}
}

what is the difference between template and mixin template?


Sorry I messed up the above code example the following should 
look like:


class Number : INumber
{
mixin NumberTemplate;
};


Re: C#'s 'is' equivalent in D

2019-10-10 Thread Just Dave via Digitalmars-d-learn

On Thursday, 10 October 2019 at 15:53:20 UTC, Adam D. Ruppe wrote:

On Thursday, 10 October 2019 at 15:47:58 UTC, Just Dave wrote:

if (obj is Person person)


Looks the same as D's

if(auto person = cast(Person) obj) {
  // use person in here
} else {
  // it was some other type
}


Excellent!


Difference between template and mixin template

2019-10-10 Thread Just Dave via Digitalmars-d-learn
I'm trying to get my head around mixing templates. I'm using it 
as kind of a replacement for class inheritance as it seems to fit 
better composition over inheritance. So I do something like:


mixin template NumberTemplate()
{
private:
int number = 0;
public:
int getNumber(int number)
{
return number;
}
}

interface INumber
{
getNumber(int number);
}

class Number : INumber
{
template NumberTemplate;
};

So two questions:

a) Is this correct usage?

b) It compiles if I just do:

template NumberTemplate()
{
private:
int number = 0;
public:
int getNumber(int number)
{
return number;
}
}

what is the difference between template and mixin template?


Re: C#'s 'is' equivalent in D

2019-10-10 Thread Just Dave via Digitalmars-d-learn
Even though static solutions would be more performance minded, 
I'd actually prefer to see the runtime equivalent so I don't have 
to rethink how I think as performance isn't really my major 
concern right now.




C#'s 'is' equivalent in D

2019-10-10 Thread Just Dave via Digitalmars-d-learn

In C# you can do something like:


if (obj is Person)
{
var person = obj as Person;
// do stuff with person...
}

where you can check the type of an object prior to casting. Does 
D have a similar mechanism? It's so widely useful in the C# realm 
that they even added syntactic sugar to allow:


if (obj is Person person)
{
// do stuff with person...
}

I would presume since D has reference objects there must exist 
some mechanism for this...


Re: Dynamic Arrays as Stack and/or Queue

2019-10-08 Thread Just Dave via Digitalmars-d-learn
Thanks for the advice. I used a quick and dirty range solution as 
was suggested. It allowed me to move on as I really wasn't 
looking to fully implement a queue or stack. Just get something 
that semantically behaved as such. I'll return later and optimize 
it with the later suggestions if it's a major issue.


Re: Dynamic Arrays as Stack and/or Queue

2019-10-07 Thread Just Dave via Digitalmars-d-learn

On Monday, 7 October 2019 at 17:24:19 UTC, Ferhat Kurtulmuş wrote:

On Monday, 7 October 2019 at 17:11:08 UTC, Just Dave wrote:
I need a stack and a queue and I noticed that the standard 
library doesn't appear to have one. Which is ok. I just need 
something that can logically behave as a stack and queue, 
which I think the dynamic array should be able to do (if I 
understand correctly this is effectively the equivalent of 
vector in C++ or List in C#). However, I'm having a hard 
time figuring out the best way to push, pop, enqueue and 
dequeue using D.


I'm not seeing here: https://dlang.org/spec/arrays.html, 
anyway to remove from the array. What's the correct 
syntax/method call for this? I see you can easily concatenate 
with '~', but I see no corresponding delete.


Sorry for the newbie question, but I'm just unsure where to 
look for this.


Built-in D arrays rely on garbage collector, and you don't need 
an explicit delete. For nogc arrays, there are 3rd party libs 
and std.container.array. take a look at

https://dlang.org/phobos/std_container_array.html


I'm not talking about memory deletion. I'm talking about push, 
pop, enqueue, and dequeue behavior. I'd assume in a garbage 
collected language letting the reference float off should be 
picked up by the GC.


Re: Dynamic Arrays as Stack and/or Queue

2019-10-07 Thread Just Dave via Digitalmars-d-learn

On Monday, 7 October 2019 at 17:18:03 UTC, bachmeier wrote:

On Monday, 7 October 2019 at 17:11:08 UTC, Just Dave wrote:
I need a stack and a queue and I noticed that the standard 
library doesn't appear to have one. Which is ok. I just need 
something that can logically behave as a stack and queue, 
which I think the dynamic array should be able to do (if I 
understand correctly this is effectively the equivalent of 
vector in C++ or List in C#). However, I'm having a hard 
time figuring out the best way to push, pop, enqueue and 
dequeue using D.


I'm not seeing here: https://dlang.org/spec/arrays.html, 
anyway to remove from the array. What's the correct 
syntax/method call for this? I see you can easily concatenate 
with '~', but I see no corresponding delete.


Sorry for the newbie question, but I'm just unsure where to 
look for this.


Does slicing do what you need?

x[1..$]


I have no clue. My problem is I need a stack and queue and I'm a 
little unsure the 'd' way to do it.


Dynamic Arrays as Stack and/or Queue

2019-10-07 Thread Just Dave via Digitalmars-d-learn
I need a stack and a queue and I noticed that the standard 
library doesn't appear to have one. Which is ok. I just need 
something that can logically behave as a stack and queue, which I 
think the dynamic array should be able to do (if I understand 
correctly this is effectively the equivalent of vector in C++ 
or List in C#). However, I'm having a hard time figuring out 
the best way to push, pop, enqueue and dequeue using D.


I'm not seeing here: https://dlang.org/spec/arrays.html, anyway 
to remove from the array. What's the correct syntax/method call 
for this? I see you can easily concatenate with '~', but I see no 
corresponding delete.


Sorry for the newbie question, but I'm just unsure where to look 
for this.


Re: Does Visual D actually work?

2019-10-07 Thread Just Dave via Digitalmars-d-learn

A machine reboot seems to have fixed the problem...


Does Visual D actually work?

2019-10-07 Thread Just Dave via Digitalmars-d-learn
I downloaded it after experiencing debugging issues with 
CodeBlocks (it wouldn't attach the provided debugger). I 
downloaded Visual D and after some fiddling with Visual Studio 
2019 not supporting third party templates in my version (had to 
update it), I haven't been able to get Visual D to compile 
anything. It looks like it doesn't know where phobos lives?


I'm getting this: std.file.FileException@std\file.d(872) project directory>: The system cannot find the path specified.


I've tried configuring both 'Additional Import Paths' and/or 
'String Import Paths' under Compiler in properties and 'Library 
Search Path' in Linker (not sure why I would need this...but I 
was desperate).


I keep getting a popup that says: 'The operation could not be 
completed. The parameter is incorrect'.


It would help if there was a setup guide for this instead of 
assuming it all works. What IDE should I be using on Windows, 
because so far the ones I've tried are not even close to working 
out of the box (my bare minimum for 'working out of the box' 
would be to at least build and be able to reference the standard 
library).


When dmd installed i put itself on my C:\ drive in a folder 
called 'D'. I've done zero custom here. I just let all of the 
installers do their thing.


Advice?


C++ base constructor call vs. D's

2019-10-02 Thread Just Dave via Digitalmars-d-learn
I was reading the C++ to D page, and came across this little bit 
about when to call the base class constructor:


"It's superior to C++ in that the base constructor call can be 
flexibly placed anywhere in the derived constructor."


Isn't there some inherent danger of not calling the base 
constructor first? Wouldn't C++'s method actually be equal in the 
effect that you could just overwrite whatever value the base 
class set in the derived class?