Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get!T does not work when T is a class with non-inout constructor #267

Closed
tom-tan opened this issue May 14, 2021 · 1 comment · Fixed by #268
Closed

get!T does not work when T is a class with non-inout constructor #267

tom-tan opened this issue May 14, 2021 · 1 comment · Fixed by #268

Comments

@tom-tan
Copy link
Contributor

tom-tan commented May 14, 2021

The following code should work without errors but it does not.
Here is the same code in run.dlang.io: https://run.dlang.io/is/7bHmjV

/+ dub.sdl:
dependency "dyaml" version="~>0.8.3"
+/
import dyaml : Loader, Node;
import std : split, to;

class MyClass
{
    int x, y, z;

    this(Node node)
    {
        auto parts = node.as!string().split(":");
        x = parts[0].to!int;
        y = parts[1].to!int;
        z = parts[2].to!int;
    }
}

void main()
{
	auto loader = Loader.fromString(`"1:2:3"`);
	Node node = loader.load();
	auto mc = node.as!MyClass;
}

It fails to compile with the following error message:

onlineapp.d(24,16): Error: template instance `dyaml.node.Node.as!(MyClass)` does not match template declaration `get(T, Flag stringConversion = Yes.stringConversion)()`
  with `T = onlineapp.MyClass`
  must satisfy one of the following constraints:
`       allowed!(Unqual!T)
       hasNodeConstructor!(Unqual!T)`
dmd failed with exit code 1.

It works when inout is added to the constructor.
However, in the most cases, using inout constructor does not work as intended because it creates only const objects unless the constructor is used in inout functions (See https://run.dlang.io/is/9zhOL8).

@tom-tan
Copy link
Contributor Author

tom-tan commented May 14, 2021

FYI:

D-YAML includes a unittest using an inout constructor (line 729 in node.d).
I guess using a constructor that can create a unique object is more appropriate for the constructor for any qualifiers.

However, it seems to be a difficult task because Node.get cannot be pure (it is used in MyClass.this) due to std.variant.Algebraic.type. Using assumePure and @trusted can make Node.get pure but this solution makes more difficult to solve #33.

Using std.sumtype that will be included in 2.097 instead of Algebraic may solve this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant