Re: anyway to set a const object after the fact?

2018-10-30 Thread Laurent Tréguier via Digitalmars-d-learn

On Monday, 29 October 2018 at 21:50:32 UTC, aliak wrote:

Hi, so if you have this piece of code:

struct C {

  void f() {
string[] others;
const string[] restArgs;
foreach (i, arg; args) {
  if (isValidArg(arg)) {
restArgs = args[i + 1 .. $];
break;
  }
  others ~= arg;
}
// "others" is a list of args before the valid arg is 
encountered

// "restArgs" is a list that is the args after the valid arg
  }
}

Is there anyway to set a const object after declaring it in the 
above context?


Cheers,
- Ali


It looks like there is a Rebindable type for that in std.typecons 
: https://dlang.org/phobos/std_typecons.html#Rebindable


Re: Built-in array opSliceAssign

2018-10-30 Thread Eduard Staniloiu via Digitalmars-d-learn

On Thursday, 25 October 2018 at 21:00:46 UTC, Adam D. Ruppe wrote:
On Thursday, 25 October 2018 at 19:56:18 UTC, Stanislav Blinov 
wrote:
The current behavior of the compiler is quite the opposite of 
those "same as" above.


Yeah, I guess I am maybe selectively reading the spec in light 
of the implementation... but I think the examples are just 
sloppy. Or maybe we have a buggy implementation but idk which 
is more useful.


I still hold my believe that if opAssign is defined then that 
should be used.


In my humble opinion, the current way might/could be faster, but 
it's not correct.


DirectX bindings

2018-10-30 Thread John Burton via Digitalmars-d-learn

I want to do some graphics using direct3d11 on windows.
There are some bindings that I used once before 
https://github.com/evilrat666/directx-d

However they are marked as [discontinued]
While I'm sure they will continue to work so wouldn't worry about 
using this I wonder if there are any other options?


Re: DirectX bindings

2018-10-30 Thread Mike Parker via Digitalmars-d-learn

On Tuesday, 30 October 2018 at 10:30:48 UTC, John Burton wrote:

I want to do some graphics using direct3d11 on windows.
There are some bindings that I used once before 
https://github.com/evilrat666/directx-d

However they are marked as [discontinued]
While I'm sure they will continue to work so wouldn't worry 
about using this I wonder if there are any other options?


https://code.dlang.org/packages/aurora-directx


Re: DirectX bindings

2018-10-30 Thread John Burton via Digitalmars-d-learn

On Tuesday, 30 October 2018 at 10:46:35 UTC, Mike Parker wrote:

On Tuesday, 30 October 2018 at 10:30:48 UTC, John Burton wrote:

I want to do some graphics using direct3d11 on windows.
There are some bindings that I used once before 
https://github.com/evilrat666/directx-d

However they are marked as [discontinued]
While I'm sure they will continue to work so wouldn't worry 
about using this I wonder if there are any other options?


https://code.dlang.org/packages/aurora-directx


Thank you, I don't know why my search of the repository didn't 
find this. I think I was searching for direct3d perhaps :)


Re: anyway to set a const object after the fact?

2018-10-30 Thread aliak via Digitalmars-d-learn

On Monday, 29 October 2018 at 22:12:24 UTC, Paul Backus wrote:

Use a lambda:

const string[] restArgs = () {
  foreach(i, arg; args) {
if (isValidArg(arg)) {
  return args[i+1 .. $];
}
others ~= arg;
  }
}();


That works.


Re: anyway to set a const object after the fact?

2018-10-30 Thread aliak via Digitalmars-d-learn
On Tuesday, 30 October 2018 at 08:18:15 UTC, Laurent Tréguier 
wrote:

On Monday, 29 October 2018 at 21:50:32 UTC, aliak wrote:

Hi, so if you have this piece of code:

struct C {

  void f() {
string[] others;
const string[] restArgs;
foreach (i, arg; args) {
  if (isValidArg(arg)) {
restArgs = args[i + 1 .. $];
break;
  }
  others ~= arg;
}
// "others" is a list of args before the valid arg is 
encountered
// "restArgs" is a list that is the args after the valid 
arg

  }
}

Is there anyway to set a const object after declaring it in 
the above context?


Cheers,
- Ali


It looks like there is a Rebindable type for that in 
std.typecons : 
https://dlang.org/phobos/std_typecons.html#Rebindable


Guess I could do that. But would there be a difference if I just 
declared the restArgs as non const then? Given the objective is 
"set this var to point to this thing and not allow it to be set 
to point to anything else".


