Re: Abstract Classes

2017-12-05 Thread Ali Çehreli via Digitalmars-d-learn

On 12/05/2017 11:23 PM, IM wrote:

Assume the following:

interface IFace {
   void foo();
   void bar();
}

abstract class A : IFace {
   override void foo() {}
}

class B : A {
   override void bar() {}
}

Now why this fails to compiler with the following message:


--->>>
function bar does not override any function, did you mean to override 
'IFace.bar()'?

<<<---


Obviously, I meant that, since the abstract class A implements IFace, 
and B derives from A.


Do I need to declare IFace's unimplemented methods in A as abstract? If 
yes, why? Isn't that already obvious enough (any unimplemented virtual 
function is abstract)?


Just remove the override keywords in this case. No function is 
overriding any implementation here, they both implement an interface 
function. The fact that override can be used for A.foo can be seen as an 
inconsistency or a bug.


Ali


Abstract Classes

2017-12-05 Thread IM via Digitalmars-d-learn

Assume the following:

interface IFace {
  void foo();
  void bar();
}

abstract class A : IFace {
  override void foo() {}
}

class B : A {
  override void bar() {}
}

Now why this fails to compiler with the following message:


--->>>
function bar does not override any function, did you mean to 
override 'IFace.bar()'?

<<<---


Obviously, I meant that, since the abstract class A implements 
IFace, and B derives from A.


Do I need to declare IFace's unimplemented methods in A as 
abstract? If yes, why? Isn't that already obvious enough (any 
unimplemented virtual function is abstract)?


Re: git workflow for D

2017-12-05 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, December 06, 2017 04:56:17 Arun Chandrasekaran via 
Digitalmars-d-learn wrote:
> Looks like Mercurial is going to be rewritten in Rust
> https://www.mercurial-scm.org/wiki/OxidationPlan
>
> So Facebook don't use D?

As I understand it, the main languages at Facebook are C++ and PHP, but they
use a variety of languages depending on who's doing the work and what part
of Facebook they're in. Some of them were using D while Andrei was working
there. I don't know if they're using it now or not. But even if some of them
are still using D, others could be using Rust or whatever took their fancy
and their bosses let them use. IIRC, some of the Facebook guys in London
were even using Haskell.

- Jonathan M Davis



Re: git workflow for D

2017-12-05 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Monday, 4 December 2017 at 01:26:45 UTC, Arun Chandrasekaran 
wrote:

On Sunday, 3 December 2017 at 23:39:49 UTC, Basile B. wrote:

[...]


If you still lose changes, you could try using Mercurial with 
hggit. It can be a bit slow, but not destructive as git itself. 
;)


