Re: How to find all modules in a package?

2022-08-03 Thread Piotr Mitana via Digitalmars-d-learn

On Wednesday, 3 August 2022 at 03:36:55 UTC, Domain wrote:
I want to find out all public functions in all modules in a 
package. Can I do that at compile time?


I think it's not possible.


Re: Return values from auto function

2020-11-07 Thread Piotr Mitana via Digitalmars-d-learn

On Friday, 6 November 2020 at 10:51:20 UTC, Andrey Zherikov wrote:


struct Result(T)
{
struct Success
{
static if(!is(T == void))
{
T value;
}
}
struct Failure
{
string error;
}

Algebraic!(Success, Failure) result;

this(Success value)
{
result = value;
}
this(Failure value)
{
result = value;
}
}
auto success(T)(T value)
{
return Result!T(Result!T.Success(value));
}
auto failure(string error)
{
return Result!void(Result!void.Failure(error));
}

auto f(int i)
{
return i > 0 ? success(i) : failure("err text"); // Error: 
incompatible types for (success(i)) : (failure("err text")): 
Result!int and Result!void

}


I'd go with:

struct Result(T)
{
Nullable!string error;

static if(!is(T == void))
{
T value;

static Result!T success(T value)
{
return Result!T(Nullable!string.init, value);
}

static Result!T failure(string error)
{
return Result!T(nullable(error), T.init);
}
}

else
{
static Result!T success()
{
return Result!T(Nullable!string.init);
}

static Result!T failure(string error)
{
return Result!T(nullable(error));
}
}

bool isSuccess()
{
return error.isNull();
}
}

// Demo
Result!int intFun(int i)
{
return i > 0 ? Result!int.success(i) : Result!int.failure("Be 
positive!");

}

Result!void voidFun(int i)
{
return i > 0 ? Result!void.success() : 
Result!void.failure("Be positive!");

}

void main()
{
auto intFunRes = intFun(5);

if(intFunRes.isSuccess)
writefln("Result: %d", intFunRes.value);
else
writefln("ERROR: %s", intFunRes.error.get);
}

Does this satisfy your needs?


Re: What kind of Editor, IDE you are using and which one do you like for D language?

2019-12-30 Thread Piotr Mitana via Digitalmars-d-learn

On Sunday, 22 December 2019 at 17:20:51 UTC, BoQsc wrote:
There are lots of editors/IDE's that support D language: 
https://wiki.dlang.org/Editors


What kind of editor/IDE are you using and which one do you like 
the most?


IntelliJ IDEA CE with this extension: 
https://intellij-dlanguage.github.io/


Wow, nobody else uses this?


Re: Cannot take the .keys of shared AA. Is this a regression in 2.087 or a feature?

2019-08-16 Thread Piotr Mitana via Digitalmars-d-learn
On Thursday, 15 August 2019 at 19:51:30 UTC, Jonathan M Davis 
wrote:
Not being able to implicitly convert to const is a bit odd, but 
arguably, nothing should ever be called on a shared AA anyway. 
If an operation isn't thread-safe, then it shouldn't work with 
shared. To use a shared object safely, you have to protect 
access to it with a mutex or some other synchronization 
mechanism, after which you would normally cast away shared to 
operate on the object as thread-local while the lock is in 
place and then release the lock when you're done (also making 
sure that no thread-local references exist when the lock is 
released). Because keys is not at all thread-safe, I'd strongly 
argue that it should not work on a shared AA, and if it does, 
that's a bug.


OK, I get the point. So I should go with something similar to 
this, right?


import core.sync.mutex;
import std;

shared(string[string]) dict;
shared(Mutex) mtx;

shared static this()
{
mtx = new shared Mutex;
}

void main()
{
mtx.lock;
(cast(string[string]) dict).keys;
mtx.unlock;
}

Or I could use synchronized, if dict was inside a class. Thank 
you!


Cannot take the .keys of shared AA. Is this a regression in 2.087 or a feature?

2019-08-15 Thread Piotr Mitana via Digitalmars-d-learn

Code:

import std;

shared(string[string]) dict;

void main()
{
dict.keys;
}

Error:

/dlang/dmd/linux/bin64/../../src/druntime/import/object.d(3417): 
Error: cannot implicitly convert expression aa of type 
shared(string[string]) to const(shared(string)[string])
onlineapp.d(7): Error: template instance 
`object.keys!(shared(string[string]), shared(string), string)` 
error instantiating


Before D 2.087 it compiled - is this a regression?


Re: Is there a function for this?

2018-10-08 Thread Piotr Mitana via Digitalmars-d-learn

On Saturday, 6 October 2018 at 13:17:22 UTC, bauss wrote:
Let's say you have a range with struct, but some of the struct 
are duplicates of each other.


Is there a standard function in Phobos to remove duplicates?

My first thought was "uniq", but it can't really do it like 
that, but it doesn't work.


See: https://run.dlang.io/is/IcFEtw

Is there another function in Phobos that perhaps would work?

I can of course write my own function, but if there is a 
standard function that can do it, then I'd rather use that.


Unless you want to implement opCmp() and do the .sort.uniq then, 
you may find this one-liner helpful:


randomAccessRange.fold!((arr, elem) => arr.canFind(elem) ? arr : 
(arr ~= elem))((ElementType!(typeof(randomAccessRange))[]).init)


Applied in your example: https://run.dlang.io/is/3KjRvY

