Re: How to make a function table?

2019-02-15 Thread Alex via Digitalmars-d-learn
On Saturday, 16 February 2019 at 05:06:55 UTC, mnar53 wrote: Absolutely a newbie to D. This code mimics c, but unfortunately does not work: import std.typecons; import std.math; alias double function(double) UNARY; UNARY[] FCNS = [sin, cos, tan]; double[][] MAP (int idx, double[][] args)

Re: How does Rebindable suppress the compiler's optimizations for immutable?

2019-02-15 Thread SimonN via Digitalmars-d-learn
Thanks for the detailed answers! Yes, I accept that immutable guarantees should be implemented only during @safe that doesn't call into @trusted. On Friday, 15 February 2019 at 18:59:36 UTC, H. S. Teoh wrote: At the very least, such [union] code should be automatically @system. Sensible.

How to make a function table?

2019-02-15 Thread mnar53 via Digitalmars-d-learn
Absolutely a newbie to D. This code mimics c, but unfortunately does not work: import std.typecons; import std.math; alias double function(double) UNARY; UNARY[] FCNS = [sin, cos, tan]; double[][] MAP (int idx, double[][] args) nothrow { UNARY f = FCNS[idx]; foreach(i; 0 ..

Re: How does Rebindable suppress the compiler's optimizations for immutable?

2019-02-15 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Feb 15, 2019 at 03:50:33AM -0700, Jonathan M Davis via Digitalmars-d-learn wrote: > On Friday, February 15, 2019 3:06:34 AM MST Kagamin via Digitalmars-d-learn > wrote: > > Union is just a pretty cast, type system guarantees don't hold for > > it. > > Well, technically, what's supposed

Re: Casting int[] to ubyte[]: element wise cast or slice cast

2019-02-15 Thread Dennis via Digitalmars-d-learn
On Friday, 15 February 2019 at 17:18:28 UTC, H. S. Teoh wrote: Why? because that's the only case where you can reinterpret the .sizeof*.length bytes as an array of a different type. There is no conversion, the cast works as a reinterpretation. It's by design. But my problem is that it

Re: Generalizing over function pointers and delegates

2019-02-15 Thread Bastiaan Veelo via Digitalmars-d-learn
On Friday, 15 February 2019 at 17:28:45 UTC, H. S. Teoh wrote: On Fri, Feb 15, 2019 at 05:40:39PM +0100, ag0aep6g via Digitalmars-d-learn wrote: Your fun_to_dlg fails when the function has parameters. Yes. Delegates are basically syntactic sugar for a function pointer with an implicit first

Re: Generalizing over function pointers and delegates

2019-02-15 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Feb 15, 2019 at 05:40:39PM +0100, ag0aep6g via Digitalmars-d-learn wrote: > On 15.02.19 15:20, Bastiaan Veelo wrote: > > Exploiting this, it is possible to explicitly convert a function > > pointer into a delegate [2]: > > ``` > > Ret delegate(Args args) fun_to_dlg(Ret, Args...)(Ret

Re: Generalizing over function pointers and delegates

2019-02-15 Thread Bastiaan Veelo via Digitalmars-d-learn
On Friday, 15 February 2019 at 16:40:39 UTC, ag0aep6g wrote: Your fun_to_dlg fails when the function has parameters. Hah ok. std.functional.toDelegate() does work in its place though. As far as I see, it would be possible make the conversion would work by changing how a delegate's context is

Re: Casting int[] to ubyte[]: element wise cast or slice cast

2019-02-15 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Feb 15, 2019 at 04:17:12PM +, Dennis via Digitalmars-d-learn wrote: > I assumed that casting an int[] to a ubyte[] would keep all bytes and > quadruple the length of the original array. But when the array is a > literal, it keeps the same length but truncates every int element to a >

Re: Generalizing over function pointers and delegates

2019-02-15 Thread ag0aep6g via Digitalmars-d-learn
On 15.02.19 15:20, Bastiaan Veelo wrote: Exploiting this, it is possible to explicitly convert a function pointer into a delegate [2]: ``` Ret delegate(Args args) fun_to_dlg(Ret, Args...)(Ret function(Args args) fun) {     Ret delegate(Args) dlg;     dlg.funcptr = fun;     return dlg; }

Casting int[] to ubyte[]: element wise cast or slice cast

2019-02-15 Thread Dennis via Digitalmars-d-learn
I assumed that casting an int[] to a ubyte[] would keep all bytes and quadruple the length of the original array. But when the array is a literal, it keeps the same length but truncates every int element to a ubyte: ``` import std.stdio; void main() { // enum: enum litA =

Re: Generalizing over function pointers and delegates

2019-02-15 Thread Bastiaan Veelo via Digitalmars-d-learn
On Friday, 15 February 2019 at 14:30:45 UTC, Alex wrote: There is https://dlang.org/library/std/functional/to_delegate.html Ah, there it is :-) Thanks. A templated function also works. ``` int genfun(F)(F dg) {return dg();} ​ int top_level() {return -1;} ​ void main() { int nested()

Re: Generalizing over function pointers and delegates

2019-02-15 Thread Alex via Digitalmars-d-learn
On Friday, 15 February 2019 at 14:20:44 UTC, Bastiaan Veelo wrote: Given a function taking a delegate, for example ``` int fun(int delegate() dg) {return dg();} ``` Sometimes we need to call `fun` with a pointer to a nested function and other times with a pointer to a top level function. As

Generalizing over function pointers and delegates

2019-02-15 Thread Bastiaan Veelo via Digitalmars-d-learn
Given a function taking a delegate, for example ``` int fun(int delegate() dg) {return dg();} ``` Sometimes we need to call `fun` with a pointer to a nested function and other times with a pointer to a top level function. As function pointers do not implicitly convert to delegates, this does

Re: Should D file end with newline?

2019-02-15 Thread Patrick Schluter via Digitalmars-d-learn
On Wednesday, 13 February 2019 at 05:13:12 UTC, sarn wrote: On Tuesday, 12 February 2019 at 20:03:09 UTC, Jonathan M Davis wrote: So, I'd say that it's safe to say that dmd The whole thing just seems like a weird requirement that really shouldn't be there, Like I said in the first reply,

Re: How does Rebindable suppress the compiler's optimizations for immutable?

2019-02-15 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, February 15, 2019 3:06:34 AM MST Kagamin via Digitalmars-d-learn wrote: > Union is just a pretty cast, type system guarantees don't hold > for it. Well, technically, what's supposed to be the case is that when you cast, the type system guarantees still hold but it's up to the

Re: How does Rebindable suppress the compiler's optimizations for immutable?

2019-02-15 Thread Kagamin via Digitalmars-d-learn
Union is just a pretty cast, type system guarantees don't hold for it.

Re: How does Rebindable suppress the compiler's optimizations for immutable?

2019-02-15 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, February 14, 2019 11:59:31 PM MST Stefan Koch via Digitalmars- d-learn wrote: > On Thursday, 14 February 2019 at 23:55:18 UTC, SimonN wrote: > > std.typecons.Rebindable!(immutable A) is implemented as: > > private union { > > > > immutable(A) original; > > A