return scope ref outlives the scope of the argument
struct Container { } static struct Inserter { private Container* container; private this(return scope ref Container container) @trusted { this.container = } } auto func()() { Container container; return Inserter(container); } void main() { static assert(!is(typeof(func!(; } The code above compiles with dmd 2.085, but not 2.086 (with -preview=dip1000). What am I doing wrong?
How to prepare and generate a simple lightweight binary?
There are lots of talks on this forum about Statical linking, Dynamic linking. There are even shouts: "use the ldc compiler instead, it can do all that and even more than the default dmd compiler!!!" and bunch of compiler flags, no instructions on how to start or even steps on how to reproduce a simple program that wouldn't weight megabytes. I would like receive all the steps in this thread, with all the obvious download links and little commenting - the walkthrough. What I would expect: A simple executable program that does a writeln and do not weight tons of megabytes. Thanks. (1 ton of megabyte == 1 megabyte == 1MB)
Re: return scope ref outlives the scope of the argument
On Tuesday, June 25, 2019 1:32:58 AM MDT Eugene Wissner via Digitalmars-d- learn wrote: > struct Container > { > } > > static struct Inserter > { > private Container* container; > > private this(return scope ref Container container) @trusted > { > this.container = > } > > } > > auto func()() > { > Container container; > return Inserter(container); > } > > void main() > { > static assert(!is(typeof(func!(; > } > > The code above compiles with dmd 2.085, but not 2.086 (with > -preview=dip1000). What am I doing wrong? You're storing a pointer to a scope variable. That's violating the entire point of scope. If something is scope, you can't store any kind of reference to it. And since container is a local variable in func, and Inserter tries to return from func with a pointer to container, you definitely have an @safety problem, because that pointer would be invalid once func returned. - Jonathan M Davis
Re: return scope ref outlives the scope of the argument
On Tuesday, 25 June 2019 at 11:16:47 UTC, Jonathan M Davis wrote: On Tuesday, June 25, 2019 1:32:58 AM MDT Eugene Wissner via Digitalmars-d- learn wrote: struct Container { } static struct Inserter { private Container* container; private this(return scope ref Container container) @trusted { this.container = } } auto func()() { Container container; return Inserter(container); } void main() { static assert(!is(typeof(func!(; } The code above compiles with dmd 2.085, but not 2.086 (with -preview=dip1000). What am I doing wrong? You're storing a pointer to a scope variable. That's violating the entire point of scope. If something is scope, you can't store any kind of reference to it. And since container is a local variable in func, and Inserter tries to return from func with a pointer to container, you definitely have an @safety problem, because that pointer would be invalid once func returned. - Jonathan M Davis So you're saying that func() shouldn't compile? And it is exactly what the assertion in the main function does: it asserts that the function cannot be instantiated. And it was true for 2.085 but it can be instantiated with 2.086.
How to use template Object in interface?
hunt-cache current version use template implemention adapter changes. I want use Interface to define Adapter, this master code unable to comple. How to do it? D programming language design flaws? ```bash git clone https://github.com/huntlabs/hunt-cache cd hunt-cache/example dub run -v ... source/app.d(29,19): Error: no property name for type Nullable!(User) ```
Re: How to convert array of structs to JSON
On Tuesday, 25 June 2019 at 05:33:57 UTC, mark wrote: I have the following array of structs: struct Person { string name; int age; }; Person people[]; Person p; Person p1 = { "Bob", 12 }; Person p2 = { "Bob", 12 }; people ~= p1; people ~= p2; I've read through the std.json documentation, but I'm still confused as to how to create an array of objects, especially if the objects are structs. I've included this: JSONValue jj; jj["nodes"].array = []; But I get the error: JSONValue is not an object. Ideally, I'd like to have a json output that looks like this: [ { "name": "Bob", "age": 12 }, { "name": "Bob", "age": 12 }, ] Any help would be greatly appreciated. use hunt library: https://github.com/huntlabs/hunt Sample code: import hunt.serialize; void main() { auto json = toJson(pepole); }
Conversion problem.
Hello! Here I am again, with my problem ... In my program, I cannot manage to convert from "double" to "int". Here is the code: << ++ struct Animation - the structure that implements the animation + / struct Animation { private List! (Image) img; /// List of pictures in the animation private double f; /// current picture in animation private double s; /// speed of animation /// speed setter (double s) public void setSpeed (double sp) { s = sp; } /// Adds a picture to the list public void addFrame (Image pia) { img.add (pia); } /// Sets the image to a specific place in the list public void setFrame (int id, Image pia) { img.set (id, pia); } /// Returns a picture from the list public Image getFrame (int id) { return img.get (id); } /// Returns the list public List! (Image) getList () { return img; } /// The animation event itself. Returns the current animation image. public Image step () { writeln ("Animation :: step start ..."); if (floor (f) == img.length) { f = 0.1f; writeln ("Animation :: f = 0.1f"); } else { f + = s; writeln ("Animation :: f + = s"); } const int i = roundTo! (int) (f); writeln ("i set"); writeln ("i:", i); return img.get (i); } } >> Another code: << import std.stdio; / ++ The structure that implements the list of objects. + / struct List (TObj) { private TObj [] obj; /// Adds an object to the list public void add (TObj o) { obj ~ = o; } /// Returns an object from the list public TObj get (int id) { return obj [id]; } /// Sets an object in a specific place. public void set (int id, TObj o) { obj [id] = o; } /// Returns the length of the list public int length () { return obj.length; } /// Gives the garbage collector a signal to clear the list. public void free () { obj = null; } } >> "If you don’t understand something, the translator has bricked it." In the moment: << public Image step () { writeln ("Animation :: step start ..."); if (floor (f) == img.length) { f = 0.1f; writeln ("Animation :: f = 0.1f"); } else { f + = s; writeln ("Animation :: f + = s"); } const int i = roundTo! (int) (f); // Error here writeln ("i set"); writeln ("i:", i); return img.get (i); } >> The compiler is silent, but when you start the program, reaching this place, it hangs. Is there any way to convert a "double" to an "int" without errors?
Re: return scope ref outlives the scope of the argument
On Tuesday, June 25, 2019 1:32:58 AM MDT Eugene Wissner via Digitalmars-d- learn wrote: > struct Container > { > } > > static struct Inserter > { > private Container* container; > > private this(return scope ref Container container) @trusted > { > this.container = > } > > } > > auto func()() > { > Container container; > return Inserter(container); > } > > void main() > { > static assert(!is(typeof(func!(; > } > > The code above compiles with dmd 2.085, but not 2.086 (with > -preview=dip1000). What am I doing wrong? Okay. I clearly looked over what you posted too quickly and assumed that the subject was the error that you were actually getting. The @trusted there is what's making the static asertion fail. Inserter is able to compile with -dip1000 (or -preview=dip1000), because you marked it as @trusted, which throws away the scope checks. If you mark it @safe, it won't compile. Without -dip1000, I wouldn't have expected anything to be caught, but trying it on run.dlang.io, it looks like the return probably makes it fail, which I find surprising, since I didn't think that return had any effect without -dip25, but I haven't done much with return on parameters. You'd have an easier time figuring out what's going on if you'd just not make func a template rather than use the static assertion, because then you'd see the compiler errors. In any case, by using @trusted, you're getting around the scope compiler checks, which is why Inserter is able to compile with -dip1000. Without -dip1000, I'm not experience enough with return parameters to know what the compiler will or won't catch, but the code is an @safety probelm regardless. It does look like the behavior changed with 2.086 even without -dip1000 being used, which probably has something to do with how the compiler was changed for DIP 1000, though it probably wasn't on purpose, since in theory, the behavior shouldn't have changed without -dip1000, but I don't know. - Jonathan M Davis
Re: How to use template Object in interface?
On Tuesday, 25 June 2019 at 12:11:47 UTC, zoujiaqing wrote: hunt-cache current version use template implemention adapter changes. I want use Interface to define Adapter, this master code unable to comple. How to do it? D programming language design flaws? ```bash git clone https://github.com/huntlabs/hunt-cache cd hunt-cache/example dub run -v ... source/app.d(29,19): Error: no property name for type Nullable!(User) ``` Interface code: https://github.com/huntlabs/hunt-cache/blob/master/source/hunt/cache/adapter/Adapter.d ```D module hunt.cache.adapter.Adapter; import hunt.cache.Nullable; interface Adapter { Nullable!V get(V) (string key); Nullable!V[string] get(V) (string[] keys); void set(V) (string key, V value, uint expired); void set(V) (V[string] maps, uint expired); bool setIfAbsent(V) (string key, V value); bool hasKey(string key); bool remove(string key); void remove(string[] keys); void clear(); } ```
Re: return scope ref outlives the scope of the argument
On Tuesday, 25 June 2019 at 12:04:27 UTC, Jonathan M Davis wrote: On Tuesday, June 25, 2019 1:32:58 AM MDT Eugene Wissner via Digitalmars-d- learn wrote: struct Container { } static struct Inserter { private Container* container; private this(return scope ref Container container) @trusted { this.container = } } auto func()() { Container container; return Inserter(container); } void main() { static assert(!is(typeof(func!(; } The code above compiles with dmd 2.085, but not 2.086 (with -preview=dip1000). What am I doing wrong? Okay. I clearly looked over what you posted too quickly and assumed that the subject was the error that you were actually getting. The @trusted there is what's making the static asertion fail. Inserter is able to compile with -dip1000 (or -preview=dip1000), because you marked it as @trusted, which throws away the scope checks. If you mark it @safe, it won't compile. Without -dip1000, I wouldn't have expected anything to be caught, but trying it on run.dlang.io, it looks like the return probably makes it fail, which I find surprising, since I didn't think that return had any effect without -dip25, but I haven't done much with return on parameters. You'd have an easier time figuring out what's going on if you'd just not make func a template rather than use the static assertion, because then you'd see the compiler errors. In any case, by using @trusted, you're getting around the scope compiler checks, which is why Inserter is able to compile with -dip1000. Without -dip1000, I'm not experience enough with return parameters to know what the compiler will or won't catch, but the code is an @safety probelm regardless. It does look like the behavior changed with 2.086 even without -dip1000 being used, which probably has something to do with how the compiler was changed for DIP 1000, though it probably wasn't on purpose, since in theory, the behavior shouldn't have changed without -dip1000, but I don't know. - Jonathan M Davis Yes, reduced code could be a bit better. @trusted doesn't throw scope checks away (and it wouldn't make any sense since I don't see another way to make the code above safe). Try: struct Container { } private Container* stuff(return scope ref Container container) @trusted { return } auto func() { Container container; return stuff(container); } It fails with -dip1000 and works without (as expected). "return scope ref" parameter in the constructor means, that the constructed object has the same scope as the scope of the argument. I just want to know whether the behaviour of 2.085 or 2.086 is correct and if it is an "improvement" in 2.086, what I'm doing wrong.
Re: is there any micro-service library in D?
On Wednesday, 19 June 2019 at 08:29:15 UTC, dangbinghoo wrote: hi there, Does anyone know the micro-service oriented design library or framework in D? thanks! binghoo dang You can try hunt-service: hunt-service is distributed RPC framework for DLang based on gRPC and neton. https://github.com/huntlabs/hunt-service
Blog Post #0047: ScaleButton and VolumeButton
There are a couple of things to watch out for with the ScaleButton and its offspring, the VolumeButton. Read all about it here: https://gtkdcoding.com/2019/06/25/0047-scalebutton-and-volumebutton.html
Re: How to prepare and generate a simple lightweight binary?
On Tuesday, 25 June 2019 at 13:13:34 UTC, BoQsc wrote: What I would expect: A simple executable program that does a writeln and do not weight tons of megabytes. Thanks. void main() { import std.stdio; writeln("Hello world!"); } Ubuntu 18.04, LDC v1.16: ldc2 hello.d => ~1.7 MB ldc2 hello.d -link-defaultlib-shared => < 20 KB The 2nd executable depends on the shared druntime and Phobos libraries though, whereas the first one is standalone.
Re: Casting to interface not allowed in @safe code?
On Tuesday, 25 June 2019 at 16:51:46 UTC, Nathan S. wrote: On Sunday, 23 June 2019 at 21:24:14 UTC, Nathan S. wrote: https://issues.dlang.org/show_bug.cgi?id=2. The fix for this has been accepted and is set for inclusion in DMD 2.080. 088 :)
Re: Conversion problem.
On Tue, Jun 25, 2019 at 12:08:07PM +, Den_d_y via Digitalmars-d-learn wrote: > Hello! Here I am again, with my problem ... In my program, I cannot manage > to convert from "double" to "int". Here is the code: [...] Did you try this? import std.conv : to; double d = ...; int i = d.to!int; T -- People tell me I'm stubborn, but I refuse to accept it!
Re: Casting to interface not allowed in @safe code?
On Sunday, 23 June 2019 at 21:24:14 UTC, Nathan S. wrote: https://issues.dlang.org/show_bug.cgi?id=2. The fix for this has been accepted and is set for inclusion in DMD 2.080.
Re: return scope ref outlives the scope of the argument
On Tuesday, 25 June 2019 at 07:32:58 UTC, Eugene Wissner wrote: struct Container { } static struct Inserter { private Container* container; private this(return scope ref Container container) @trusted { this.container = } } auto func()() { Container container; return Inserter(container); } void main() { static assert(!is(typeof(func!(; } The code above compiles with dmd 2.085, but not 2.086 (with -preview=dip1000). What am I doing wrong? Whatever. https://issues.dlang.org/show_bug.cgi?id=20006
Re: return scope ref outlives the scope of the argument
On Tuesday, June 25, 2019 6:32:35 AM MDT Eugene Wissner via Digitalmars-d- learn wrote: > On Tuesday, 25 June 2019 at 12:04:27 UTC, Jonathan M Davis wrote: > > On Tuesday, June 25, 2019 1:32:58 AM MDT Eugene Wissner via > > > > Digitalmars-d- learn wrote: > >> struct Container > >> { > >> } > >> > >> static struct Inserter > >> { > >> > >> private Container* container; > >> > >> private this(return scope ref Container container) > >> > >> @trusted > >> > >> { > >> > >> this.container = > >> > >> } > >> > >> } > >> > >> auto func()() > >> { > >> > >> Container container; > >> return Inserter(container); > >> > >> } > >> > >> void main() > >> { > >> > >> static assert(!is(typeof(func!(; > >> > >> } > >> > >> The code above compiles with dmd 2.085, but not 2.086 (with > >> -preview=dip1000). What am I doing wrong? > > > > Okay. I clearly looked over what you posted too quickly and > > assumed that the subject was the error that you were actually > > getting. The @trusted there is what's making the static > > asertion fail. > > > > Inserter is able to compile with -dip1000 (or > > -preview=dip1000), because you marked it as @trusted, which > > throws away the scope checks. If you mark it @safe, it won't > > compile. Without -dip1000, I wouldn't have expected anything to > > be caught, but trying it on run.dlang.io, it looks like the > > return probably makes it fail, which I find surprising, since I > > didn't think that return had any effect without -dip25, but I > > haven't done much with return on parameters. > > > > You'd have an easier time figuring out what's going on if you'd > > just not make func a template rather than use the static > > assertion, because then you'd see the compiler errors. > > > > In any case, by using @trusted, you're getting around the scope > > compiler checks, which is why Inserter is able to compile with > > -dip1000. Without -dip1000, I'm not experience enough with > > return parameters to know what the compiler will or won't > > catch, but the code is an @safety probelm regardless. It does > > look like the behavior changed with 2.086 even without -dip1000 > > being used, which probably has something to do with how the > > compiler was changed for DIP 1000, though it probably wasn't on > > purpose, since in theory, the behavior shouldn't have changed > > without -dip1000, but I don't know. > > > > - Jonathan M Davis > > Yes, reduced code could be a bit better. > > @trusted doesn't throw scope checks away (and it wouldn't make > any sense since I don't see another way to make the code above > safe). Try: > > struct Container > { > } > > private Container* stuff(return scope ref Container container) > @trusted > { > return > } > > auto func() > { > Container container; > return stuff(container); > } > > It fails with -dip1000 and works without (as expected). > > "return scope ref" parameter in the constructor means, that the > constructed object has the same scope as the scope of the > argument. > > I just want to know whether the behaviour of 2.085 or 2.086 is > correct and if it is an "improvement" in 2.086, what I'm doing > wrong. scope is only checked in @safe code. If you use @trusted, it's not checked. At that point, it's up to you to make sure that no references escape, and taking the address of the scope variable and storing it is definitely escaping a reference to it. As I understand it, what you're trying to do is not something that works with scope. scope objects can be passed around, but they can't be stored like this. Unfortunately, the primary source of documentation for DIP 1000 is the DIP itself, and it was "superceded," meaning that the actual implementation does not match what's in the DIP, and I don't know how it differs. Unfortunately, for the most part, with DIP 1000, you just have to see what works, but I am very sure that using @trusted or @system means that the scope checks are off, and if you have to use @trusted to make something work with scope, then what you're trying to do doesn't work with scope. I can say however that if you can get the compiler to let you return a reference to a local variable like you're doing here with only @safe code (so, no @trusted), then it's a definitely a compiler bug. - Jonathan M Davis
Re: Conversion problem.
On Tuesday, 25 June 2019 at 16:44:28 UTC, H. S. Teoh wrote: On Tue, Jun 25, 2019 at 12:08:07PM +, Den_d_y via Digitalmars-d-learn wrote: Hello! Here I am again, with my problem ... In my program, I cannot manage to convert from "double" to "int". Here is the code: [...] Did you try this? import std.conv : to; double d = ...; int i = d.to!int; T This does not work. The program hangs at this stage, even the code you have proposed also does not work as we would like. Maybe I'm doing something wrong?