Re: Delegates and classes for custom code.

2018-04-16 Thread Nathan S. via Digitalmars-d-learn

On Tuesday, 17 April 2018 at 04:09:57 UTC, Chris Katko wrote:
I'm having trouble conceptualizing this issue at the moment. 
But it seems if I pass to the delegate my object, then I can 
ONLY use one class type.


Can you post the code you're trying to run?


Re: Delegates and classes for custom code.

2018-04-16 Thread Chris Katko via Digitalmars-d-learn
I'm having trouble conceptualizing this issue at the moment. But 
it seems if I pass to the delegate my object, then I can ONLY use 
one class type.


Say, the delegate takes a "this" from... some class that wants to 
have a dialog. A window. Now the delegate NEEDS a this from a 
window, and only a window. So if I have a delegate in... 
something else, I'll either need to figure out some way for 
templates to work with that... or some kind of wrapping class / 
interface.


But all I want is a kind of Javascript'y:

Every time this object gets .onDraw() called. It does whatever I 
told it to do.


auto t = new object1( onDraw(){ do stuff} );
auto t2 = new object2( onDraw() { do other stuff} );

I'm trying to do the same kind of API for button handling where 
it automatically links into event queues (which works) and the 
button does whatever I want it to. But, it can only access PUBLIC 
INTERFACES (globals) because it can't access the underlying 
object and globals are the only ones it can statically figure out 
at compile-time.




Re: Delegates and classes for custom code.

2018-04-16 Thread Chris Katko via Digitalmars-d-learn

Some typos in there.

execute == on_draw.

Basically, I'm just sending a delegate/lambda "custom function" 
at initialization time. But I'd like that delegate to somehow 
access the holding classes functions. Or figure out how to do 
that.


Maybe the class somehow sends the delegate a this parameter when 
the class goes to execute it?


Delegates and classes for custom code.

2018-04-16 Thread Chris Katko via Digitalmars-d-learn

What I want:

class viewport_t
  {
  int x,y,w,h;
  }

class dialog_t
  {
  int x,y;

  this( int x, int y, delegate void (viewport_t) on_draw )
{
this.x = x;
this.y = y;
this.execute = execute;
}

  void draw_text(string text)
{
}

  delegate void (viewport_t) on_draw;
  }

void function()
  {
  viewport_t v;
  dialog_t (15, 15,
delegate void (viewport_t)
  {
  draw_text("hello world"); //calls dialog_t function
  }
)
  }

Is this possible? Pass to a class, the code to run. But the code 
has to somehow know about the class methods.


I don't think you can pass "dialog_t.this" as it's being 
constructed!


Re: Error calling geqrs function from lubeck package.

2018-04-16 Thread Jamie via Digitalmars-d-learn

On Tuesday, 17 April 2018 at 03:26:25 UTC, Jamie wrote:


Sorry it's really an error calling geqrs function from mir-lapack 
package.





Error calling geqrs function from lubeck package.

2018-04-16 Thread Jamie via Digitalmars-d-learn
I'm attempting to use the lubeck package, as described here 
https://forum.dlang.org/post/axacgiisczwvygyef...@forum.dlang.org


I have lubeck, mir-algorithm, mir-blas, mir-lapack downloaded and 
accessible by the compiler, and I have installed liblapack-dev 
and libblas-dev.


When I attempt to run the example for geqrs, 
https://github.com/libmir/mir-lapack/blob/master/examples/geqrs/source/app.d , I get the following error


undefined reference to 'dgeqrs_'

Note that the issue comes from the line
geqrs(A_, B_, tau_, work_);

but the previous line works
geqrf(A_, tau_, work_);

After following the calls between the different packages and 
files, I find that the issue is likely the fact that

sgeqrf.o
dgeqrf.o
cgeqrf.o
zgeqrf.o

all exist in the liblapack-dev library and are hence callable, but
sgeqrs.o
dgeqrs.o
cgeqrs.o
zgeqrs.o

do not exist, and hence are not callable.

Is this an issue with mir-lapack? The fact that it is calling 
files/ functions that do not exist? Or is my version of 
liblapack-dev the incorrect one, as it doesn't contain the files/ 
functions being called?


Note that if I don't call the function geqrs(..); my code works, 
so I believe that I have set-up the mir- files correctly.


Re: making a struct an inputRange with free functions

