5

With using Ruby, we can do this.

s = "split by space"
A,B,C = s.split(" ").map(&:to_i)

With using D-lang, it's compile error.

string s = "split by space";
int A,B,C = s.split(" ").map!(x => x.to!int);

2 Answers 2

8

Jonathan is mostly right, but there is in fact a way to split a tuple into its constituent parts, albeit more verbose than in Ruby, and without any handy type inference:

import std.meta : AliasSeq;
import std.typecons : tuple;

auto foo() { return tuple(42, 29, "hello"); }

unittest {
   int a, b;
   string c;
   AliasSeq!(a, b, c) = foo(); // Look ma, magic!
   assert(a == 42);
   assert(b == 29);
   assert(c == "hello");
}

While there's no built-in way to do this with ranges like your example, it's possible to implement in a library:

import std.meta : AliasSeq, Repeat;
import std.typecons : Tuple, tuple;
import std.algorithm : map;
import std.conv : to;
import std.string : split;
import std.range : isInputRange, ElementType;

unittest {
   string s = "1 2 3";
   int A,B,C;
   AliasSeq!(A,B,C) = s.split(" ").map!(x => x.to!int).tuplify!3;
   assert(A == 1);
   assert(B == 2);
   assert(C == 3);
}

auto tuplify(size_t n, R)(R r) if (isInputRange!R) {
   Tuple!(Repeat!(n, ElementType!R)) result;

   static foreach (i; 0..n) {
      result[i] = r.front;
      r.popFront();
   }
   assert(r.empty);

   return result;
}
4
  • Splendid ! It works fine on dmd 2.076. If D-lang had this kind of feature, competitive-programmer would be happy. Anyway thank you.
    – hiropon
    Commented Nov 1, 2017 at 7:50
  • Instead of tuplify you should use import std.meta : aliasSeqOf;. Specifiying a number of required range elements is bad because it leads to coupling of code. It's annoying if you count wrong or you change a subtly which gives a larger range or your range has dynamic length, then the assert will go boom! Commented May 24, 2021 at 21:08
  • Problem only is aliasSeqOf works only with one specific argument because it takes an alias-template argument. If you try to pass a lambda argument into aliasSeqOf, it will fail with "... cannot be read at compile time". Commented May 24, 2021 at 21:47
  • @ChrisoLosoph There's quite a lot of cases where aliasSeqOf doesn't work. There's definitely also cases where it's better than tuplify, but try to make the unittest above work with aliasSeqOf without making s an enum value...
    – BioTronic
    Commented May 25, 2021 at 6:47
6

No, there is no way to do that. There has been talk off-and-on about possibly adding tuple support to the language such that you could something like

int a;
int b;
string c;
(a, b, c) = foo();

and maybe that will happen someday, but it's not possible right now. The closest would be using something like std.typecons.Tuple/tuple so that you can do something like

Tuple!(int, int, string) foo() { return tuple(42, 29, "hello"); }
Tuple!(int, int, string) result = foo();

or more likely

auto foo() { return tuple(42, 29, "hello"); }
auto result = foo();

but Tuple is ultimately just a struct, and you can't magically split it out at the other end. You have to access its members via indices such as result[0] or result[1], or if you declare Tuple with names - e.g. Tuple!(int, "x", int, "y", string, "str") - then you can access the members by name - e.g. result.x. So, Tuple/tuple allows you to return multiple values without explicitly declaring a struct type just for that, but it's still creating a struct type just for that, and while it allows you to easily pack values to return, it does not allow you to automatically unpack them on the other end. That would require compiler support of some kind that we don't have.

However, even if we had better tuple support in the language so that something like

(a, b, c) = foo();

worked, I doubt that what you're trying to do would work, since map specifically returns a range. So, it's an object with member functions, not a tuple of any kind to be split up. It just so happens to represent a list of values that can be extracted with the right set of function calls. And the number of values that it has is not known at compile time, so even if you assume that the compiler understands the range primitives well enough to get a list out of them for you, it can't guarantee at compile time that there are enough values to put into the variables you're trying to assign to, let alone that there are exactly that number of values. So, while it wouldn't be impossible to make something like that work (e.g. if it threw an Error at compile time if there weren't enough values in the range), I'd be surprised if that were implemented. D is a statically typed language and that would effectively be making a piece of it dynamic, so it would be pretty out-of-character for it to be in the language. Ruby is a dynamic language, so it's a very different beast.

Regardless, any improvements with tuples would be improvements to the language and would have to go through the DIP process and get approved, and nothing like that has happened yet.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.