Anonymous function scope issue?

2023-12-06 Thread DLearner via Digitalmars-d-learn
e as a variable name in the main function (it would obviously be dangerous to do this deliberately, but this happened by accident). The first demo runs OK. The second fails with a 'circular reference' error. My question is simply why this is a problem at all - the scope of any names used i

Re: Is this a ctfe bugs ? ref scope const(ubyte)[32]

2023-09-06 Thread d007 via Digitalmars-d-learn
On Wednesday, 6 September 2023 at 12:15:02 UTC, Adam D Ruppe wrote: On Wednesday, 6 September 2023 at 12:04:40 UTC, d007 wrote: extern(C) int test(ref scope const(ubyte)[32] b); extern(C) int test(ref scope const(ubyte[32]) b); These are the same thing since the ref cannot be rebound anyway

Re: Is this a ctfe bugs ? ref scope const(ubyte)[32]

2023-09-06 Thread Adam D Ruppe via Digitalmars-d-learn
On Wednesday, 6 September 2023 at 12:04:40 UTC, d007 wrote: extern(C) int test(ref scope const(ubyte)[32] b); extern(C) int test(ref scope const(ubyte[32]) b); These are the same thing since the ref cannot be rebound anyway; a static array just is its contents.

Is this a ctfe bugs ? ref scope const(ubyte)[32]

2023-09-06 Thread d007 via Digitalmars-d-learn
```d extern(C) int test(ref scope const(ubyte)[32] b); ``` This will automatic become this in ctfe relection ```d extern(C) int test(ref scope const(ubyte[32]) b); ``` LDC2 1.34.0 DMD v2.104.2

Re: Scope guards