I really wish Mercurial won instead of git. Now that hg evolve 
and hg topic are stable, that actually alleviates the need for 
git. But the world talks git now. So everyone else is forced to 
talk in git :(


I guess, without StackOverflow and GitHub, no one would be 
using git.


Facebook uses Mercurial and their team is working on a 
Mercurial server in Rust. 
https://github.com/facebookexperimental/mononoke


I thought Facebook uses DLang as well. No one's motivated to 
write one in DLang?


Looks like Mercurial is going to be rewritten in Rust 
https://www.mercurial-scm.org/wiki/OxidationPlan


So Facebook don't use D?


Re: lower case only first letter of word

2017-12-05 Thread Steven Schveighoffer via Digitalmars-d-learn

On 12/5/17 2:41 PM, kdevel wrote:

On Tuesday, 5 December 2017 at 17:25:57 UTC, Steven Schveighoffer wrote:
[...]

struct LowerCaseFirst(R) // if(isSomeString!R)
{
   R src;
   bool notFirst; // terrible name, but I want default false
   dchar front() {
  import std.uni: toLower;
  return notFirst ? src.front : src.front.toLower;
   }
   void popFront() { notFirst = true; src.popFront; }
   bool empty() { return src.empty; }
}

auto lowerCaseFirst(R)(R r)
{
   return LowerCaseFirst!R(r);
}

Warning: it ain't going to be fast. Auto-decoding everywhere.


But one cannot use the return value of lowerCaseFirst as argument for 
foo(string).


Define foo as:

foo(R)(R r) if (isInputRange!R && isSomeChar!(ElementType!R))

Then it can take any range of char types.

Only the use as argument to writeln seems to work. Also I 
had to put


     import std.range.primitives : front, popFront, empty;

outside the struct otherwise the compiler complains about missing front, 
popFront and empty for type string.


Yeah, it wasn't a complete example. These are the extensions to arrays 
that allow them to work as ranges.


-Steve


cannot deduce template lambda from argument

2017-12-05 Thread aliak via Digitalmars-d-learn

Hi,

Having a little trouble understanding lambda type deduction. I 
have this lambda:


immutable lambda(T) = (T n) => n * n;

and if I call it with an explicit type it works else it errors 
with: lambda cannot deduce function from argument types !()(int)


auto x = lambda!int(2); // ok
auto x = lambda(2); // errors

But if it's a normal template function then calling it without 
explicit type is ok.


Thanks for any help!



Re: StopWatch problem

2017-12-05 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, December 05, 2017 14:25:12 Ali Çehreli via Digitalmars-d-learn 
wrote:
> Selective imports complicates matters. Changing the imports lets it
> compile with 2.076:
>
> void main() {
>  import std.datetime;
>  import std.stdio: writeln;
>
>  StopWatch sw;
>  writeln(sw.peek.msecs);
> }

Yes, and then you'll get deprecation warnings with 2.077. The cleanest way
to deal with replacing the TickDuration versions of the benchmarking stuff
with the MonoTime/Duration versions was to put the new ones in a module that
isn't imported by std/datetime/package.d and leave the old functions in
package.d (the whole reason that the old benchmarking stuff wasn't replaced
sooner was because it required splitting std.datetime first to do it in even
a semi-clean manner). Once they've gone through the full deprecation process
and are gone, then std.datetime.stopwatch can be publicly imported in
package.d, and the import mess will be clean again. But fortunately, if you
do

import std.datetime.stopwatch : StopWatch;
import std.datetime;

everything works just fine, because the module system then prefers the one
that's selectively imported. So, it's much less of an import mess than it
would be otherwise. I was surprised at how cleanly D's module system deals
with the potential conflict.

- Jonathan M Davis




Re: StopWatch problem

2017-12-05 Thread Ali Çehreli via Digitalmars-d-learn

On 12/05/2017 01:45 PM, Jonathan M Davis wrote:
> On Tuesday, December 05, 2017 21:33:53 Joel via Digitalmars-d-learn 
wrote:

>> void main() {
>>import std.datetime: Duration, msecs;
>>import std.datetime.stopwatch: StopWatch;
>>
>>StopWatch sw;
>>if (sw.peek.msecs) {
>>
>>}
>> }
>>
>> I get this error with the code:
>> z.d(6): Error: function core.time.dur!"msecs".dur (long length)
>> is not callable using argument types (Duration)
>
> core.time.msecs is an alias for core.time.dur!"msecs". It takes a 
long for
> the number of milliseconds and returns a Duration. If you want to 
convert a

> Duration to milliseconds, then use its member function, total. e.g.
> sw.peek.total!"msecs". It will convert the hnsecs in the duration to 
msecs

> using integral math and return a long.
>
> If what you want is a floating point value like TickDuration.msecs does,
> then you'll have to do the math yourself with something like
> sw.peek.total!"hnsecs" / cast(real)convert!("seconds", "hnsecs")(1).
>
> https://dlang.org/phobos/core_time.html#.msecs
> https://dlang.org/phobos/core_time.html#.Duration.total
>
> - Jonathan M Davis
>

Selective imports complicates matters. Changing the imports lets it 
compile with 2.076:


void main() {
import std.datetime;
import std.stdio: writeln;

StopWatch sw;
writeln(sw.peek.msecs);
}

Ali



Re: StopWatch problem

2017-12-05 Thread Joel via Digitalmars-d-learn
On Tuesday, 5 December 2017 at 21:45:20 UTC, Jonathan M Davis 
wrote:
On Tuesday, December 05, 2017 21:33:53 Joel via 
Digitalmars-d-learn wrote:

[...]


core.time.msecs is an alias for core.time.dur!"msecs". It takes 
a long for the number of milliseconds and returns a Duration. 
If you want to convert a Duration to milliseconds, then use its 
member function, total. e.g. sw.peek.total!"msecs". It will 
convert the hnsecs in the duration to msecs using integral math 
and return a long.


If what you want is a floating point value like 
TickDuration.msecs does,

then you'll have to do the math yourself with something like
sw.peek.total!"hnsecs" / cast(real)convert!("seconds", 
"hnsecs")(1).


https://dlang.org/phobos/core_time.html#.msecs 
https://dlang.org/phobos/core_time.html#.Duration.total


- Jonathan M Davis


Got it, thanks.


Re: StopWatch problem

2017-12-05 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, December 05, 2017 21:33:53 Joel via Digitalmars-d-learn wrote:
> void main() {
>   import std.datetime: Duration, msecs;
>   import std.datetime.stopwatch: StopWatch;
>
>   StopWatch sw;
>   if (sw.peek.msecs) {
>
>   }
> }
>
> I get this error with the code:
> z.d(6): Error: function core.time.dur!"msecs".dur (long length)
> is not callable using argument types (Duration)

core.time.msecs is an alias for core.time.dur!"msecs". It takes a long for
the number of milliseconds and returns a Duration. If you want to convert a
Duration to milliseconds, then use its member function, total. e.g.
sw.peek.total!"msecs". It will convert the hnsecs in the duration to msecs
using integral math and return a long.

If what you want is a floating point value like TickDuration.msecs does,
then you'll have to do the math yourself with something like
sw.peek.total!"hnsecs" / cast(real)convert!("seconds", "hnsecs")(1).

https://dlang.org/phobos/core_time.html#.msecs
https://dlang.org/phobos/core_time.html#.Duration.total

- Jonathan M Davis



StopWatch problem

2017-12-05 Thread Joel via Digitalmars-d-learn

void main() {
import std.datetime: Duration, msecs;
import std.datetime.stopwatch: StopWatch;

StopWatch sw;
if (sw.peek.msecs) {

}
}

I get this error with the code:
z.d(6): Error: function core.time.dur!"msecs".dur (long length) 
is not callable using argument types (Duration)


Re: lower case only first letter of word

2017-12-05 Thread Ali Çehreli via Digitalmars-d-learn

On 12/05/2017 11:41 AM, kdevel wrote:

On Tuesday, 5 December 2017 at 17:25:57 UTC, Steven Schveighoffer wrote:


But one cannot use the return value of lowerCaseFirst as argument for 
foo(string). Only the use as argument to writeln seems to work.


That's how ranges work. LowerCaseFirst produces dchar elements one at a 
time. An easy way of getting a string out of it is calling 
std.conv.text, which converts all those dchars to a series of UTF-8 chars.


Note, .text below is an expensive call because it allocates a new 
string. You may want to cache its result first if you need the result 
more than once:


auto lowered = lowerCaseFirst("HELLO").text;
foo(lowered);

This works:

import std.range;

struct LowerCaseFirst(R) // if(isSomeString!R)
{
   R src;
   bool notFirst; // terrible name, but I want default false
   dchar front() {
  import std.uni: toLower;
  return notFirst ? src.front : src.front.toLower;
   }
   void popFront() { notFirst = true; src.popFront; }
   bool empty() { return src.empty; }
}

auto lowerCaseFirst(R)(R r)
{
   return LowerCaseFirst!R(r);
}

void foo(string s) {
import std.stdio;
writefln("good old string: %s", s);
}

void main() {
import std.conv;
foo(lowerCaseFirst("HELLO").text);
}

Ali


Re: lower case only first letter of word

2017-12-05 Thread kdevel via Digitalmars-d-learn
On Tuesday, 5 December 2017 at 17:25:57 UTC, Steven Schveighoffer 
wrote:

[...]

struct LowerCaseFirst(R) // if(isSomeString!R)
{
   R src;
   bool notFirst; // terrible name, but I want default false
   dchar front() {
  import std.uni: toLower;
  return notFirst ? src.front : src.front.toLower;
   }
   void popFront() { notFirst = true; src.popFront; }
   bool empty() { return src.empty; }
}

auto lowerCaseFirst(R)(R r)
{
   return LowerCaseFirst!R(r);
}

Warning: it ain't going to be fast. Auto-decoding everywhere.


But one cannot use the return value of lowerCaseFirst as argument 
for foo(string). Only the use as argument to writeln seems to 
work. Also I had to put


import std.range.primitives : front, popFront, empty;

outside the struct otherwise the compiler complains about missing 
front, popFront and empty for type string.


Re: lower case only first letter of word

2017-12-05 Thread Ali Çehreli via Digitalmars-d-learn

On 12/05/2017 09:25 AM, Steven Schveighoffer wrote:


Non-allocating version:

struct LowerCaseFirst(R) // if(isSomeString!R)
{
    R src;
    bool notFirst; // terrible name, but I want default false
    dchar front() {
   import std.uni: toLower;
   return notFirst ? src.front : src.front.toLower;
    }
    void popFront() { notFirst = true; src.popFront; }
    bool empty() { return src.empty; }
}

auto lowerCaseFirst(R)(R r)
{
    return LowerCaseFirst!R(r);
}

Warning: it ain't going to be fast. Auto-decoding everywhere.

-Steve


One using existing facilities:

import std.range;
import std.uni;
import std.algorithm;

auto lowerCaseFirst(R)(R r) {
R rest = r.save;
rest.popFront();
return chain(r.front.toLower.only, rest);
}

unittest {
assert(lowerCaseFirst("SchveiGoffer").equal("schveiGoffer"));
assert(lowerCaseFirst("ŞchveıĞöffer").equal("şchveıĞöffer"));
}

void main() {
}

Ali


Re: Embedded interfaces with generic members

2017-12-05 Thread A Guy With a Question via Digitalmars-d-learn

On Tuesday, 5 December 2017 at 19:27:37 UTC, Ali Çehreli wrote:

On 12/05/2017 11:07 AM, A Guy With a Question wrote:
> The following doesn't appear to be valid syntax. Array!Item!T

You can ommit the template argument list parenteses only for 
single symbols.


Starting with the full syntax:

  Array!(Item!(T))

Since Item!(T) uses a single symbol, T, you can remove the 
parenteses from that one:


  Array!(Item!T)

The following compiles:

import std.container.array;

struct Item(T) {
}

void main() {
alias T = int;
auto a = Array!(Item!T)();
}

Ali


yup that was the issue.


Re: Embedded interfaces with generic members

2017-12-05 Thread Ali Çehreli via Digitalmars-d-learn

On 12/05/2017 11:07 AM, A Guy With a Question wrote:
> The following doesn't appear to be valid syntax. Array!Item!T

You can ommit the template argument list parenteses only for single symbols.

Starting with the full syntax:

  Array!(Item!(T))

Since Item!(T) uses a single symbol, T, you can remove the parenteses 
from that one:


  Array!(Item!T)

The following compiles:

import std.container.array;

struct Item(T) {
}

void main() {
alias T = int;
auto a = Array!(Item!T)();
}

Ali



Embedded interfaces with generic members

2017-12-05 Thread A Guy With a Question via Digitalmars-d-learn

The following doesn't appear to be valid syntax. Array!Item!T

I get the following error:

"multiple ! arguments are not allowed"

Which is ok...I get THAT error, however, this does not work 
either:


alias Items(T) = Array!Item(T);

This gives me the error:

Error: function declaration without return type. (Note that 
constructors are always named `this`)	


Item is defined as follows:

interface Item(T)
{
   @property
   T member();
}

That part compiles fine. However, even when I remove the 
aliasing, I can't import this interface. I get "Error: undefined 
identifier 'Item'"


I'm not quite sure I understand how to create a generic container 
interface or class in D. Half of how I expect it to work works, 
but the other half doesn't. The docs aren't too helpful. I'm not 
sure if it's a case where there's just too much to go through or 
if what I'm trying to do isn't really covered. Essentially I'm 
trying to create an array of this type 'Item' that has some 
generic members. I think people who come from C# will kind of get 
what I'm trying to do here, because I'm trying to port C# code.


Re: git workflow for D

2017-12-05 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Dec 04, 2017 at 12:02:37PM -0800, Ali Çehreli via Digitalmars-d-learn 
wrote:
[...]
> Paraphrasing someone I trust very much, "Never 'pull', always 'fetch
> -p' and then rebase."

I always use `git pull --ff-only`.  Lets me pull when it's "safe",
aborts if it will end up in a mess (i.e., tell me when I've made a
boo-boo and committed to master).  Usually, I only make changes in a
local branch, so it's just a matter of rebasing the local branch
afterwards.


T

-- 
Right now I'm having amnesia and deja vu at the same time. I think I've 
forgotten this before.


Re: git workflow for D

2017-12-05 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Dec 04, 2017 at 06:51:42AM -0500, Nick Sabalausky (Abscissa) via 
Digitalmars-d-learn wrote:
> On 12/03/2017 03:05 PM, bitwise wrote:
> > I've finally started learning git, due to our team expanding beyond
> > one person - awesome, right?
> 
> PROTIP: Version control systems (no matter whether you use git,
> subversion, or whatever), are VERY helpful on single-person projects,
> too! Highly recommended! (Or even any time you have a directory tree
> where you might want to enable undo/redo/magic-time-machine on!)

+100!  (and by '!' I mean 'factorial'. :-P)

I've been using version control for all my personal projects, and I
cannot tell you how many times it has saved me from my own stupidity
(i.e., have to rollback a whole bunch of changes, or just plain ole
consult an older version of the code that I've forgotten). Esp. with
git, it also lets me play with experimental code changes without ever
worrying that if things don't work out I might have to revert everything
by hand (not fun! and very error-prone).

In fact, I use version control for more than just code: *anything*
that's text-based is highly recommended to be put under version control
if you're doing any serious amount of editing with it, because it's just
such a life-saver. Of course, git works with binaries too, but diffing
and such become a lot easier if everything is text-based.  This is why I
always prefer text-based file formats when it comes to authoring.

Websites are a good example that really ought to be under version
control.  Git, especially, lets you clone the website to a testing
server where you can experiment with changes without fear, and once
you're happy with the changes, commit and push to the "real" web server.
Notice an embarrassing mistake that isn't easy to fix? No problem, just
git checkout HEAD^, and that buys you the time you need to fix the
problem locally, then re-push.

I've also recently started putting certain subdirectories under /etc in
git.  Another life-saver when you screw up a configuration accidentally
and need to revert to the last-known good config. Also good for
troubleshooting to see exactly what changes were made that led to the
current state of things.

tl;dr: use version control WHEREVER you can, even for personal 1-man
projects, not only for code, but for *everything* that involves a lot of
changes over time.


> > Anyways, I've got things more or less figured out, which is nice,
> > because being clueless about git is a big blocker for me trying to
> > do any real work on dmd/phobos/druntime. As far as working on a
> > single master branch works, I can commit, rebase, merge, squash,
> > push, reset, etc, like the best of em.
> 
> Congrats! Like Arun mentioned, git's CLI can be a royal mess. I've
> heard it be compared to driving a car by crawling under the hood and
> pulling on wires - and I agree.
> 
> But it's VERY helpful stuff to know, and the closer you get to
> understanding it inside and out, the better off you are. (And I admit,
> I still have a long ways to go myself.)

Here's the thing: in order to use git effectively, you have to forget
all the traditional notions of version control. Yes, git does use many
of the common VC terminology, and, on the surface, does work in similar
ways.

BUT.

You will never be able to avoid problems and unexpected behaviours
unless you forget all the traditional VC notions, and begin to think in
terms of GRAPHS. Because that's what git is: a system for managing a
graph. To be precise, a directed acyclic graph (DAG).

Roughly speaking, a git repo is just a graph (a DAG) of commits,
objects, and refs.  Objects are the stuff you're tracking, like files
and stuff.  Commits are sets of files (objects) that are considered to
be part of a changeset. Refs are just pointers to certain nodes in the
graph.

A git 'branch' is nothing but a pointer to some node in the DAG. In git,
a 'branch' in the traditional sense is not a first-class entity; what
git calls a "branch" is nothing but a node pointer. The traditional
"branch" is merely a particular configuration of nodes in the DAG that
has no special significance to git.

Git maintains a notion of the 'current branch', i.e., which pointer will
serve as the location where new nodes will be added to the DAG. By
default, this is the 'master' branch (i.e., a pointer named 'master'
pointing to some node in the DAG).

When you run `git commit`, what you're doing is creating a new node in
the DAG, with the parent pointer set to the current branch pointer. So
if the current branch is 'master', and it's pointing to the node with
SHA hash 012345, then `git commit` will create a new node with its
parent pointer set to 012345.  After this node is added to the graph,
the current pointer, 'master', is updated to point to the new node.

By performing a series of `git commit`s, what you end up with is a
linear chain of nodes, with the current branch ('master') pointing to
the last node.  This, we traditionally view as a 

Re: lower case only first letter of word

2017-12-05 Thread Steven Schveighoffer via Digitalmars-d-learn

On 12/5/17 10:00 AM, Mengu wrote:

On Tuesday, 5 December 2017 at 14:34:57 UTC, Mengu wrote:

On Tuesday, 5 December 2017 at 14:01:35 UTC, Marc wrote:

On Tuesday, 5 December 2017 at 13:40:08 UTC, Daniel Kozak wrote:

[...]


Yes, this is not what I want. I want to convert only the first letter 
of the word to lower case and left all the others immutable. similar 
to PHP's lcfirst():


http://php.net/manual/en/function.lcfirst.php


this is how i'd do it:

string upcaseFirst(string wut) {
  import std.ascii : toUpper;
  import std.array : appender;

  auto s = appender!string;
  s ~= wut[0].toUpper;
  s ~= wut[1..$];
  return s.data;
}


however a solution that does not allocate any memory would be a lot better.


Non-allocating version:

struct LowerCaseFirst(R) // if(isSomeString!R)
{
   R src;
   bool notFirst; // terrible name, but I want default false
   dchar front() {
  import std.uni: toLower;
  return notFirst ? src.front : src.front.toLower;
   }
   void popFront() { notFirst = true; src.popFront; }
   bool empty() { return src.empty; }
}

auto lowerCaseFirst(R)(R r)
{
   return LowerCaseFirst!R(r);
}

Warning: it ain't going to be fast. Auto-decoding everywhere.

-Steve


Directory Size

2017-12-05 Thread Vino via Digitalmars-d-learn

Hi All,

 Is there any better ways to get the size of folders , The below 
code perfectly works , but i need return type as 
Array!(Tuple!(string, string)) rather then using the 
"Result.insertBack(d); 
Result.insertBack(to!string(SdFiles[].sum))" as per the below 
example.


E.g:
Array!(Tuple!(string, string)) Result;
Result = (d, to!string(SdFiles[].sum));

Program:
import std.algorithm: filter, map, sum, uniq;
import std.container.array;
import std.file: dirEntries, SpanMode, isDir, isFile;
import std.stdio: writeln;
import std.typecons: tuple, Tuple;
import std.conv: to;
/**/
/* Sub Function : Size of Dir List*/
/**/
auto mSize () {
string FFs = "C:\\Temp\\BACKUP";
Array!string Result;
	auto dFiles = Array!string ((dirEntries(FFs, 
SpanMode.shallow).filter!(a => a.isDir)).map!(a => a.name));

foreach (d; dFiles[])   {
		auto SdFiles = Array!ulong((dirEntries(d, 
SpanMode.depth).filter!(a => a.isFile)).map!(a => a.size));
		if (SdFiles[].sum / 1024 / 1024  > 30) { Result.insertBack(d); 
Result.insertBack(to!string(SdFiles[].sum)); }

}
return Result;
}

void main() {
writeln(mSize[]);
}

From,
Vino.B


Re: git workflow for D

2017-12-05 Thread John Gabriele via Digitalmars-d-learn

On Sunday, 3 December 2017 at 20:05:47 UTC, bitwise wrote:
{snip} If anyone can offer any kind of advice, or an article 
that explains these things concisely and effectively, that 
would be helpful.


I found some git-specific info in this wiki page:




Re: Object oriented programming and interfaces

2017-12-05 Thread Steven Schveighoffer via Digitalmars-d-learn

On 12/4/17 3:43 PM, Dirk wrote:

Hi!

I defined an interface:

interface Medoid {
     float distance( Medoid other );
     uint id() const @property;
}

and a class implementing that interface:

class Item : Medoid {
     float distance( Item i ) {...}
     uint id() const @property {...}
}

The compiler says:
Error: class Item interface function 'float distance(Medoid other)' is 
not implemented


Is there a way to implement the Item.distance() member function taking 
any object whose class is Item?


You are thinking about covariance and contravariance.

In D, only the return value can work with covariance:

interface Medoid {
   Medoid getIt();
}

class Item : Medoid {
   Item getIt() { return this; }
}

D does not support contravariance on parameters. And in any case you 
have it backwards. A derived override could take a base type, but not a 
more specific type, for the reasons Adam outlined.


-Steve


Re: Passing Function as an argument to another Function

2017-12-05 Thread Vino via Digitalmars-d-learn

On Monday, 4 December 2017 at 19:25:15 UTC, Ali Çehreli wrote:

On 12/04/2017 04:52 AM, Vino wrote:

> [...]

Every expression has a type. 'auto' in that context (or 
'const', etc.) just helps with not spelling-out that type. You 
can see the type with pragma(msg) and typeof:


[...]


Hi Ali,

 Thank you very much, was able to resolve the issue using the 
template method.


From,
Vino.B


Re: git workflow for D

2017-12-05 Thread Steven Schveighoffer via Digitalmars-d-learn

On 12/4/17 3:14 PM, Ali Çehreli wrote:

Dear git experts, given 3 repos, now what are the steps? Is the 
following correct? What are the exact commands?


Disclaimer: I'm not a git expert.


- Only once, create the original repo as an upstream of your local repo.


The wording is off (s/repo/remote), but yes. This one I always have to 
look up, because I don't remember the order of the URL vs. the name, and 
git doesn't care if you swap them. But the command is this:


git remote add upstream 

Where url is the https version of dlang's repository (IMPORTANT: for 
dlang members, do NOT use the ssh version, as then you can accidentally 
push to it without any confirmation).




- For each change:

1) Fetch from upstream


