Builtin regex (Was: How to complex switch?)

Timon Gehr timon.gehr at gmx.ch
Wed May 18 15:12:24 PDT 2011


> Timon Gehr:
>
> > Library code should make use of the language to implement its semantics.
> > Not the other way round.
>
> Why?

Short story: Obvious?

Do you really want to make some non-built-in types more equal than others? Why
would you want to have a dependency cycle between std and the compiler?

Longer story: You get all the drawbacks of a library-based solution except literal
syntax (a one-character alias for tuple can already make up much of it without
work from the compiler so your benefit is basically zero).

But you want improved return type deduction, many will want tuple assignments in
the style (a,b)=(b-a,a+b), etc. tuple cannot do it. Therefore the part the
compiler takes will get so large that in the end the only relict of the library
stuff will be that you have to import std.typecons; or else you will get some
strange errors that do not at all look like your code. (Or even more silly, the
compiler will directly nag you "Did you forget to import std.typecons?"). The
question is, do you want to import typecons or not. It is just that and
furthermore a question of clean design.

How tuples are handled internally should be a compiler/runtime thing, not a
standard library thing. I would be fine with Tuple!(T) defining conversions from
and to the built-in tuple.

>
>
> > Another reason I dislike it: it looks different to other "tuple literals"
> > that are already built-in:
> >
> > foo (note, how, this, is, a, tuple, "!");
>
> Looking different from the built-in ones was the point of it, to avoid clashes.

No. There is no way it could clash with foo (a,b,c).

The only use case where the straightforward solution would clash is something like
this:

auto a=(i++,j++,k++);

Nobody who is sane writes such code!

>
>
> > Still, I think (|1,2,3|) is not optimal.
>
> Please, invent better things :-)

1. Deprecate using CommaExpression inside ParanthesizedExpression. (This does not
affect most code that uses the comma operator!)
2. Wait a few releases.
3. Introduce straightforward, built-in tuples:

auto t=(1,"string",'c');
auto (i,s,c)=t; //Tuple!(int,string,char) cannot do this. Maybe there is a better
syntax for type inference, but I think this one it quite good.

(auto x,auto y,auto d) s=(1,2,3.0);
//... (can use s.x, s.y and s.d now)

writeln(i," ",s," ",c);

(string,) t=("this is a one-element tuple",);

(int,int,int) q=(1,2,3);
int[3] arr=q; //should be allowed in both directions, because arrays are basically
special cases of tuples. function calls already reflect this.

Other semantics could be similar to Tuple!(T). Maybe I missed some obvious
feature. In that case, include that too. :)

>
> Bye,
> bearophile


Timon


More information about the Digitalmars-d mailing list