But yes, it is de facto a custom function. I don't think that 
there is a function in Phobos that will perfectly deduplicate 
your range without having it sorted before.


@safe - why does this compile?

2018-07-13 Thread Piotr Mitana via Digitalmars-d-learn

This code:

import std.stdio;

class X1 {}
class X2 : X1
{
void run() @safe
{
writeln("DONE");
}
}

void main() @safe
{
X1 x1 = new X1;
X2 x2 = cast(X2) x1;
x2.run();
}

is obviously wrong gets killed by OS's signal. Why is it @safe? I 
thought @safe should prevent such errors as well.


Re: vibe.d: problematic "Non-@safe methods are deprecated in REST interfaces"

2018-07-11 Thread Piotr Mitana via Digitalmars-d-learn

On Tuesday, 10 July 2018 at 13:24:43 UTC, WebFreak001 wrote:
It's supposed to make webservers safe and not crash because of 
segmentation faults, etc.


If you still want to write code like you are used to and don't 
care about that in your webserver, just mark everything in the 
implementation @trusted (but @safe in the interface) and it 
will be fine.


I understand the motivation of this and this motivation is 
undoubtly correct.


The problem is when you use the libraries, especially those 
interfacing with C code. The intention of @trusted is to use it 
to mark the code that *is* memory safe, but it cannot be verified 
automatically by the compiler (for example required checks are 
done before an array access).


That's why there is a problem with the libraries that are *not* 
safe - or at least I don't know the code and cannot verify that 
they are.


vibe.d: problematic "Non-@safe methods are deprecated in REST interfaces"

2018-07-10 Thread Piotr Mitana via Digitalmars-d-learn

Hello,

I've recently started building a little REST application on 
vibe.d. I decided to use the "database" library, as I need to 
communicate with the PostgreSQL instance.


During the compilation I see the deprecation warning:
"Non-@safe methods are deprecated in REST interfaces"

So two questions rise:

1. Will non-@safe methods in REST be disallowed someday?
2. Isn't it too restrictive? If it IS to be disallowed someday, I 
just won't be able to call most libraries from REST, as I expect 
the database library for example is definitely not @safe and 
cannot be.


If I post in the wrong forum, please drop me a line where should 
I post instead.


Package method is not virtual and cannot override - a bug or a feature?

2018-05-10 Thread Piotr Mitana via Digitalmars-d-learn

Given this code:

abstract class A
{
package @property void x(int x);
package @property int x();
}

class B : A
{
package @property override void x(int x) {}
package @property override int x() { return 0; }
}

void main() {}

I get the following message:

onlineapp.d(9): Error: function `onlineapp.B.x` package method is 
not virtual and cannot override
onlineapp.d(10): Error: function `onlineapp.B.x` package method 
is not virtual and cannot override


Why is that? If the access specifier is private, I can perfectly 
understand it - subclasses can't call the private method of 
parent class, so there's no need in making it virtual. For 
protected/public the code compiles. However, for the package 
protection it may happen that a subclass is in the same package 
(and just happened to me).


Should I file a bug or is there a reason for such behavior?


Slide - what does withPartial do?

2018-03-01 Thread Piotr Mitana via Digitalmars-d-learn

For some reason this is true:

slide!(Yes.withPartial)([1, 2, 3, 4, 5], 3).array == [[1, 2, 3], 
[2, 3, 4], [3, 4, 5]]


Shouldn't it rather return [[1], [1, 2], [1, 2, 3], [2, 3, 4], 
[3, 4, 5], [4, 5], [5]], or at least [[1, 2, 3], [2, 3, 4], [3, 
4, 5], [4, 5], [5]]?


I can see no difference on the result when withPartial is on and 
off.


Is it a bug or I don't understand it correctly?


getSymbolsByUDA does not take private symbols under consideration. Should I file a bug?

2018-02-16 Thread Piotr Mitana via Digitalmars-d-learn

Hello,

The code below:


import std.traits;

enum Attr;

class MyClass
{
private @Attr int a;
static assert(getSymbolsByUDA!(typeof(this), MyClass).length 
== 1);

}


does not compile as static assertion fails. Making the filed a 
public makes it compile properly. Should I file a bug or is by 
design?


One path skips constructor - is this a bug?

2017-09-07 Thread Piotr Mitana via Digitalmars-d-learn

Code:

===
import std.conv;
import std.regex;

struct A
{
int field1;
int field2;

this(int field1, int field2)
{
if(field1 > field2)
throw new Exception("This is illegal!");
}

this(string str)
{
if(str.match(ctRegex!(`^[0-9]{4}-[0-9]{2}`)))
this(str[0..4].to!int, str[5..7].to!int);
else
throw new Exception("Invalid string");
}
}

void main()
{
A(2004, 43);
A("2004-43");
}
===

This throws a compilation error:

main.d(17): Error: one path skips constructor
main.d(15): Error: return without calling constructor

Why do I need the constructor call, if I throw the exception 
anyway? Is this a bug?


Base class' constructor is not implicitly inherited for immutable classes. A bug or a feature?

2017-07-19 Thread Piotr Mitana via Digitalmars-d-learn

Hello, I have this code:

immutable class Base
{
this() {}
}

immutable class Derived : Base {}

void main()
{
new immutable Derived();
}

I'd like class Derived to automatically inherit the default 
constructor from Base. However, this is not the case:


main.d(6): Error: class main.Derived cannot implicitly generate a 
default ctor when base class main.Base is missing a default ctor


Is it a bug or it should be like this?