git fetch upstream

will fetch EVERYTHING, all branches. But just for reference so you can 
use it without affecting your local repo.




2) Rebase origin/master (on upstream, right?)


No, don't do this every time! If you never commit to your local master 
(which you shouldn't), you do it this way:


# This checks out your local master branch
git checkout master

# This moves your local master branch along to match that of master.
# The --ff-only is to ensure it only works if the merge is a fast-forward.
# A fast forward merge happens when your master is on the same commit
# history as the upstream master.
git merge --ff-only upstream/master

Optionally you can push your local master to origin, but it's not 
strictly necessary:


git push origin master



3) Make changes


Step 2.1: checkout a local branch. You can even do this after you have 
changed some files but *before* you have committed them (IMO one of the 
best features of git as compared to, say, subversion).


# creates a new branch called mylocalfix based on local master, and 
checks it out.

git checkout -b mylocalfix



4) Commit (potentially after 'add')


Protip: git commit -a automatically adds any modified files that are 
currently in the repo, but have been changed.




5) Repeat steps 3 and 4 as needed


Correct!



6) 'git push -force' so that your GitHub repo is up-to-date right? 
(There, I mentioned "force". :) )


I'd say:

git push origin mylocalfix

This pushes *only* your mylocalfix branch to *your* fork of the repo.