2023-06-26 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/26/23 2:42 PM, Cecil Ward wrote: == { size_t p = offset; ++p; scope(exit) { writeOutput( 0, p ); ++p … ++p; return; } == The correctness of its behaviour depends on what the value of p is when it calls writeOutput(), and the value of p is being changed. To be correct, the final value

Re: Scope guards

2023-06-26 Thread Cecil Ward via Digitalmars-d-learn
On Monday, 26 June 2023 at 17:41:16 UTC, Paul Backus wrote: On Saturday, 24 June 2023 at 17:00:36 UTC, Cecil Ward wrote: I would like to use scope guards but in the guard I need to get access to some local variables at the end of the routine. This doesn’t really seem to make sense as to how

Re: Scope guards

2023-06-26 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 24 June 2023 at 17:00:36 UTC, Cecil Ward wrote: I would like to use scope guards but in the guard I need to get access to some local variables at the end of the routine. This doesn’t really seem to make sense as to how it would work, because their values depend on the exact point

Re: pointer escaping return scope bug?

2022-12-18 Thread j via Digitalmars-d-learn
On Saturday, 19 November 2022 at 14:07:59 UTC, Nick Treleaven wrote: Hi, The following seems like a bug to me (reduced code, FILE* changed to int*): ```d @safe: struct LockedFile { private int* fps; auto fp() return scope => fps; } void main() { int* p; { auto

Re: pointer escaping return scope bug?

2022-12-15 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 15 December 2022 at 20:02:38 UTC, Nick Treleaven wrote: auto f() return @trusted => p ? p : v.ptr; Whoops, that can't be @trusted unless I `assert(p)`.

Re: pointer escaping return scope bug?

2022-12-15 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 19 November 2022 at 15:24:33 UTC, Dukc wrote: On Saturday, 19 November 2022 at 15:02:54 UTC, Nick Treleaven wrote: OK, so how do I make `lf` implicitly scope? Have the `int*` inside it to point to a local, or assign another `scope int*` to it. Thanks, this works: ```d @safe

Re: pointer escaping return scope bug?

2022-11-27 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 25 November 2022 at 15:03:57 UTC, ShadoLight wrote: I don't grok how `lf` can survive the local scope. Or am I missing something? Perhaps because the local scope is not pushed as a separate (anonymous) function on the stack... if true then, yes, then `lf` will indeed have the same

Re: pointer escaping return scope bug?

2022-11-25 Thread Tejas via Digitalmars-d-learn
On Friday, 25 November 2022 at 17:45:57 UTC, Paul Backus wrote: On Friday, 25 November 2022 at 14:07:28 UTC, ShadoLight wrote: On Saturday, 19 November 2022 at 15:00:16 UTC, Paul Backus wrote: Since, in your example, `lf` has global lifetime, the compiler deduces that `lf.fp` also has global

Re: pointer escaping return scope bug?

2022-11-25 Thread Paul Backus via Digitalmars-d-learn
On Friday, 25 November 2022 at 14:07:28 UTC, ShadoLight wrote: On Saturday, 19 November 2022 at 15:00:16 UTC, Paul Backus wrote: Since, in your example, `lf` has global lifetime, the compiler deduces that `lf.fp` also has global lifetime, and therefore there is nothing wrong with assigning it

Re: pointer escaping return scope bug?

2022-11-25 Thread ShadoLight via Digitalmars-d-learn
On Friday, 25 November 2022 at 14:07:28 UTC, ShadoLight wrote: On Saturday, 19 November 2022 at 14:07:59 UTC, Nick Treleaven ```d @safe: struct LockedFile { private int* fps; auto fp() return scope => fps; } void main() { int* p; { auto lf = LockedFile(new

Re: pointer escaping return scope bug?

2022-11-25 Thread ShadoLight via Digitalmars-d-learn
scope => fps; } void main() { int* p; { auto lf = LockedFile(new int); p = lf.fp; } assert(p != null); // address escaped } ``` There's no error with -dip1000. I'll file this unless I overlooked something. I think this is intended behavior, because you *do*

Re: pointer escaping return scope bug?

2022-11-19 Thread Dukc via Digitalmars-d-learn
On Saturday, 19 November 2022 at 15:02:54 UTC, Nick Treleaven wrote: On Saturday, 19 November 2022 at 14:52:23 UTC, ag0aep6g wrote: That's essentially just a function that returns its pointer parameter. So the program boils down to this: ```D @safe: int* fp(return scope int* p) { return p

Re: pointer escaping return scope bug?

2022-11-19 Thread ag0aep6g via Digitalmars-d-learn
On Saturday, 19 November 2022 at 15:02:54 UTC, Nick Treleaven wrote: OK, so how do I make `lf` implicitly scope? By explicit/implicit I just meant this: scope explicit = new int; int x; auto implicit = That's probably not helping with whatever you want to accomplish.

Re: pointer escaping return scope bug?

2022-11-19 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 19 November 2022 at 14:52:23 UTC, ag0aep6g wrote: That's essentially just a function that returns its pointer parameter. So the program boils down to this: @safe: int* fp(return scope int* p) { return p; } void main() { int* p; { auto lf = new int; p

Re: pointer escaping return scope bug?

2022-11-19 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 19 November 2022 at 14:07:59 UTC, Nick Treleaven wrote: Hi, The following seems like a bug to me (reduced code, FILE* changed to int*): ```d @safe: struct LockedFile { private int* fps; auto fp() return scope => fps; } void main() { int* p; { auto

Re: pointer escaping return scope bug?

2022-11-19 Thread ag0aep6g via Digitalmars-d-learn
On 19.11.22 15:07, Nick Treleaven wrote: Hi, The following seems like a bug to me (reduced code, FILE* changed to int*): ```d @safe: struct LockedFile {     private int* fps;     auto fp() return scope => fps; } void main() {     int* p;     {     auto lf = LockedFile(new

pointer escaping return scope bug?

2022-11-19 Thread Nick Treleaven via Digitalmars-d-learn
Hi, The following seems like a bug to me (reduced code, FILE* changed to int*): ```d @safe: struct LockedFile { private int* fps; auto fp() return scope => fps; } void main() { int* p; { auto lf = LockedFile(new int); p = lf.fp; } assert(p != n

Re: Making sense out of scope and function calls

2022-11-14 Thread Dukc via Digitalmars-d-learn
accomplished is this: ``` enum LowerCaseToken { u, l, c } struct Foo { @safe: int* dummy; string[int][LowerCaseToken] _headers; this(return ref int i) { dummy = } string[] getHeader(LowerCaseToken name) scope return { return _headers[name].values

Re: Making sense out of scope and function calls

2022-11-13 Thread 0xEAB via Digitalmars-d-learn
like the one here[0], just annoted with `scope` `scope return` etc. all over the place. I’m sorry, I don’t have any copy of that around anymore (as I’ve never commited it and already discarded my code + the idea of using `scope` – in its current state – in real world code). and I suspect

Re: Making sense out of scope and function calls

2022-11-13 Thread Dennis via Digitalmars-d-learn
On Sunday, 13 November 2022 at 19:06:40 UTC, 0xEAB wrote: Why does only the latter sample compile? The former leads to the following warning: Can you please provide a full example? I'm missing the definitions of _headers, hstring, values, and I suspect there's at least one `@safe` annotation

Re: Making sense out of scope and function calls

2022-11-13 Thread Imperatorn via Digitalmars-d-learn
On Sunday, 13 November 2022 at 19:06:40 UTC, 0xEAB wrote: ```d struct Foo { /* … */ hstring[] getHeader(LowerCaseToken name) scope return { return _headers[name].values; } [...] There's an old saying "you can't make sense out of scope"

Making sense out of scope and function calls

2022-11-13 Thread 0xEAB via Digitalmars-d-learn
```d struct Foo { /* … */ hstring[] getHeader(LowerCaseToken name) scope return { return _headers[name].values; } hstring[] getHeader(hstring name)() scope return { enum token = LowerCaseToken.makeConverted(name); return this.getHeader(token); // line

Re: return scope vs. scope return

2022-11-05 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 5 November 2022 at 16:13:18 UTC, 0xEAB wrote: Apparently there a difference between: - ```d Message withBody(Body body_) return scope { /* … */ } ``` - ```d Message withBody(Body body_) scope return { /* … */ } ``` ``` Deprecation: returning `this._body` escapes

return scope vs. scope return

2022-11-05 Thread 0xEAB via Digitalmars-d-learn
Apparently there a difference between: - ```d Message withBody(Body body_) return scope { /* … */ } ``` - ```d Message withBody(Body body_) scope return { /* … */ } ``` ``` Deprecation: returning `this._body` escapes a reference to parameter `this` perhaps change the `return

Re: auto scope question?

2022-10-25 Thread WhatMeWorry via Digitalmars-d-learn
typeof(screen.output.findSplit("")) s; Perfect. That was the "essence" of my question. But thanks to Ali, I don't have to use such esoteric syntax. D is a wonderful language, but I seem to shoot myself in the foot :)

Re: auto scope question?

2022-10-25 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/25/22 6:07 PM, WhatMeWorry wrote: I'm naturally getting a undefined identifier `s` error in the return. Is there some way to refactor my code?  I tried to declare s outside of the else brackets like: auto screen = executeShell(cmdLine); auto s; ... {     s =

Re: auto scope question?

2022-10-25 Thread Ali Çehreli via Digitalmars-d-learn
On 10/25/22 15:07, WhatMeWorry wrote: > auto screen = executeShell(cmdLine); > auto s; That can't work because there is no information to infer the type of 's'. Judging from the return type of getPath, perhaps it's string[]: string[] s; This is the question we should answer first: What

auto scope question?

2022-10-25 Thread WhatMeWorry via Digitalmars-d-learn
I'm naturally getting a undefined identifier `s` error in the return. Is there some way to refactor my code? I tried to declare s outside of the else brackets like: auto screen = executeShell(cmdLine); auto s; ... { s = screen.output.findSplit("REG_SZ"); } but that doesn't compile

Re: Any way to define variables from one scope in another scope?

2022-06-21 Thread bauss via Digitalmars-d-learn
On Monday, 20 June 2022 at 13:56:04 UTC, Ruby The Roobster wrote: Is there any way to define variables in an outer scope from an inner scope? I was thinking ```d void main() { int .y = 3; } ``` would work, but it doesn't. No and it would only lead to bugs. If you have access

Any way to define variables from one scope in another scope?

2022-06-20 Thread Ruby The Roobster via Digitalmars-d-learn
Is there any way to define variables in an outer scope from an inner scope? I was thinking ```d void main() { int .y = 3; } ``` would work, but it doesn't.

Re: dip1000 return scope dmd v 2.100

2022-05-07 Thread vit via Digitalmars-d-learn
On Friday, 6 May 2022 at 17:17:01 UTC, Dennis wrote: On Friday, 6 May 2022 at 09:24:06 UTC, vit wrote: [...] They were recently updated to match the implementation in 2.100. [...] `return scope` means pointer members (such `this.ptr`, `C.ptr`) may not escape the function, unless

Re: dip1000 return scope dmd v 2.100

2022-05-06 Thread Dennis via Digitalmars-d-learn
On Friday, 6 May 2022 at 09:24:06 UTC, vit wrote: It look like examples at page https://dlang.org/spec/function.html#ref-return-scope-parameters are no longer relevant. They were recently updated to match the implementation in 2.100. What difference are between `return scope`, `scope return

dip1000 return scope dmd v 2.100

2022-05-06 Thread vit via Digitalmars-d-learn
Hello, new dmd (2.100) has return/scope changes. It look like examples at page https://dlang.org/spec/function.html#ref-return-scope-parameters are no longer relevant. What difference are between `return scope`, `scope return` and `return`? Why `void* ptr` in struct change effect of `scope

Re: Lambdas Scope

2022-04-11 Thread Timon Gehr via Digitalmars-d-learn
On 11.04.22 11:11, Salih Dincer wrote: How is this possible? Why is it compiled? Don't the same names in the same scope conflict? ```d int function(int) square; void main() {   square = (int a) => a * a;   int square = 5.square;   assert(square == 25); } ``` Thanks, SDB@79 - Lo

Re: Lambdas Scope

2022-04-11 Thread user1234 via Digitalmars-d-learn
On Monday, 11 April 2022 at 09:11:06 UTC, Salih Dincer wrote: How is this possible? Why is it compiled? Don't the same names in the same scope conflict? ```d int function(int) square; void main() { square = (int a) => a * a; int square = 5.square; assert(square == 25); } ``` Thanks,

Lambdas Scope

2022-04-11 Thread Salih Dincer via Digitalmars-d-learn
How is this possible? Why is it compiled? Don't the same names in the same scope conflict? ```d int function(int) square; void main() { square = (int a) => a * a; int square = 5.square; assert(square == 25); } ``` Thanks, SDB@79

Re: scope variable `b` assigned to `a` with longer lifetime (-dip1000)

2022-04-09 Thread Dennis via Digitalmars-d-learn
On Saturday, 9 April 2022 at 10:39:33 UTC, vit wrote: Why doesn't this code compile? `proxySwap1` is lying about its attributes. It says `rhs` is `scope`, but it escapes by assignment `this.ptr = rhs.ptr;`. The compiler doesn't raise an error because it's marked `@trusted`. `proxySwap2

scope variable `b` assigned to `a` with longer lifetime (-dip1000)

2022-04-09 Thread vit via Digitalmars-d-learn
Hello, Why doesn't this code compile? ```d static struct Foo{ void *ptr; void proxySwap1(scope ref typeof(this) rhs)scope pure nothrow @trusted @nogc{ auto tmp = this.ptr; this.ptr = rhs.ptr; rhs.ptr = tmp; } void proxySwap2()(scope ref typeof

Re: How to create delegates with an independent scope?

2022-03-30 Thread Tejas via Digitalmars-d-learn
On Wednesday, 30 March 2022 at 12:46:07 UTC, Vijay Nayar wrote: Consider the following code example: ```d import std.stdio; void main() { alias DelegateT = string delegate(); // An array of delegates, each has their own scope. DelegateT[] funcs; foreach (i; ["ham"

Re: How to create delegates with an independent scope?

2022-03-30 Thread vit via Digitalmars-d-learn
On Wednesday, 30 March 2022 at 12:56:39 UTC, Vijay Nayar wrote: On Wednesday, 30 March 2022 at 12:53:10 UTC, vit wrote: use two delegates :) ```d (){ // Deliberately create a copy to keep in delegate scope. string myStr = i.dup

Re: How to create delegates with an independent scope?

2022-03-30 Thread Vijay Nayar via Digitalmars-d-learn
On Wednesday, 30 March 2022 at 12:53:10 UTC, vit wrote: use two delegates :) ```d (){ // Deliberately create a copy to keep in delegate scope. string myStr = i.dup; // The delegate will hopefully carry this copy around in its own scope

Re: How to create delegates with an independent scope?

2022-03-30 Thread vit via Digitalmars-d-learn
On Wednesday, 30 March 2022 at 12:46:07 UTC, Vijay Nayar wrote: Consider the following code example: ```d import std.stdio; void main() { alias DelegateT = string delegate(); // An array of delegates, each has their own scope. DelegateT[] funcs; foreach (i; ["ham"

How to create delegates with an independent scope?

2022-03-30 Thread Vijay Nayar via Digitalmars-d-learn
Consider the following code example: ```d import std.stdio; void main() { alias DelegateT = string delegate(); // An array of delegates, each has their own scope. DelegateT[] funcs; foreach (i; ["ham", "cheese"]) { // Deliberately create a copy to keep in delega

Re: error forward references if scope

2022-03-12 Thread Salih Dincer via Digitalmars-d-learn
On Saturday, 12 March 2022 at 13:12:25 UTC, vit wrote: ```d enum touch_T = __traits(hasMember, T, "touch"); ``` I think you meant build instead of touch? ```d struct Query { public const SharedPtr!Builder builder; } interface Builder { void build(ref Query query); } struct SharedPtr(T)

error forward references if scope

2022-03-12 Thread vit via Digitalmars-d-learn
`ref Query query` is scope then there is error? ```d struct Query{ public const SharedPtr!Builder builder; } interface Builder{ void build(scope ref Query query); //SCOPE } struct SharedPtr(T){ enum touch_T = __traits(hasMember, T, "touch"); //... } void main(){

Re: Scope with owner types

2021-09-23 Thread Kagamin via Digitalmars-d-learn
Yes, the `return` attribute is what should do it. You also need to compile the code with -dip1000 option.

Scope with owner types

2021-09-20 Thread Thomas Gregory via Digitalmars-d-learn
tly to c functions that accept `ctype *`. But how do I either: ensure `rc` stays in scope or have compile-time protection against escaping `ptr` past the lifetime of `rc`. Is this possible with either `scope` or `@live`? Could I add attributes to the C bindings to help with this?

Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 26 August 2021 at 19:31:54 UTC, DLearner wrote: if (typeof(v).stringof == "int" ) { Tip: you can instead of string of do if (is(typeof(v) == int)) That is operator lets you compare types directly. (stringof is something you will almost never use as you learn more of the

Re: Scope of Mixins

2021-08-26 Thread DLearner via Digitalmars-d-learn
On Thursday, 26 August 2021 at 16:28:22 UTC, Adam D Ruppe wrote: On Thursday, 26 August 2021 at 16:16:55 UTC, DLearner wrote: Please confirm that mixins of format: You really shouldn't use string mixins like this at all. If you want to work with a variable, pass the variable itself as an

Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 26 August 2021 at 18:07:48 UTC, Ali Çehreli wrote: In some cases it's more useful to have a 'static if' inside a single function template instead of two separate function templates. In most cases that's better. A template constraint is really a way to say "this template cannot

Re: Scope of Mixins

2021-08-26 Thread Ali Çehreli via Digitalmars-d-learn
On 8/26/21 10:45 AM, Adam D Ruppe wrote: On Thursday, 26 August 2021 at 17:39:16 UTC, Ali Çehreli wrote: String mixins are appealing because they can inject code like C macros do. It's not trivially possible to do the same with template mixins. Template mixins are great, but obviously totally

Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 26 August 2021 at 17:39:16 UTC, Ali Çehreli wrote: String mixins are appealing because they can inject code like C macros do. It's not trivially possible to do the same with template mixins. Template mixins are great, but obviously totally inappropriate here. I'm just talking

Re: Scope of Mixins

2021-08-26 Thread Ali Çehreli via Digitalmars-d-learn
On 8/26/21 10:06 AM, Adam D Ruppe wrote: On Thursday, 26 August 2021 at 17:01:06 UTC, DLearner wrote: The object was to take a variable, and do alternative things with it depending on (say) whether it was an 'int' or an 'int*'. That's *very* easy to do with the alias. You can just check

Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 26 August 2021 at 17:01:06 UTC, DLearner wrote: The object was to take a variable, and do alternative things with it depending on (say) whether it was an 'int' or an 'int*'. That's *very* easy to do with the alias. You can just check `typeof(v)` in there.

Re: Scope of Mixins

2021-08-26 Thread DLearner via Digitalmars-d-learn
On Thursday, 26 August 2021 at 16:28:22 UTC, Adam D Ruppe wrote: On Thursday, 26 August 2021 at 16:16:55 UTC, DLearner wrote: Please confirm that mixins of format: You really shouldn't use string mixins like this at all. If you want to work with a variable, pass the variable itself as an

Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 26 August 2021 at 16:16:55 UTC, DLearner wrote: Please confirm that mixins of format: You really shouldn't use string mixins like this at all. If you want to work with a variable, pass the variable itself as an argument to the function and use it with regular code instead of

Re: Scope of Mixins

2021-08-26 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 26 August 2021 at 16:16:55 UTC, DLearner wrote: Please confirm that mixins of format: ``` string mxn1(string VarName) { ... } ``` Invoked like: ``` mixin(mxn1("Var1")); ``` Have a wider scope than mixins like: ``` string mxn2(string VarName)() { ... } ``` In

Scope of Mixins

2021-08-26 Thread DLearner via Digitalmars-d-learn
Please confirm that mixins of format: ``` string mxn1(string VarName) { ... } ``` Invoked like: ``` mixin(mxn1("Var1")); ``` Have a wider scope than mixins like: ``` string mxn2(string VarName)() { ... } ``` Invoked like: ``` mixin(mxn2!"Var2"); ``` I tried direct r

Re: scope(exit) with expected library

2021-08-25 Thread WebFreak001 via Digitalmars-d-learn
On Wednesday, 25 August 2021 at 15:30:57 UTC, Steven Schveighoffer wrote: [...] Another approach is to let the compiler deal with the error handling and not muddy your return type. Swift does something similar, where it rewrites the throw/catch into a standard return and doesn't do actual

Re: scope(exit) with expected library

2021-08-25 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/25/21 10:58 AM, WebFreak001 wrote: Hm I'm not quite seeing how the error handler is related to an "Expected type interface" that the compiler could expect. This would be without compiler changes. Currently with exceptions the scope things are implemented using try-cat

Re: scope(exit) with expected library

2021-08-25 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 25 August 2021 at 14:42:07 UTC, Steven Schveighoffer wrote: I think it's possible to work with some mechanics that aren't necessarily desirable. Something like: ```d ErrorHandler error = registerErrorHandler; error.onFailure({writeln("division failed");});

Re: scope(exit) with expected library

2021-08-25 Thread WebFreak001 via Digitalmars-d-learn
On Wednesday, 25 August 2021 at 14:52:34 UTC, Steven Schveighoffer wrote: On 8/25/21 10:42 AM, Steven Schveighoffer wrote: I think it's possible to work with some mechanics that aren't necessarily desirable. Something like: One has to weigh how much this is preferred to actual exception

Re: scope(exit) with expected library

2021-08-25 Thread WebFreak001 via Digitalmars-d-learn
d be messy and likely brittle. Hm I'm not quite seeing how the error handler is related to an "Expected type interface" that the compiler could expect. Currently with exceptions the scope things are implemented using try-catch-finally, this would be even simpler: ```d scope(ex

Re: scope(exit) with expected library

2021-08-25 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/25/21 10:42 AM, Steven Schveighoffer wrote: I think it's possible to work with some mechanics that aren't necessarily desirable. Something like: One has to weigh how much this is preferred to actual exception handling... If something like DIP1008 could become usable, it might

Re: scope(exit) with expected library

2021-08-25 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/25/21 10:22 AM, Paul Backus wrote: On Wednesday, 25 August 2021 at 14:04:54 UTC, WebFreak001 wrote: Would it be possible to extend `scope(exit)` and `scope(success)` to trigger properly for functions returning `Expected!T` as defined in the [expectations](https://code.dlang.org/packages

Re: scope(exit) with expected library

2021-08-25 Thread WebFreak001 via Digitalmars-d-learn
On Wednesday, 25 August 2021 at 14:22:26 UTC, Paul Backus wrote: On Wednesday, 25 August 2021 at 14:04:54 UTC, WebFreak001 wrote: [...] Probably the only principled way to make this work would be to define some kind of "concept"/structural interface that's recognized by the compiler to mean

Re: scope(exit) with expected library

2021-08-25 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 25 August 2021 at 14:04:54 UTC, WebFreak001 wrote: Would it be possible to extend `scope(exit)` and `scope(success)` to trigger properly for functions returning `Expected!T` as defined in the [expectations](https://code.dlang.org/packages/expectations) and [expected](https

scope(exit) with expected library

2021-08-25 Thread WebFreak001 via Digitalmars-d-learn
Would it be possible to extend `scope(exit)` and `scope(success)` to trigger properly for functions returning `Expected!T` as defined in the [expectations](https://code.dlang.org/packages/expectations) and [expected](https://code.dlang.org/packages/expected) DUB libraries? For example

Re: Create alias of same name in inner scope, bug or feature?

2021-08-14 Thread Tejas via Digitalmars-d-learn
On Saturday, 14 August 2021 at 08:23:20 UTC, user1234 wrote: On Saturday, 14 August 2021 at 04:09:34 UTC, Tejas wrote: [...] Oh right, the ```.``` operator will reference variable in the _module_ scope, not just the _immediate outer scope_, you can use the module name to disambiguate as well

Re: Create alias of same name in inner scope, bug or feature?

2021-08-14 Thread user1234 via Digitalmars-d-learn
On Saturday, 14 August 2021 at 04:09:34 UTC, Tejas wrote: [...] Oh right, the ```.``` operator will reference variable in the _module_ scope, not just the _immediate outer scope_, you can use the module name to disambiguate as well. To extend Mike answer, the general rule is that if you can

Re: Create alias of same name in inner scope, bug or feature?

2021-08-13 Thread Tejas via Digitalmars-d-learn
() { writeln(macro_1(15, 20)); alias macro_1 = def;// is this NOT considered variable shadowing? writeln(macro_1(100, 20)); } ``` Shadowing local symbols is illegal. But it's okay for local symbols to have the same name as module-scope symbols. You can disambigbuate with [the module

Re: Create alias of same name in inner scope, bug or feature?

2021-08-13 Thread Mike Parker via Digitalmars-d-learn
;// is this NOT considered variable shadowing? writeln(macro_1(100, 20)); } ``` Shadowing local symbols is illegal. But it's okay for local symbols to have the same name as module-scope symbols. You can disambigbuate with [the module scope operator][1]: ```d void main() macro_1

Create alias of same name in inner scope, bug or feature?

2021-08-13 Thread Tejas via Digitalmars-d-learn
```d import std; auto abc(T)(auto ref T a, auto ref T b){ return a+b; } auto def(T)(auto ref T a, auto ref T b){ return a*b; } alias macro_1 = abc; void main() { writeln(macro_1(15, 20)); alias macro_1 = def;// is this NOT considered variable shadowing?

Re: Scope of enum

2021-07-11 Thread Adam D Ruppe via Digitalmars-d-learn
On Sunday, 11 July 2021 at 13:21:35 UTC, DLearner wrote: Is there a 'D' way of avoiding the issue? Pass the size as a parameter to the thing instead of trying to combine things. like mixin template Thing(size_t size) { ubyte[size] pool; } and where you want it like mixin

Re: Scope of enum

2021-07-11 Thread DLearner via Digitalmars-d-learn
On Sunday, 11 July 2021 at 12:47:48 UTC, Adam D Ruppe wrote: On Sunday, 11 July 2021 at 12:37:20 UTC, DLearner wrote: C:\Users\SoftDev\Documents\BDM\D\Examples\CTFE\T2>type k_mod.d // k_mod.d ubyte[MemSiz] MemPool; You didn't import the other module here. D's imports aren't like C's

Re: Scope of enum

2021-07-11 Thread Adam D Ruppe via Digitalmars-d-learn
On Sunday, 11 July 2021 at 12:37:20 UTC, DLearner wrote: C:\Users\SoftDev\Documents\BDM\D\Examples\CTFE\T2>type k_mod.d // k_mod.d ubyte[MemSiz] MemPool; You didn't import the other module here. D's imports aren't like C's includes. Each module is independent and can only see what it

Re: Scope of enum

2021-07-11 Thread DLearner via Digitalmars-d-learn
On Sunday, 11 July 2021 at 12:01:27 UTC, jfondren wrote: On Sunday, 11 July 2021 at 10:58:58 UTC, DLearner wrote: Is there a way of forcing DMD to extend the scope of `MemSiz` to include `k_mod`? Best regards ``` $ cat k_mod.d import test01; ubyte[MemSiz] MemPool; $ cat test01.d enum

Re: Scope of enum

2021-07-11 Thread jfondren via Digitalmars-d-learn
On Sunday, 11 July 2021 at 10:58:58 UTC, DLearner wrote: Is there a way of forcing DMD to extend the scope of `MemSiz` to include `k_mod`? Best regards ``` $ cat k_mod.d import test01; ubyte[MemSiz] MemPool; $ cat test01.d enum MemSiz = 240; void main() { import std.stdio, k_mod

Scope of enum

2021-07-11 Thread DLearner via Digitalmars-d-learn
has the enum `MemSiz`, but set to different values. However, DMD does not recognise the enum `MemSiz` within `k_mod`, failing with 'undefined identifier'. Is there a way of forcing DMD to extend the scope of `MemSiz` to include `k_mod`? Best regards

Re: anonymous functions and scope(exit)

2021-07-04 Thread frame via Digitalmars-d-learn
On Sunday, 4 July 2021 at 10:07:08 UTC, jfondren wrote: Not cleaning up after an Error is thrown is allowed by the D spec. This enhancement allows much better code to be generated for `nothrow` code when `scope` is used. It will also not unwind declarations with destructors in `nothrow` code

Re: anonymous functions and scope(exit)

2021-07-04 Thread Luis via Digitalmars-d-learn
) RangeError to exit without properly unwinding the stack. By that, what you're running into is an unpleasant interaction between 1. scope(exit)s that you're writing 2. Errors being thrown rather than Exceptions 3. anonymous functions getting inferred as nothrow I did https://issues.dlang.org

Re: anonymous functions and scope(exit)

2021-07-04 Thread jfondren via Digitalmars-d-learn
On Sunday, 4 July 2021 at 10:07:08 UTC, jfondren wrote: By that, what you're running into is an unpleasant interaction between 1. scope(exit)s that you're writing 2. Errors being thrown rather than Exceptions 3. anonymous functions getting inferred as nothrow And a resolution could

Re: anonymous functions and scope(exit)

2021-07-04 Thread jfondren via Digitalmars-d-learn
you're running into is an unpleasant interaction between 1. scope(exit)s that you're writing 2. Errors being thrown rather than Exceptions 3. anonymous functions getting inferred as nothrow And a resolution could be to submit an issue to make #1 prevent #3: if someone wants nothrow on an anonoymous

Re: anonymous functions and scope(exit)

2021-07-04 Thread Luis via Digitalmars-d-learn
On Saturday, 3 July 2021 at 22:52:39 UTC, frame wrote: On Saturday, 3 July 2021 at 22:04:04 UTC, Luis wrote: scope(exit) it's syntactic sugar for a classic `try {} finally {}` . The documentation says that must be executed. It works if you replace printf() with writeln() or use writeln

Re: anonymous functions and scope(exit)

2021-07-03 Thread frame via Digitalmars-d-learn
On Saturday, 3 July 2021 at 22:04:04 UTC, Luis wrote: scope(exit) it's syntactic sugar for a classic `try {} finally {}` . The documentation says that must be executed. It works if you replace printf() with writeln() or use writeln() after. There must be some buffer issue.

Re: anonymous functions and scope(exit)

2021-07-03 Thread Luis via Digitalmars-d-learn
or handler is found, execution resumes there. If not, the default Error handler is run, which displays the message and terminates the program. scope(exit) it's syntactic sugar for a classic `try {} finally {}` . The documentation says that must be executed.

Re: anonymous functions and scope(exit)

2021-07-03 Thread Luis via Digitalmars-d-learn
On Saturday, 3 July 2021 at 17:47:47 UTC, Dennis wrote: On Saturday, 3 July 2021 at 17:20:47 UTC, Luis wrote: scope(exit) inside of a anonymous functions, it's never called. I think the compiler infers the function `nothrow` since you don't throw any `Exception`, only an `Error`. Errors

Re: anonymous functions and scope(exit)

2021-07-03 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/3/21 4:08 PM, frame wrote: On Saturday, 3 July 2021 at 17:39:18 UTC, Steven Schveighoffer wrote: But in practice, the compiler does not have to clean up anything when an `Error` is thrown. Whether it does or not is defined by the implementation. This should be really mentionend in the

Re: anonymous functions and scope(exit)

2021-07-03 Thread frame via Digitalmars-d-learn
On Saturday, 3 July 2021 at 17:39:18 UTC, Steven Schveighoffer wrote: But in practice, the compiler does not have to clean up anything when an `Error` is thrown. Whether it does or not is defined by the implementation. This should be really mentionend in the docs? "Guard", yeah...

Re: anonymous functions and scope(exit)

2021-07-03 Thread Dennis via Digitalmars-d-learn
On Saturday, 3 July 2021 at 17:20:47 UTC, Luis wrote: scope(exit) inside of a anonymous functions, it's never called. I think the compiler infers the function `nothrow` since you don't throw any `Exception`, only an `Error`. Errors represent unrecoverable bugs, after which the program

Re: anonymous functions and scope(exit)

2021-07-03 Thread jfondren via Digitalmars-d-learn
On Saturday, 3 July 2021 at 17:20:47 UTC, Luis wrote: This is intentional ? ... scope(exit) inside of a anonymous functions, it's never called. ``` $ rdmd --eval 'iota(2).map!((int x) { scope(exit) writeln("got: ", x); return x+1; }).array.writeln' got: 0 got: 1 [1, 2] ```

Re: anonymous functions and scope(exit)

2021-07-03 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/3/21 1:20 PM, Luis wrote: This is intentional ? ```     should(function void() {     auto emptyStack = SimpleStack!int();     scope(exit) emptyStack.free; // <= This is never called     emptyStack.reserve(16);     emptyStack.top;     }).Th

Re: anonymous functions and scope(exit)

2021-07-03 Thread frame via Digitalmars-d-learn
On Saturday, 3 July 2021 at 17:20:47 UTC, Luis wrote: This is intentional ? ``` should(function void() { auto emptyStack = SimpleStack!int(); scope(exit) emptyStack.free; // <= This is never called emptyStack.reserve(16); emptyStack.

anonymous functions and scope(exit)

2021-07-03 Thread Luis via Digitalmars-d-learn
This is intentional ? ``` should(function void() { auto emptyStack = SimpleStack!int(); scope(exit) emptyStack.free; // <= This is never called emptyStack.reserve(16); emptyStack.top; }).Throw!RangeError; ``` scope(exit) ins

Re: cannot take address of scope local in safe function

2021-06-14 Thread ag0aep6g via Digitalmars-d-learn
On 13.06.21 19:49, vit wrote: Is possible create and use scope output range allocated on stack in @safe code? Example: ```d //-dip1000     struct OutputRange{     private bool valid = true;     private void* ptr;     int count = 0;     void put(Val)(auto ref scope Val

Re: cannot take address of scope local in safe function

2021-06-13 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 13 June 2021 at 17:49:50 UTC, vit wrote: Is possible create and use scope output range allocated on stack in @safe code? Example: ```d //-dip1000 struct OutputRange{ private bool valid = true; private void* ptr; int count = 0; void put(Val

  1   2   3   4   5   6   7   >