2018-04-16 Thread Johannes Loher via Digitalmars-d-learn
Am 16.04.2018 um 21:27 schrieb Jonathan M Davis:
> On Monday, April 16, 2018 21:10:03 Johannes Loher via Digitalmars-d-learn 
> wrote:
>> Is there a way to do this? Here is a naive implementation:
>> https://run.dlang.io/is/JKvL80 .
>>
>> It does not pass `isInputRange` (I think, because the free functions are
>> not visible in the scope of `isInputRange`).
>>
>> Trying to iterate over it with a foreach loop results in a compile error:
>> Error: invalid foreach aggregate NoRange(0, 0).this(5), define
>> opApply(), range primitives, or use .tupleof
>>
>> Thanks for your help!
> 
> It doesn't work unless the module using the range either contains the free
> functions or imports them. So, I believe that that would mean that for
> isInputRange to pass, std.range.primitives would have to have access to the
> functions, which isn't going to happen for anything but dynamic arrays. It's
> a limitation of UFCS in general (it's similar to why using string lambdas
> with std.functional has become fairly rare - you can only use functions in
> them that std.functional already knows about). The only way for the
> functions to be associated with the type and thus always be available is if
> they're member functions. So, if you want a type to be a range, you have to
> declare the range functions as member functions.
> 
> - Jonathan M Davis
> 
Yeah, that's what I guessed, too. Thanks for the clarification.


Re: making a struct an inputRange with free functions

2018-04-16 Thread Steven Schveighoffer via Digitalmars-d-learn

On 4/16/18 3:10 PM, Johannes Loher wrote:

Is there a way to do this? Here is a naive implementation:
https://run.dlang.io/is/JKvL80 .

It does not pass `isInputRange` (I think, because the free functions are
not visible in the scope of `isInputRange`).


You are correct, it's not possible.


Trying to iterate over it with a foreach loop results in a compile error:
Error: invalid foreach aggregate NoRange(0, 0).this(5), define
opApply(), range primitives, or use .tupleof


The compiler actually looks to see if it has front as a member.

If it has that one function, the foreach is attempted.

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

But this is an implementation detail. It may not be valid in future 
versions of the compiler. Note, I had to add a ref to your popFront, or 
it's an infinite loop :)


-Steve


Re: making a struct an inputRange with free functions

2018-04-16 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, April 16, 2018 21:10:03 Johannes Loher via Digitalmars-d-learn 
wrote:
> Is there a way to do this? Here is a naive implementation:
> https://run.dlang.io/is/JKvL80 .
>
> It does not pass `isInputRange` (I think, because the free functions are
> not visible in the scope of `isInputRange`).
>
> Trying to iterate over it with a foreach loop results in a compile error:
> Error: invalid foreach aggregate NoRange(0, 0).this(5), define
> opApply(), range primitives, or use .tupleof
>
> Thanks for your help!

It doesn't work unless the module using the range either contains the free
functions or imports them. So, I believe that that would mean that for
isInputRange to pass, std.range.primitives would have to have access to the
functions, which isn't going to happen for anything but dynamic arrays. It's
a limitation of UFCS in general (it's similar to why using string lambdas
with std.functional has become fairly rare - you can only use functions in
them that std.functional already knows about). The only way for the
functions to be associated with the type and thus always be available is if
they're member functions. So, if you want a type to be a range, you have to
declare the range functions as member functions.

- Jonathan M Davis



making a struct an inputRange with free functions

2018-04-16 Thread Johannes Loher via Digitalmars-d-learn
Is there a way to do this? Here is a naive implementation:
https://run.dlang.io/is/JKvL80 .

It does not pass `isInputRange` (I think, because the free functions are
not visible in the scope of `isInputRange`).

Trying to iterate over it with a foreach loop results in a compile error:
Error: invalid foreach aggregate NoRange(0, 0).this(5), define
opApply(), range primitives, or use .tupleof

Thanks for your help!


Re: why use string for this example of appender?

2018-04-16 Thread WhatMeWorry via Digitalmars-d-learn
Thanks all.  I sometimes feel like Michael Corleone: "Just when I 
thought I was out, they pull me back in!" :)


I realize it is not the place for it, but sometimes I wish the 
Library Reference explained things in terms of "why".


Re: How/where to hack DMD to generate docs for string mixed members.