No need to force as long as you do not want to squash. Squashing is when 
you merge multiple commits into one commit so that the history looks 
cleaner. I'd recommend never using force (btw, it's --force with 2 
dashes) unless it complains. And then, make sure you aren't doing 
something foolish before using the --force command! Because you are 
committing only to your fork, and to your branch, even if you mess up 
here, it's pretty easy to recover.


A couple of examples:

1. You commit, but missed a space between "if" and "(". Instead of 
generating a commit and log for the typo, you just squash the new commit 
into the first one.
2. You commit a work in progress, but then change the design. The first 
commit is useless in the history, as it probably doesn't even apply 
anymore, so you squash them together, as if you only ever committed the 
correct version.


To squash the last few commits, I recommend using rebase -i:

# Replace 3 with the number of the last few commits you want to work with.
# IMPORTANT: this must contain the commit you want to squash into!
git rebase -i HEAD~3

This will pop up an editor. Follow the instructions listed there! If you 
want to squash 2 commits together, use "fixup", or even just "f". Note: 
do NOT "fixup" your first commit, as this will try to squash into 
someone else's commit that happened before you changed anything!


Once you write the file and exit, git will rebase using your directions.

At this point you need to use --force to push (as long as you have 
already pushed before), as your commit history now differs from github's.




7) Go to GitHub and press the big button to create a pull request


