Re: question as to when a new command gets executed
On 2020-11-11 06:29, WhatMeWorry wrote: Which begs the question, how would the statement, m_State = new BreakState() ever get executed? class DebuggerSession { private BreakState m_State = new BreakState(); private UnrealCallback m_UnrealCallback; this( ) { } // rest of class } It gets executed at compile time. All instances of `DebuggerSession` will share the same single instance of `BreakState`. -- /Jacob Carlborg
question as to when a new command gets executed
I've been studying an 8 year old D project in Github and this code fragment has left me befuddled. I'm confused as to when and how the new BreakState() statement gets executed. Wouldn't the class DebuggerSession need to be instantiated first and then the this() constructor be called? Which begs the question, how would the statement, m_State = new BreakState() ever get executed? class DebuggerSession { private BreakState m_State = new BreakState(); private UnrealCallback m_UnrealCallback; this( ) { } // rest of class }
Re: Unclear error message
On Wednesday, 11 November 2020 at 02:05:33 UTC, H. S. Teoh wrote: Definitely. Bad/confusing error messages should always be improved. Please file a bug at: http://issues.dlang.org/ T https://issues.dlang.org/show_bug.cgi?id=21377 I wonder if this is the same as: https://issues.dlang.org/show_bug.cgi?id=21003 I filed it either way, just in case.
Re: Unclear error message
On Wed, Nov 11, 2020 at 01:05:21AM +, SealabJaster via Digitalmars-d-learn wrote: > Please see the code at https://run.dlang.io/is/Yjidek [Quoting code in full here for future reference] > struct PreValidate > { > alias FuncT = bool delegate(string arg); > > FuncT func; > > this(FuncT func) > { > this.func = func; > } > > bool onPreValidate(string arg) > { > return this.func(arg); > } > } > > // OK > @PreValidate(str => str.length == 3) > int i; > > void main() > { > // OK > auto v = PreValidate(str => str.length == 3); > } > > struct S > { > // ERROR? > @PreValidate(str => str.length == 3) > int a; > } > As I understand the error is caused by trying to provide a delegate > when there's no context to provide. Not complaining about that. Is this even a valid error? The UDA works in module scope, where there *isn't* any local context, yet it's accepted, but here, in a struct, it's not accepted. I'm not 100% but this looks like a bug. > However what I am complaining about is about the error message: > `onlineapp.d(31): Error: delegate onlineapp.S.__lambda2 cannot be > struct members` > > I'm not sure if it's just me, but that error message makes absolutely > no sense to me. Should that message be improved? Definitely. Bad/confusing error messages should always be improved. Please file a bug at: http://issues.dlang.org/ T -- Unix was not designed to stop people from doing stupid things, because that would also stop them from doing clever things. -- Doug Gwyn
Re: Unclear error message
On Wednesday, 11 November 2020 at 01:05:21 UTC, SealabJaster wrote: Please see the code at https://run.dlang.io/is/Yjidek As I understand the error is caused by trying to provide a delegate when there's no context to provide. Not complaining about that. However what I am complaining about is about the error message: `onlineapp.d(31): Error: delegate onlineapp.S.__lambda2 cannot be struct members` I'm not sure if it's just me, but that error message makes absolutely no sense to me. Should that message be improved? Yeah, that message is really bad. The actual problem is that the compiler isn't able to figure out the type of the lambda you've provided. If you change the argument to `(string str)`, it'll work. The real question is, why does type inference fail for the UDA when it works for the normal constructor call?
Unclear error message
Please see the code at https://run.dlang.io/is/Yjidek As I understand the error is caused by trying to provide a delegate when there's no context to provide. Not complaining about that. However what I am complaining about is about the error message: `onlineapp.d(31): Error: delegate onlineapp.S.__lambda2 cannot be struct members` I'm not sure if it's just me, but that error message makes absolutely no sense to me. Should that message be improved?
Re: Unicode Regular Expressions
On Monday, 9 November 2020 at 17:06:50 UTC, H. S. Teoh wrote: I'm pretty sure std.regex already supports Unicode regexes. Yep [1]. Thanks [1] https://dlang.org/phobos/std_regex.html#Syntax%20and%20general%20information
Re: Generic comparison
On Tuesday, 10 November 2020 at 20:32:40 UTC, Paul Backus wrote: The compiler infers pure, @nogc, nothrow etc. for template functions automatically. It's actually better if you don't add them by hand. Ok, thanks :-).
Re: Generic comparison
On Tuesday, 10 November 2020 at 20:32:40 UTC, Paul Backus wrote: alias isOrderingComparableWith(T, U) = __traits(compiles, (T t, U u) => t < u); My bad, should be `enum`, not `alias`.
Re: Generic comparison
On Tuesday, 10 November 2020 at 17:19:09 UTC, Ola Fosheim Grøstad wrote: Interesting, so "auto ref T" is the go-to type specifier for generic code then? I guess I also should conditionally add things like pure, nogc, nothrow... I assume I would have to test the comparison operator. I actually want to implement The compiler infers pure, @nogc, nothrow etc. for template functions automatically. It's actually better if you don't add them by hand. (low <= value) && (value < high) So I guess I need to test both. But how...? compiles-trait? You could add a template constraint, if you wanted. Something like: alias isOrderingComparableWith(T, U) = __traits(compiles, (T t, U u) => t < u); bool between(Value, Bound)(...) if (isOrderingComparaibleWith!(Value, Bound)) For a function this short, though, I don't think it's really necessary.
Re: How to get address of a nested function?
On Tue, Nov 10, 2020 at 11:55 AM Max Samukha via Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote: > We can get the compile time equivalent of a member function's > address by applying '&' to the function in a static context: > > struct S { > void foo() {} > } > > enum pfoo = &S.foo; // ok > > void main() { > // now we can use the pointer to create, for example, a > delegate > S s; > void delegate() dg; > dg.ptr = &s; > dg.funcptr = pfoo; > dg(); > } > > However, we can't do that to a nested function: > > void main() { > void foo() { > } > enum pfoo = &foo; // weird kind of an enum delegate; > pfoo.funcptr can't be accessed at compile time. > } > > Is there a way to get a pointer to a non-static nested function? > non static nested function is a delegate, so you can just assign it to delegate like I have posted or you can du this: import std.stdio; void main() { void foo() { writeln("It works as expected"); } enum pfoo = &foo; void delegate() dg; dg.ptr = pfoo.ptr; dg.funcptr = pfoo.funcptr; dg(); }
Re: How to get address of a nested function?
On Tue, Nov 10, 2020 at 8:50 PM Max Samukha via Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote: > On Tuesday, 10 November 2020 at 14:36:04 UTC, Steven > Schveighoffer wrote: > > >> > >> Is there a way to get a pointer to a non-static nested > >> function? > > > > I don't think you can do it at compile time. You can at runtime > > by accessing the funcptr of the delegate. > > > > -Steve > > Thanks for the reply. I will post the issue to bugzilla. > Why? It works for me import std.stdio; void main() { void foo() { writeln("It works as expected"); } enum pfoo = &foo; void delegate() dg = pfoo; dg(); }
Re: How to get address of a nested function?
On Tuesday, 10 November 2020 at 14:36:04 UTC, Steven Schveighoffer wrote: Is there a way to get a pointer to a non-static nested function? I don't think you can do it at compile time. You can at runtime by accessing the funcptr of the delegate. -Steve Thanks for the reply. I will post the issue to bugzilla.
Re: Generic comparison
On Tuesday, 10 November 2020 at 17:09:00 UTC, Paul Backus wrote: bool between(Value, Bound)(auto ref Value value, auto ref Bound low, auto ref Bound high) { return (low < value) && (value < high); } You need `auto ref` because either Bound or Value may have copying disabled. Because the function is a template, attributes like `scope` will be inferred when applicable (modulo compiler bugs). Interesting, so "auto ref T" is the go-to type specifier for generic code then? I guess I also should conditionally add things like pure, nogc, nothrow... I assume I would have to test the comparison operator. I actually want to implement (low <= value) && (value < high) So I guess I need to test both. But how...? compiles-trait?
Re: Generic comparison
On Tuesday, 10 November 2020 at 16:55:11 UTC, Ola Fosheim Grøstad wrote: I want to implement a generic function for "a < f(x) < b" that will give the same result as "(a < f(x)) && (f(x) < b)" for any conceivable mix of types. Except if that "f(x)" should only be evaluated once. Is it sufficient to use "scope ref" in parameters? I don't want to assume _anything_ about the definition of the types and the implementation of the comparison operator (can be overloaded). bool between(Value, Bound)(auto ref Value value, auto ref Bound low, auto ref Bound high) { return (low < value) && (value < high); } You need `auto ref` because either Bound or Value may have copying disabled. Because the function is a template, attributes like `scope` will be inferred when applicable (modulo compiler bugs).
Generic comparison
I want to implement a generic function for "a < f(x) < b" that will give the same result as "(a < f(x)) && (f(x) < b)" for any conceivable mix of types. Except if that "f(x)" should only be evaluated once. Is it sufficient to use "scope ref" in parameters? I don't want to assume _anything_ about the definition of the types and the implementation of the comparison operator (can be overloaded).
Re: How to get address of a nested function?
On 11/10/20 5:51 AM, Max Samukha wrote: We can get the compile time equivalent of a member function's address by applying '&' to the function in a static context: struct S { void foo() {} } enum pfoo = &S.foo; // ok void main() { // now we can use the pointer to create, for example, a delegate S s; void delegate() dg; dg.ptr = &s; dg.funcptr = pfoo; dg(); } However, we can't do that to a nested function: void main() { void foo() { } enum pfoo = &foo; // weird kind of an enum delegate; pfoo.funcptr can't be accessed at compile time. } Is there a way to get a pointer to a non-static nested function? I don't think you can do it at compile time. You can at runtime by accessing the funcptr of the delegate. -Steve
Re: canFind all elements in a array.
On Tuesday, 10 November 2020 at 09:47:06 UTC, sarn wrote: On Tuesday, 10 November 2020 at 08:19:15 UTC, Vino wrote: [...] This is iterating over all the elements in data2 and outputting some of them, so the output will never be longer than data2. [...] Hi Sarn, Thank you very much
Re: Disallow implicit "conversion" from alias-types
On Tuesday, 10 November 2020 at 11:49:19 UTC, Jerry wrote: On Tuesday, 10 November 2020 at 11:38:30 UTC, Vladimirs Nordholm wrote: Hello. I am unsure if I am going about this the right way, and if my question even makes sense. In essence what I want is to have two "types" represented by a size_t. Here is an example of what I want think I want (but might be completely off) alias Foo = size_t; alias Bar = size_t; Foo foo = 4; Bar bar = foo; // i want some error like // "cannot implicitly convert from type Foo to Bar" My best solution is to have the types as classes to force type checking. Is there a better way to do what I want here? https://dlang.org/library/std/typecons/typedef.html This is exactly what I need. Thanks!
Re: Disallow implicit "conversion" from alias-types
On Tuesday, 10 November 2020 at 11:38:30 UTC, Vladimirs Nordholm wrote: Hello. I am unsure if I am going about this the right way, and if my question even makes sense. In essence what I want is to have two "types" represented by a size_t. Here is an example of what I want think I want (but might be completely off) alias Foo = size_t; alias Bar = size_t; Foo foo = 4; Bar bar = foo; // i want some error like // "cannot implicitly convert from type Foo to Bar" My best solution is to have the types as classes to force type checking. Is there a better way to do what I want here? https://dlang.org/library/std/typecons/typedef.html
Disallow implicit "conversion" from alias-types
Hello. I am unsure if I am going about this the right way, and if my question even makes sense. In essence what I want is to have two "types" represented by a size_t. Here is an example of what I want think I want (but might be completely off) alias Foo = size_t; alias Bar = size_t; Foo foo = 4; Bar bar = foo; // i want some error like // "cannot implicitly convert from type Foo to Bar" My best solution is to have the types as classes to force type checking. Is there a better way to do what I want here?
How to get address of a nested function?
We can get the compile time equivalent of a member function's address by applying '&' to the function in a static context: struct S { void foo() {} } enum pfoo = &S.foo; // ok void main() { // now we can use the pointer to create, for example, a delegate S s; void delegate() dg; dg.ptr = &s; dg.funcptr = pfoo; dg(); } However, we can't do that to a nested function: void main() { void foo() { } enum pfoo = &foo; // weird kind of an enum delegate; pfoo.funcptr can't be accessed at compile time. } Is there a way to get a pointer to a non-static nested function?
Re: canFind all elements in a array.
On Tuesday, 10 November 2020 at 08:19:15 UTC, Vino wrote: foreach(i; data2[]) { if(data1[].canFind(i[0])) { writeln(i[1]); } } This is iterating over all the elements in data2 and outputting some of them, so the output will never be longer than data2. It looks like you want to iterate over data1. Something like this: foreach(i; data1[]) { auto result = data2[].find!((p, x) => p[0] == x)(i); if (!result.empty) writeln(result.front[1]); } However, you could also use an associative array for data2: string[string] data2 = [ "DEV Systems": "DEV Cluster", "QAS Systems": "QAS Cluster", ]; foreach (i; data1[]) { if (auto v = i in data2) writeln(*v); } The "in" operator returns a pointer to the value in data2 at index "i", or else a null pointer. See more info here: https://ddili.org/ders/d.en/aa.html https://dlang.org/spec/hash-map.html
canFind all elements in a array.
Hi All, Request your help, the below code output's as below hence request your help on hot to get the output as below(Required Output). Output DEV Cluster QAS Cluster Required Output DEV Cluster DEV Cluster DEV Cluster QAS Cluster Code import std.container.array; import std.stdio: writeln; import std.algorithm: canFind; import std.typecons: Tuple, tuple; void main () { Array!string data1; Array!(Tuple!(string,string)) data2; Array!string rs; data1.insertBack("DEV Systems"); data1.insertBack("DEV Systems"); data1.insertBack("DEV Systems"); data1.insertBack("QAS Systems"); data2.insertBack(tuple("DEV Systems","DEV Cluster")); data2.insertBack(tuple("QAS Systems","QAS Cluster")); foreach(i; data2[]) { if(data1[].canFind(i[0])) { writeln(i[1]); } } } From, Vino.B