2018-04-16 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, April 16, 2018 16:05:40 9il via Digitalmars-d-learn wrote:
> On Sunday, 15 April 2018 at 08:17:21 UTC, Jonathan M Davis wrote:
> > On Sunday, April 15, 2018 07:59:17 Stefan Koch via
> >
> > Digitalmars-d-learn wrote:
> >> On Sunday, 15 April 2018 at 05:20:31 UTC, 9il wrote:
> >> > Hey,
> >> >
> >> > How/where to hack DMD to generate docs for string mixed
> >> > members?
> >> >
> >> > struct S
> >> > {
> >> >
> >> > mixin("
> >> >
> >> >  ///
> >> >  auto bar() {}
> >> >
> >> > ");
> >> >
> >> > }
> >> >
> >> > Best regards,
> >> > Ilya Yaroshenko
> >>
> >> hmm you should be able to see docs for string mixins, if not.
> >> try using -vcg-ast and try to run ddoc on the cg file
> >
> > AFAIK, it's never worked to see any ddoc from string mixins.
> > Certainly, I'm quite sure that it didn't used to work, so if it
> > does now, something changed within the last couple of years.
> >
> > The closest that I'm aware of is that putting /// on a template
> > mixin works so that you can do something like
> >
> > class MyException : Exception
> > {
> >
> > ///
> > mixin basicExceptionCtors;
> >
> > }
> >
> > and have the ddoc within the template mixin show up.
> >
> > - Jonathan M Davis
>
> Mixin templates works. The problem is the use case for the
> library (you know it) I am working on is looks like:
>
> --
>
> struct S
> {
>  mixin(WithGetters!("private", // or WithGettersAndConstructor
>  Date, "startDate",
>   Date, "endDate",
>   DayCount, "dayCount",
>   double, "yearFraction",
>   double, "spread",
>   Calculation, "calculation",
>  ));
> }
>
> --
>
> It should define members and getters and maybe one or more
> constructors.
> So mixin strings will be here anyway either in the struct or in a
> mixin template.

Well, the fact that the compiler doesn't display any ddoc from string mixins
is one of those annoying things that has come up from time to time for years
now. I don't know what the consequences of changing that behavior would be,
but on the surface at least, it seems like it would be a positive change.
Regardless, someone would have to make the change to the compiler, and
AFAIK, that particular improvement has never been a priority for anyone, but
I don't pay much attention to dmd PRs. A quick search turns up this bug
report:

https://issues.dlang.org/show_bug.cgi?id=2420

And it looks like that one links to this fixed bug

https://issues.dlang.org/show_bug.cgi?id=648

which looks like it was when it became possible to at least mark a template
mixin with /// to make its ddoc show up.

- Jonathan M Davis



Re: How/where to hack DMD to generate docs for string mixed members.

2018-04-16 Thread 9il via Digitalmars-d-learn

On Sunday, 15 April 2018 at 08:17:21 UTC, Jonathan M Davis wrote:
On Sunday, April 15, 2018 07:59:17 Stefan Koch via 
Digitalmars-d-learn wrote:

On Sunday, 15 April 2018 at 05:20:31 UTC, 9il wrote:
> Hey,
>
> How/where to hack DMD to generate docs for string mixed 
> members?

>
> struct S
> {
>
> mixin("
>
>  ///
>  auto bar() {}
>
> ");
>
> }
>
> Best regards,
> Ilya Yaroshenko

hmm you should be able to see docs for string mixins, if not. 
try using -vcg-ast and try to run ddoc on the cg file


AFAIK, it's never worked to see any ddoc from string mixins. 
Certainly, I'm quite sure that it didn't used to work, so if it 
does now, something changed within the last couple of years.


The closest that I'm aware of is that putting /// on a template 
mixin works so that you can do something like


class MyException : Exception
{
///
mixin basicExceptionCtors;
}

and have the ddoc within the template mixin show up.

- Jonathan M Davis


Mixin templates works. The problem is the use case for the 
library (you know it) I am working on is looks like:


--

struct S
{
mixin(WithGetters!("private", // or WithGettersAndConstructor
Date, "startDate",
Date, "endDate",
DayCount, "dayCount",
double, "yearFraction",
double, "spread",
Calculation, "calculation",
));
}

--

It should define members and getters and maybe one or more 
constructors.
So mixin strings will be here anyway either in the struct or in a 
mixin template.




Re: How/where to hack DMD to generate docs for string mixed members.

2018-04-16 Thread 9il via Digitalmars-d-learn

On Sunday, 15 April 2018 at 07:59:17 UTC, Stefan Koch wrote:

On Sunday, 15 April 2018 at 05:20:31 UTC, 9il wrote:

Hey,

How/where to hack DMD to generate docs for string mixed 
members?


struct S
{
mixin("
 ///
 auto bar() {}
");
}

Best regards,
Ilya Yaroshenko


hmm you should be able to see docs for string mixins, if not.
try using -vcg-ast and try to run ddoc on the cg file


-vcg-ast does not help:

---
import object;
struct S
{
mixin("\x0a ///\x0a auto bar() {}\x0a");
}
RTInfo!(S)
{
enum typeof(null) RTInfo = null;

}
---

Is it bug or is it possible to improve it?


Re: why use string for this example of appender?

2018-04-16 Thread Steven Schveighoffer via Digitalmars-d-learn

On 4/16/18 4:49 AM, Ali Çehreli wrote:

On 04/15/2018 11:46 PM, WhatMeForget wrote:
 >
 > I think I got a handle on D's static and dynamic arrays, till I come to
 > std.array and see all the shiny new tools. I can understand all the
 > replace.. functions, but the appender function gave me pause. The
 > documentation says appender "Returns a new Appender or RefAppender
 > initialized with a given array."

New Appender allocates new memory. If an array is already available for 
an Appender to use, then it's more efficient.


 > My first thought that doesn't D's built in arrays already allow
 > appending? (at least for dynamic arrays)

Yes but Appender is reported to be faster presumably it uses a different 
allocation scheme and may be more free compared to dynamic arrays that 
must play well with GC and its pages.


It's because it stores the relevant information in a local type vs. 
having to look it up in the GC.


It's also not all opaque calls (they can be inlined).


 > Another thing that had me wondering is the use of put()
 > down below; doesn't the append syntax (~=) give you the same exact
 > functionality; so why bother?

put() is old, ~= is new. Both are supported.



put is required for Appender to be an output range.

-Steve


Re: why use string for this example of appender?

2018-04-16 Thread Ali Çehreli via Digitalmars-d-learn

On 04/15/2018 11:46 PM, WhatMeForget wrote:
>
> I think I got a handle on D's static and dynamic arrays, till I come to
> std.array and see all the shiny new tools. I can understand all the
> replace.. functions, but the appender function gave me pause. The
> documentation says appender "Returns a new Appender or RefAppender
> initialized with a given array."

New Appender allocates new memory. If an array is already available for 
an Appender to use, then it's more efficient.


> My first thought that doesn't D's built in arrays already allow
> appending? (at least for dynamic arrays)

Yes but Appender is reported to be faster presumably it uses a different 
allocation scheme and may be more free compared to dynamic arrays that 
must play well with GC and its pages.


> And the example shows the use
> of appender with string.  Isn't string an immutable array or
> characters?

Mutable array of immutable characters: immutable(char)[].

What is important is that existing elements shoould not mutate so that 
existing slices don't get confused. Appending is fine because a new 
array is allocated and copied for the appending slice. (Aha! This 
feature is likely why Appender is faster than a dynamic array: it does 
not have the feature of "element stomping prevention". (Something is 
wrong with my English at the moment. :p))


> Wouldn't this be the last data type you would want to be
> appending to?

It's not unusual.

> Another thing that had me wondering is the use of put()
> down below; doesn't the append syntax (~=) give you the same exact
> functionality; so why bother?

put() is old, ~= is new. Both are supported.

Ali



why use string for this example of appender?

2018-04-16 Thread WhatMeForget via Digitalmars-d-learn


I think I got a handle on D's static and dynamic arrays, till I 
come to std.array and see all the shiny new tools. I can 
understand all the replace.. functions, but the appender function 
gave me pause. The documentation says appender "Returns a new 
Appender or RefAppender initialized with a given array."


My first thought that doesn't D's built in arrays already allow 
appending? (at least for dynamic arrays)  And the example shows 
the use of appender with string.  Isn't string an immutable array 
or characters?  Wouldn't this be the last data type you would 
want to be appending to?  Another thing that had me wondering is 
the use of put() down below; doesn't the append syntax (~=) give 
you the same exact functionality; so why bother?


void main()
{
import std.array;
import std.stdio: write, writeln, writef, writefln;
auto w = appender!string;
// pre-allocate space (this avoids costly reallocations)
w.reserve(10);
assert(w.capacity >= 10);

w.put('a'); // single elements
w.put("bc"); // multiple elements

// use the append syntax
w ~= 'd';
w ~= "ef";

writeln(w.data); // "abcdef"