Correct! After you do this, you can continue to run steps 3, 4, 6 to 
update your PR.


One further step that I like to do to keep my repo clean:

8) When your PR is pulled:

git fetch upstream
git checkout master
git merge --ff-only upstream/master
git branch -d mylocalfix

This pulls the new changes that were successfully merged into dlang's 
master into your master. Then it deletes the mylocalfix branch (no 
longer needed). The lower case -d means to only delete if the changes 
have been merged (git will complain if they aren't in the history). This 
is a nice way to clean your local branches up, and verify there isn't 
anything amiss.


Note also, if you want to work on several fixes at once, you can 
checkout more than one local branch, and switch between them. Just 
remember to commit before you checkout the different branches (git will 
complain if you have uncommitted files, but not files that haven't ever 
been added).


Hope this all helps!

-Steve


Re: lower case only first letter of word

2017-12-05 Thread Jacob Carlborg via Digitalmars-d-learn

On 2017-12-05 15:34, Mengu wrote:


this is how i'd do it:

string upcaseFirst(string wut) {
   import std.ascii : toUpper;
   import std.array : appender;

   auto s = appender!string;
   s ~= wut[0].toUpper;
   s ~= wut[1..$];
   return s.data;
}


That's not Unicode aware and is only safe to do with single byte characters.

--
/Jacob Carlborg


Re: git workflow for D

2017-12-05 Thread Jesse Phillips via Digitalmars-d-learn
I'm going to answer with something that others may not agree 
with, maybe they can enlighten me, but let me first get a generic 
principle of git and answer some questions.


Git has 2 types of branches, local branches (you know them as 
just branches) and remotes (which have their own local branches). 
I say this to remove the confusion with having an original 
repository, a forked repository, and a cloned repository. When it 
comes to interactions with these repositories the only difference 
from your local branches is that you can't interact directly with 
them (i.e. you can't commit to them) and the interactions require 
specifying the remote location of the branch.


Some of your other questions are about GitHub and Forking. Git 
doesn't know what a fork is, but GitHub ties a pull request to a 
branch. This means you can update/change your pull request by 
updating/changing your branch. From that you should realize each 
pull request needs its own branch.


Back to git remotes. I'm sure you're aware of the commonly named 
"origin" remote and possible the second common "upstream." When 
you're dealing with many remotes your local branches can get 
complicated. For example many people utilize 'master' as a 
*tracking* branch to "origin" well, "upstream" if there is one. 
I'm to the point that in this situation my recommendation is just 
delete your 'master' branch both local and "origin." Don't worry 
you can bring it back if you need it, you won't need it.


Here is the thing, you already have two, maybe 3 master branches 
'master' 'origin/master' 'upstream/master' these are local 
branches (they are special in that you can't commit to them). And 
these are also your true tracking branches, whenever you 
fetch/pull from your remote these branches are updated, they will 
always reflect the branch of the remote and they will never 
conflict during updates. You can always create your own master $ 
git branch master upstream/master


I want to note that 'origin/master' is different from such 
commands as `$ git push origin master` because in the first you 
are referring to a local branch and the second you reference your 
remote followed by your remotes local branch (actually I could be 
wrong here because the full syntax is `$ git push origin 
master:master` where the left: is your local and :right is the 
remote local branch [and note that `push origin :master` would 
delete the remote master branch because you're push no branch to 
it.).


I hope that this along with answers other have given will help 
you to answer all of your questions.


Re: Object oriented programming and interfaces

2017-12-05 Thread Jesse Phillips via Digitalmars-d-learn

On Monday, 4 December 2017 at 20:43:27 UTC, Dirk wrote:

Hi!

I defined an interface:

interface Medoid {
float distance( Medoid other );
uint id() const @property;
}

and a class implementing that interface:

class Item : Medoid {
float distance( Item i ) {...}
uint id() const @property {...}
}

The compiler says:
Error: class Item interface function 'float distance(Medoid 
other)' is not implemented


Is there a way to implement the Item.distance() member function 
taking any object whose class is Item?


I think everyone here has missed the reason.

The problem is that a derived type can do more, the interface 
only allows for two method calls while the class could 
theoretically do more.


If the compiler allowed the class it would let you access 
functions and members not available to the interfaces, and the 
function would still be passed a Medoid.


You could make a cast within your function, but then you're still 
really not handling the interface but that at least would be 
clear in the code rather than the compiler hiding it by ignoring 
the problem.


Re: lower case only first letter of word

2017-12-05 Thread Mengu via Digitalmars-d-learn

On Tuesday, 5 December 2017 at 14:34:57 UTC, Mengu wrote:

On Tuesday, 5 December 2017 at 14:01:35 UTC, Marc wrote:
On Tuesday, 5 December 2017 at 13:40:08 UTC, Daniel Kozak 
wrote:

[...]


Yes, this is not what I want. I want to convert only the first 
letter of the word to lower case and left all the others 
immutable. similar to PHP's lcfirst():


http://php.net/manual/en/function.lcfirst.php


this is how i'd do it:

string upcaseFirst(string wut) {
  import std.ascii : toUpper;
  import std.array : appender;

  auto s = appender!string;
  s ~= wut[0].toUpper;
  s ~= wut[1..$];
  return s.data;
}


however a solution that does not allocate any memory would be a 
lot better.


Re: lower case only first letter of word

2017-12-05 Thread Mengu via Digitalmars-d-learn

On Tuesday, 5 December 2017 at 14:01:35 UTC, Marc wrote:

On Tuesday, 5 December 2017 at 13:40:08 UTC, Daniel Kozak wrote:
but this will change all other uppercase to lowercase, so 
maybe it is not what you want. If you really want just change 
first char to upper, then there is nothing wrong to do it 
yourself


On Tue, Dec 5, 2017 at 2:37 PM, Daniel Kozak 
 wrote:


Something like this: 
https://dlang.org/phobos/std_uni.html#asCapitalized


On Tue, Dec 5, 2017 at 2:31 PM, Marc via Digitalmars-d-learn 
< digitalmars-d-learn@puremagic.com> wrote:


Does D have a native function to capitalize only the first 
letter of the word? (I'm asking that so I might avoid 
reinvent the wheel, which I did sometimes in D)


Yes, this is not what I want. I want to convert only the first 
letter of the word to lower case and left all the others 
immutable. similar to PHP's lcfirst():


http://php.net/manual/en/function.lcfirst.php


this is how i'd do it:

string upcaseFirst(string wut) {
  import std.ascii : toUpper;
  import std.array : appender;

  auto s = appender!string;
  s ~= wut[0].toUpper;
  s ~= wut[1..$];
  return s.data;
}


Re: lower case only first letter of word

2017-12-05 Thread Marc via Digitalmars-d-learn

On Tuesday, 5 December 2017 at 13:40:08 UTC, Daniel Kozak wrote:
but this will change all other uppercase to lowercase, so maybe 
it is not what you want. If you really want just change first 
char to upper, then there is nothing wrong to do it yourself


On Tue, Dec 5, 2017 at 2:37 PM, Daniel Kozak 
 wrote:


Something like this: 
https://dlang.org/phobos/std_uni.html#asCapitalized


On Tue, Dec 5, 2017 at 2:31 PM, Marc via Digitalmars-d-learn < 
digitalmars-d-learn@puremagic.com> wrote:


Does D have a native function to capitalize only the first 
letter of the word? (I'm asking that so I might avoid 
reinvent the wheel, which I did sometimes in D)


Yes, this is not what I want. I want to convert only the first 
letter of the word to lower case and left all the others 
immutable. similar to PHP's lcfirst():


http://php.net/manual/en/function.lcfirst.php


No line numbers in stack trace (again)

2017-12-05 Thread Nordlöw via Digitalmars-d-learn
If I get the following stack trace ___without line numbers___ 
(instead ??:?) what's missing?


core.exception.AssertError@src/knet/linking.d(444): Assertion 
failure


??:? _d_assertp [0x5092ab19]
??:? pure @safe knet.storage.Edge 
knet.linking.dlink!(knet.storage.ZingOf!(knet.storage.NodeInst).ZingOf[], knet.storage.ZingOf!(knet.storage.NodeInst).ZingOf[]).dlink(knet.storage.ZingOf!(knet.storage.NodeInst).ZingOf[], knet.rel.Rel, knet.storage.ZingOf!(knet.storage.NodeInst).ZingOf[], knet.origins.Origin, double, bool) [0x5081b323]
??:? pure @safe knet.storage.Edge 
knet.linking.dlink(knet.storage.ZingOf!(knet.storage.NodeInst).ZingOf, knet.rel.Rel, knet.storage.ZingOf!(knet.storage.NodeInst).ZingOf, knet.origins.Origin, double, bool) [0x50819ec6]
??:? pure @safe 
knet.storage.ZingOf!(knet.storage.NodeInst).ZingOf 
knet.patterns.oneOrMore(knet.storage.ZingOf!(knet.storage.NodeInst).ZingOf) [0x508400aa]
??:? pure @safe 
knet.storage.ZingOf!(knet.storage.NodeInst).ZingOf 
knet.patterns.nounPhrase(knet.base.Db, languages.Lang) 
[0x508401d4]
??:? pure @safe void 
knet.patterns.__unittest_src_knet_patterns_d_286_3() [0x50841afb]

??:? void knet.patterns.__modtest() [0x50841d1f]
??:? int 
core.runtime.runModuleUnitTests().__foreachbody2(object.ModuleInfo*) [0x5094d98c]
??:? int object.ModuleInfo.opApply(scope int 
delegate(object.ModuleInfo*)).__lambda2(immutable(object.ModuleInfo*)) [0x509229d2]
??:? int rt.minfo.moduleinfos_apply(scope int 
delegate(immutable(object.ModuleInfo*))).__foreachbody2(ref 
rt.sections_elf_shared.DSO) [0x50933001]
??:? int rt.sections_elf_shared.DSO.opApply(scope int 
delegate(ref rt.sections_elf_shared.DSO)) [0x50933090]
??:? int rt.minfo.moduleinfos_apply(scope int 
delegate(immutable(object.ModuleInfo*))) [0x50932f8d]
??:? int object.ModuleInfo.opApply(scope int 
delegate(object.ModuleInfo*)) [0x509229a9]

??:? runModuleUnitTests [0x5094d870]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).runAll() [0x5092e29a]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate()) [0x5092e233]

??:? _d_run_main [0x5092e1a3]
??:? main [0x506833e9]
??:? __libc_start_main [0xb9a7b1c0]


I'm compiling with flags

-vcolumns -dip25 -vcolumns -dip25 -vcolumns -dip25 -c 
-of.dub/build/application-unittest-linux.posix-x86_64-dmd_2077-CB3BD7AAC3F0D7B6FD8DE72BF286ACE5/knetquery.o -debug -g -unittest -w


and using DMD 2.077.1


Re: lower case only first letter of word

2017-12-05 Thread Daniel Kozak via Digitalmars-d-learn
but this will change all other uppercase to lowercase, so maybe it is not
what you want. If you really want just change first char to upper, then
there is nothing wrong to do it yourself

On Tue, Dec 5, 2017 at 2:37 PM, Daniel Kozak  wrote:

> Something like this: https://dlang.org/phobos/std_uni.html#asCapitalized
>
> On Tue, Dec 5, 2017 at 2:31 PM, Marc via Digitalmars-d-learn <
> digitalmars-d-learn@puremagic.com> wrote:
>
>> Does D have a native function to capitalize only the first letter of the
>> word? (I'm asking that so I might avoid reinvent the wheel, which I did
>> sometimes in D)
>>
>
>


Re: lower case only first letter of word

2017-12-05 Thread ketmar via Digitalmars-d-learn

Marc wrote:

Does D have a native function to capitalize only the first letter of the 
word? (I'm asking that so I might avoid reinvent the wheel, which I did 
sometimes in D)


http://dpldocs.info/experimental-docs/std.string.capitalize.html
http://dpldocs.info/experimental-docs/std.uni.asCapitalized.html

searching rox!

p.s.: but beware, it will lowercase all the letters except the first one, 
so it may not be exactly the thing you aksed for.


Re: lower case only first letter of word

2017-12-05 Thread Daniel Kozak via Digitalmars-d-learn
Something like this: https://dlang.org/phobos/std_uni.html#asCapitalized

On Tue, Dec 5, 2017 at 2:31 PM, Marc via Digitalmars-d-learn <
digitalmars-d-learn@puremagic.com> wrote:

> Does D have a native function to capitalize only the first letter of the
> word? (I'm asking that so I might avoid reinvent the wheel, which I did
> sometimes in D)
>


lower case only first letter of word

2017-12-05 Thread Marc via Digitalmars-d-learn
Does D have a native function to capitalize only the first letter 
of the word? (I'm asking that so I might avoid reinvent the 
wheel, which I did sometimes in D)


Re: Python and D (PyD)

2017-12-05 Thread Fat_Umpalumpa via Digitalmars-d-learn
Omg this was working a few days ago just fine! Now I can't even 
start the histogram example! I haven't changed the code and I 
know this worked a week ago.


dub still builds the program, however now when running the 
program, I get an error


object.Exception@../../../.dub/packages/pyd-0.9.9/pyd/infrastructure/pyd/extra.d(74):
 numpy is not available

and then many more errors as if I have a problem with python or 
numpy... Crap I was installing random things with pip and may 
have ruined something.


I'll post a few more errors but I think the problem is on line 
(74) of the extra.d script


4   matplot_d   0x00010c2f5262 pure 
@safe void 
std.exception.bailOut!(Exception).bailOut(immutable(char)[], 
ulong, const(char[])) + 126
5   matplot_d   0x00010c2f51d6 pure 
@safe deimos.python.object.PyTypeObject* 
std.exception.enforce!(Exception, 
deimos.python.object.PyTypeObject*).enforce(deimos.python.object.PyTypeObject*, lazy const(char)[], immutable(char)[], ulong) + 106
6   matplot_d   0x00010c31065a 
deimos.python.object.PyObject* 
pyd.extra.d_to_python_numpy_ndarray!(long[]).d_to_python_numpy_ndarray(long[]) + 70
7   matplot_d   0x00010c2e8429 _Dmain 
+ 73


Re: Object oriented programming and interfaces

2017-12-05 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 5 December 2017 at 07:47:32 UTC, Dirk wrote:
The distance function is implementation dependend and can only 
be computed between two objects of the same class (in this 
example the class is Item).


Just don't put it in the interface. Leave it in the individual 
classes with the stricter definition.


Or, as I said before, implement the interface by asking the 
question: what if you call Item.distance (NotAnItem)? Maybe you 
could throw an exception or return NaN to show this is an invalid 
comparison. But if it is in the interface, you MUST answer that 
question somehow.


---
interface Medoid {
float distance(Medoid other);
}

class Item {
float distance(Medoid other) {
   if(auto item = cast(Item) other) {
// it is an item, implement comparison here
   } else {
return float.nan; // invalid comparison
   }
}
}
---


Tip: you can generically test that two objects are of the same 
class through their interfaces by checking:


if(typeid(cast(Object) o1) == typeid(cast(Object) o2)) {
   // same class
} else {
   // same interface, but different class
}

Might be useful for your classifier, though checking the nan of 
distance might be good too.


Re: Object oriented programming and interfaces

2017-12-05 Thread bauss via Digitalmars-d-learn

On Tuesday, 5 December 2017 at 08:08:55 UTC, Daniel Kozak wrote:

You can do something like this:

interface Medoid(T) {
float distance( T other );
uint id() const @property;
}

class Item : Medoid!(Item) {
float distance( Item m ) { return 0.;}
uint id() const @property { return 1; }
}

class MedoidClassification {
this(T:Medoid!T)(T[] list) {}
//Medoid[][] getClusters() {...}
}

void main() {
auto items = new Item[10];
auto mc = new MedoidClassification( items );
}

On Tue, Dec 5, 2017 at 8:47 AM, Dirk via Digitalmars-d-learn < 
digitalmars-d-learn@puremagic.com> wrote:


The distance function is implementation dependend and can only 
be computed between two objects of the same class (in this 
example the class is Item).


My goal is to write a module for a k-medoids clustering 
algorithm. The class MedoidClassification shall be able to 
partition a list of objects from the same class, which 
implement the Medoid interface.


My current approach is this (which does not work):

interface Medoid {
float distance( Medoid other );
uint id() const @property;
}

class Item : Medoid {
float distance( Item m ) {...}
uint id() const @property {...}
}

class MedoidClassification {
this( Medoid[] list ) {...}
Medoid[][] getClusters() {...}
}

void main() {
Item[10] items;
auto mc = MedoidClassification( items );
}


What would be a good way to implement this?


This still defeats the purpose of having multiple Medoid types, 
as each Medoid is still specified with a specific type.


Re: Object oriented programming and interfaces

2017-12-05 Thread Daniel Kozak via Digitalmars-d-learn
You can do something like this:

interface Medoid(T) {
float distance( T other );
uint id() const @property;
}

class Item : Medoid!(Item) {
float distance( Item m ) { return 0.;}
uint id() const @property { return 1; }
}

class MedoidClassification {
this(T:Medoid!T)(T[] list) {}
//Medoid[][] getClusters() {...}
}

void main() {
auto items = new Item[10];
auto mc = new MedoidClassification( items );
}

On Tue, Dec 5, 2017 at 8:47 AM, Dirk via Digitalmars-d-learn <
digitalmars-d-learn@puremagic.com> wrote:

> The distance function is implementation dependend and can only be computed
> between two objects of the same class (in this example the class is Item).
>
> My goal is to write a module for a k-medoids clustering algorithm. The
> class MedoidClassification shall be able to partition a list of objects
> from the same class, which implement the Medoid interface.
>
> My current approach is this (which does not work):
>
> interface Medoid {
> float distance( Medoid other );
> uint id() const @property;
> }
>
> class Item : Medoid {
> float distance( Item m ) {...}
> uint id() const @property {...}
> }
>
> class MedoidClassification {
> this( Medoid[] list ) {...}
> Medoid[][] getClusters() {...}
> }
>
> void main() {
> Item[10] items;
> auto mc = MedoidClassification( items );
> }
>
>
> What would be a good way to implement this?
>


Re: Object oriented programming and interfaces

2017-12-05 Thread Andrea Fontana via Digitalmars-d-learn

On Tuesday, 5 December 2017 at 07:47:32 UTC, Dirk wrote:

What would be a good way to implement this?


Did you tried to use introspection?