Re: anyway to set a const object after the fact?

2018-10-30 Thread aliak via Digitalmars-d-learn

On Monday, 29 October 2018 at 22:05:16 UTC, H. S. Teoh wrote:


What exactly are you trying to accomplish?  I.e., what 
semantics do you want from modifying restArgs?


Trying to set restArgs to point to some data but only set it 
once. Would require some sort of control flow analysis on the 
part of D though I guess. So meh.




If you're looking to rebind the array, just be a bit more 
explicit in how you spell out the type:


const(string)[] restArgs;

will allow you to rebind it to a different array / slice, but 
still not permit you to modify the array elements.



T


Ya, I was looking to bind once. Like what you can do this with a 
module constructor:


const int a;
static this() {
  a = 5;
}




Re: anyway to set a const object after the fact?

2018-10-30 Thread Laurent Tréguier via Digitalmars-d-learn

On Tuesday, 30 October 2018 at 11:23:48 UTC, aliak wrote:
Guess I could do that. But would there be a difference if I 
just declared the restArgs as non const then? Given the 
objective is "set this var to point to this thing and not allow 
it to be set to point to anything else".


The difference with const is that you wouldn't be able to modify 
the array itself (by adding or removing arguments for example). 
But yes, you can still re-assign it multiple times using 
Rebindable, so the lambda solution is a better idea indeed.


Re: Where do I learn to use GtkD

2018-10-30 Thread Michelle Long via Digitalmars-d-learn

On Sunday, 13 March 2016 at 19:28:57 UTC, karabuta wrote:
Gtk3 from python3 has got I nice book with examples that are 
not so advanced but enough to get you doing real work(from a 
beginner point of view). GtkD seem to have changed the API 
structure compared to python3 Gtk3 and the demo examples just 
"show-off" IMO :). The documentation is really^ not good :)


Any help on where I can get better leaning materials(GtkD)? 
Repo, blogs post, etc please


I will avoid using GTK for large projects. If can be used for 
simple things and you can automate a lot of acts(and use glade 
for UI design)... but it has some problems that will bite you in 
the long run.


I'd just jump in to it, it's not too hard but hard to find the 
correct information. It will be a time investment of a few months.


Might try nuklearD first. Seems to be better in many aspects.


Re: anyway to set a const object after the fact?

2018-10-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, October 30, 2018 2:18:15 AM MDT Laurent Tréguier via 
Digitalmars-d-learn wrote:
> On Monday, 29 October 2018 at 21:50:32 UTC, aliak wrote:
> > Hi, so if you have this piece of code:
> >
> > struct C {
> >
> >   void f() {
> >
> > string[] others;
> > const string[] restArgs;
> > foreach (i, arg; args) {
> >
> >   if (isValidArg(arg)) {
> >
> > restArgs = args[i + 1 .. $];
> > break;
> >
> >   }
> >   others ~= arg;
> >
> > }
> > // "others" is a list of args before the valid arg is
> >
> > encountered
> >
> > // "restArgs" is a list that is the args after the valid arg
> >
> >   }
> >
> > }
> >
> > Is there anyway to set a const object after declaring it in the
> > above context?
> >
> > Cheers,
> > - Ali
>
> It looks like there is a Rebindable type for that in std.typecons
>
> : https://dlang.org/phobos/std_typecons.html#Rebindable

Rebindable is specifically intentended for class references, since D doesn't
really distinguish between the reference and what it's pointing to, making
it impossible to under normal circumstances to have a mutable reference to a
const object. On the other hand, you can easily have mutable arrays of const
objects. Using Rebindable is a really a hack (albeit a very necessary one
for some circumstances), and I would very much advise _against_ using it if
you don't need it. Historically, it tends to run into compiler bugs, because
it's trying to hack it's way around the type system, and if you're not
dealing with class references, there's probably a better way to handle the
problem.

- Jonathan M Davis






How in the name of D do you deal with more than one optional template parameter?

2018-10-30 Thread aliak via Digitalmars-d-learn

Hi,

Do you guys have any strategies for dealing with templates when 
they have more than one optional  parameter?


E.g let's say we have a type C that takes three parameters

struct B(T) {}
struct C(string name, T, string desc) {}

And let's say I want T and desc to be optional and T should be of 
type B, i.e. I would want to be able to do is this:


auto x = C!("name", B!int)();
auto y = C!("name", B!int, "desc")();
auto z = C!("name", "desc")();

So C is a template that must have a name, and can optionally have 
a B, or a description, or both.


The immediate (maybe naive) way that comes to mind is:

struct C(string name, rest...)
if (rest.length <= 2)
if (rest has one elements then it can be either template B or a 
string) // how?
if (rest has two elements then it must be template B followed by 
string) // how?

{
  static if (rest[0] is type B) {
enum hasB = true;
static if (rest.length > 1) {
  string desc = rest[1];
}
  } else {
enum hasB = false;
if (rest.length) {
  string desc = rest[1];
} else {
  string desc = null;
}
  }

  // Now I have access to a desc if it was provided
  // And a boolean that tells me if I have a B type.
  // But dayum that was a lot of hoops.
}

Giving it some more thought, one could do this too:

struct Void {}

struct C(string name, T, string desc)
if (isInstanceOf!(B, T) || is(T == Void)
{}
template C(string name T) {
  alias C = C!(name, T, null);
}
template C(string name, string desc) {
  alias C = C!(name, Void, desc);
}

Which requires one to create this meta-Void type to get to work. 
Which I guess is not that bad. But, anyway.


Are there other ways? Or does anyone have any tips on how to deal 
with this? Or is the alias way generally the way to go?


Cheers,
- Ali


Re: How in the name of D do you deal with more than one optional template parameter?

2018-10-30 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Oct 30, 2018 at 08:46:31PM +, aliak via Digitalmars-d-learn wrote:
> Hi,
> 
> Do you guys have any strategies for dealing with templates when they
> have more than one optional  parameter?
> 
> E.g let's say we have a type C that takes three parameters
> 
> struct B(T) {}
> struct C(string name, T, string desc) {}
> 
> And let's say I want T and desc to be optional and T should be of type
> B, i.e. I would want to be able to do is this:
> 
> auto x = C!("name", B!int)();
> auto y = C!("name", B!int, "desc")();
> auto z = C!("name", "desc")();
> 
> So C is a template that must have a name, and can optionally have a B,
> or a description, or both.
> 
> The immediate (maybe naive) way that comes to mind is:
> 
> struct C(string name, rest...)
> if (rest.length <= 2)
> if (rest has one elements then it can be either template B or a string) //
> how?
> if (rest has two elements then it must be template B followed by string) //
> how?

struct C(Args...)
if ((rest.length == 2 && is(Args[0] == B!C, C) && is(Args[1] == 
string)) ||
(rest.length == 1 && (is(Args[0] == B!C, C) || is(Args[0] == 
string
{
...
}


[...]
> Giving it some more thought, one could do this too:
> 
> struct Void {}

This is unnecessary.  Why not just use `void` directly?

struct C(string name, T, string desc)
if (is(T : B!C, C) || is(T == void))
{
...
}


> Are there other ways? Or does anyone have any tips on how to deal with
> this?  Or is the alias way generally the way to go?
[...]

You can also use default parameters for templates:

struct C(string name, T, string desc = null)
{
...
}

then you just need one more alias for the string-only variant:

alias C(string name, string desc) = C!(name, void, desc);

Note that this syntax is much shorter than the full eponymous template
syntax, which makes it easier to deal with longer parameter lists:

struct ManyParams(A a, B b=defaultB, C c=defaultC, D d=defaultD)
{
...
}
alias ManyParams(A a, C c, D d=defaultD) = ManyParams!(a, null, c, d);
alias ManyParams(A a, D d=defaultD) = ManyParams!(a, void, void, d);

And so on.

And there's probably a way to auto-generate those alias templates if you
anticipate needing to do this many times. (Or if you're insane and want
to let the user specify different arguments in any order.) Mixins and
CTFE codegen FTW!  :-D


T

-- 
A mathematician is a device for turning coffee into theorems. -- P. Erdos


std.math log and family

2018-10-30 Thread Joe via Digitalmars-d-learn
I've discovered that the 'log' function as well several similar 
functions are only defined as taking a real argument and 
returning a real, unlike most other std.math functions, which 
have triple definitions (also supporting double and float types). 
This created a problem when I tried compiling code like the 
following:


---
import std.math;

alias double function(double) Funcptr;

Funcptr sinptr = &sin;
Funcptr tanptr = &tan;
Funcptr logptr = &log;
---

dmd (and ldc2) report

test.d(7): Error: cannot implicitly convert expression & log of 
type real function(real x) pure nothrow @nogc @safe to double 
function(double)


[ldc2 also reports
test.d(6): Error: cannot implicitly convert expression & tan of 
type real function(real x) pure nothrow @nogc @trusted to double 
function(double)

but apparently this is an LDC-only problem]

I'd like to know if the lack of double/float versions of 'log', 
'log10', etc. are intentional, i.e., there's some rationale 
behind it, or an oversight.  I'd also like to know the 
proper/best way to deal with the error, considering the real code 
embeds the 'Funcptr's in an array of structs.  Is it better to 
write a function 'mylog' that internally casts the argument and 
the return value, or is there some safe way to cast away the 
'log' function at the point of initializing the array?


Also, is it preferable to post the first question 
(intentional/oversight) in the Phobos forum? And what is the 
preferred way of reporting the LDC problem, via GitHub?


Re: std.math log and family

2018-10-30 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Oct 31, 2018 at 12:48:03AM +, Joe via Digitalmars-d-learn wrote:
[...]
> I'd like to know if the lack of double/float versions of 'log',
> 'log10', etc. are intentional, i.e., there's some rationale behind it,
> or an oversight.

It's an oversight.  Thanks for bringing it to our attention.

Well, actually there's a historical reason behind it, but that's no
excuse.  Please file a bug against Phobos here:

http://issues.dlang.org/


> I'd also like to know the proper/best way to deal with the error,
> considering the real code embeds the 'Funcptr's in an array of
> structs.  Is it better to write a function 'mylog' that internally
> casts the argument and the return value, or is there some safe way to
> cast away the 'log' function at the point of initializing the array?

I don't think it's safe to cast the function pointer, because of
mismatching types. You might get undefined behaviour at runtime. Define
your own wrapper instead, as you said, that just forwards the
implementation to std.math.log. You don't need to cast the argument
because it implicitly promotes to real anyway:

float mylog(float arg) { return std.math.log(arg); }
auto funcptr = &mylog;


> Also, is it preferable to post the first question
> (intentional/oversight) in the Phobos forum? And what is the preferred
> way of reporting the LDC problem, via GitHub?

If you're unsure, just ask on the forum. :)

In this case, this is a bug in Phobos itself, so an issue should be
filed at: http://issues.dlang.org/

(While discussion on the forum may be helpful, always remember to file
an issue, because forum discussions may get forgotten after some time,
whereas issues in the bug tracker will eventually get looked at. Don't
be shy to clamor on the forum about bugs that nobody has responded to in
a long time, though.  Sometimes things get overlooked and we just need
a reminder.)


T

-- 
Long, long ago, the ancient Chinese invented a device that lets them see 
through walls. It was called the "window".


Re: std.math log and family

2018-10-30 Thread kinke via Digitalmars-d-learn

On Wednesday, 31 October 2018 at 00:48:03 UTC, Joe wrote:
I'd like to know if the lack of double/float versions of 'log', 
'log10', etc. are intentional, i.e., there's some rationale 
behind it, or an oversight.


Just laziness or people still thinking that you don't lose any 
performance by computing in `real` precision.



[ldc2 also reports
test.d(6): Error: cannot implicitly convert expression & tan of 
type real function(real x) pure nothrow @nogc @trusted to 
double function(double)

but apparently this is an LDC-only problem]


You've got to be using an older LDC version; v1.12 does define 
tan() for all 3 FP types.



I'd also like to know the proper/best way to deal with the error


Long-term? Definitely adding the missing implementations to 
std.math ;), continuing my work here: 
https://github.com/dlang/phobos/pull/6272

Short-term, I'd go with tiny wrappers.


Re: std.math log and family

2018-10-30 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Oct 31, 2018 at 01:14:37AM +, kinke via Digitalmars-d-learn wrote:
> On Wednesday, 31 October 2018 at 00:48:03 UTC, Joe wrote:
> > I'd like to know if the lack of double/float versions of 'log',
> > 'log10', etc. are intentional, i.e., there's some rationale behind
> > it, or an oversight.
> 
> Just laziness or people still thinking that you don't lose any
> performance by computing in `real` precision.

Is it true that on modern hardware computing with `real` reverts to slow
x87 emulation in the CPU instead of using SSE/MMX/whatever native math
functions?  I've heard that said a lot, I'm just wondering if there's
actual measurements to back that up.


[...]
> > I'd also like to know the proper/best way to deal with the error
> 
> Long-term? Definitely adding the missing implementations to std.math ;),
> continuing my work here: https://github.com/dlang/phobos/pull/6272
> Short-term, I'd go with tiny wrappers.

Yeah, going forward std.math definitely needs to have native
float/double counterparts for all functions.  Preferably CTFE-eable
versions of them all, so that I can compute my math lookup tables at
compile-time. :-D


T

-- 
Любишь кататься - люби и саночки возить.