From 7145693748de755747bd1195682d69245145cb61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sat, 23 Feb 2019 17:47:24 +0100 Subject: [PATCH] Work around name lookup issue on DMD < 2.081.0. --- source/taggedalgebraic/taggedunion.d | 44 +++++++++++++--------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/source/taggedalgebraic/taggedunion.d b/source/taggedalgebraic/taggedunion.d index bf1b080..b302e10 100644 --- a/source/taggedalgebraic/taggedunion.d +++ b/source/taggedalgebraic/taggedunion.d @@ -434,23 +434,27 @@ template visit(VISITORS...) /// unittest { - union U { - int number; - string text; + static if (__VERSION__ >= 2081) { + import std.conv : to; + + union U { + int number; + string text; + } + alias TU = TaggedUnion!U; + + auto tu = TU.number(42); + tu.visit!( + (int n) { assert(n == 42); }, + (string s) { assert(false); } + ); + + assert(tu.visit!((v) => to!int(v)) == 42); + + tu.setText("43"); + + assert(tu.visit!((v) => to!int(v)) == 43); } - alias TU = TaggedUnion!U; - - auto tu = TU.number(42); - tu.visit!( - (int n) { assert(n == 42); }, - (string s) { assert(false); } - ); - - assert(tu.visit!((v) => to!int(v)) == 42); - - tu.setText("43"); - - assert(tu.visit!((v) => to!int(v)) == 43); } unittest { @@ -480,14 +484,6 @@ unittest { } -// workaround for "template to is not defined" error in the unit test above -// happens on DMD 2.080 and below -private U to(U, T)(T val) { - static import std.conv; - return std.conv.to!U(val); -} - - /** The same as `visit`, except that failure to handle types is checked at runtime. Instead of failing to compile, `tryVisit` will throw an `Exception` if none