Re: override toString() for a tuple?

2014-06-04 Thread Jonathan M Davis via Digitalmars-d-learn
On Wed, 04 Jun 2014 05:35:18 +
Steve D via Digitalmars-d-learn digitalmars-d-learn@puremagic.com
wrote:

 Is it possible to override std tuple's toString format?

 so that
  auto a = tuple(hello,1,2,3);
  writeln(a);

 prints
  (hello, 1, 2, 3)
 and not
  Tuple!(string, int, int, int)(hello, 1, 2, 3)


 I'm aware I could write a custom formatter function, but it would
 be nice not to
 have to use such a function for every tuple printed by the
 program.
 Overriding toString() one time in program (if possible) would
 give the ideal default behaviour. (I would duplicate the current
 typecons.d toString() and strip off the prefix)

 thanks for any help

toString is a member of Tuple, and there's no way to override that externally.
You could create a wrapper struct for a Tuple whose toString method did what
you want, and you could just create a function which generated the string that
you wanted that you used whenever printing out a Tuple, but there is no way to
globally override Tuple's toString.

The closest that you could do to overriding Tuple's toString in one place
would be to write your own wrappers for whatever printing functions you want
to use, have them detect when they're given a Tuple, and then print them the
way that you want and pass everything else directly on to writeln or whatever
it is you're wrapping. Then, the print functions would take care of it for
you, but writing such a function wouldn't exactly be fun.

If you're really determined to print tuples differently, you _could_ simply
copy std.typecons.Tuple to your own code and alter it to do what you want.

- Jonathan M Davis


Re: override toString() for a tuple?

2014-06-04 Thread Steve D via Digitalmars-d-learn
On Wednesday, 4 June 2014 at 06:04:22 UTC, Jonathan M Davis via 
Digitalmars-d-learn wrote:


toString is a member of Tuple, and there's no way to override 
that externally.

...

Hi Jonathan,

Yeah, I'll probably just keep my locally cobbled version of 
typecons.d in my path.
The other options would be hard going as I've got tuples printed 
from arrays and variant arrays etc as well as individually.
It's just easier to hack the default library code, although not 
so elegant. You would think the promise of OO and Inheritance 
would make it easy and free us from hacks like this ;)  That 
said, it's only a personal project so as long as it works, who 
cares?


Many Thanks for your reply
Steve D



Re: why it said no identifier for declarator …

2014-06-04 Thread bioinfornatics via Digitalmars-d-learn

On Wednesday, 4 June 2014 at 00:07:35 UTC, Jesse Phillips wrote:

On Tuesday, 3 June 2014 at 22:10:06 UTC, bioinfornatics wrote:
I do not see why it fail in debug output we see that tuple 
have a

field with given name.


Your generated output (short and formatted)

alias TL = Tuple!(int,x,  bool function( const ref string 
),

xStartsWith , bool function( const ref string ), xEndsWith ,
int,y,  bool function( const ref string ), yStartsWith , 
bool

function( const ref string ), yEndsWith );

TL.xStartsWith...

TL is a type.

TL variable;
variable.xStartsWith...


oh stupid me thanks Jesse sometime you do not see a problem 
visible as the nose on your face.


Re: override toString() for a tuple?

2014-06-04 Thread Jonathan M Davis via Digitalmars-d-learn
On Wed, 04 Jun 2014 06:25:53 +
Steve D via Digitalmars-d-learn digitalmars-d-learn@puremagic.com
wrote:

 You would think the promise of OO and Inheritance
 would make it easy and free us from hacks like this ;)

That would require using OO and inheritance, which has nothing to do with
Tuple. ;)

And actually, I find that I very rarely need inheritance. It's definitely the
right solution for some problems, but in the vast majority of cases, I find
that structs are a better solution - especially because they're far more
composable. OO is actually very bad for code reuse, because it's not
particularly composable at all.

- Jonathan M Davis


Re: why it said no identifier for declarator …

2014-06-04 Thread bioinfornatics via Digitalmars-d-learn

On Wednesday, 4 June 2014 at 00:07:35 UTC, Jesse Phillips wrote:

On Tuesday, 3 June 2014 at 22:10:06 UTC, bioinfornatics wrote:
I do not see why it fail in debug output we see that tuple 
have a

field with given name.


Your generated output (short and formatted)

alias TL = Tuple!(int,x,  bool function( const ref string 
),

xStartsWith , bool function( const ref string ), xEndsWith ,
int,y,  bool function( const ref string ), yStartsWith , 
bool

function( const ref string ), yEndsWith );

TL.xStartsWith...

TL is a type.

TL variable;
variable.xStartsWith...



after a tmeplate update


template toTuple(T){
static string maker(){
string assignFunction = TL tl; ;
string statement  = alias TL = Tuple!(;
foreach(const memberName; __traits(allMembers, T)){
mixin(`alias f = Filter!(isSection, 
__traits(getAttributes, T.` ~ memberName ~ `));`);

if( f.length == 1 )
{
statement   ~= typeof(__traits(getMember, T, 
memberName)).stringof  ~ ,\ ~ memberName ~ \, 
~  bool function( const ref string 
), \ ~ memberName ~ StartsWith\,
~  bool function( const ref string 
), \ ~ memberName ~ EndsWith\,  ;
assignFunction ~= tl. ~ memberName ~ 
StartsWith = __traits(getAttributes, T. ~ memberName ~ 
)[0].startsWith; 
   ~  tl. ~ memberName ~ EndsWith  
 = __traits(getAttributes, T. ~ memberName ~ )[0].endsWith; ;

}
}
statement = statement[0..$-2] ~ ) ; ; // $-2 to remove 
extra comma

return statement~assignFunction~ alias toTuple = tl;;
}
pragma( msg, maker() );
mixin( maker() );
}




which generate this string:

alias TL = Tuple!(int,x,  bool function( const ref string ), 
xStartsWith, bool function( const ref string ), xEndsWith, 
int,y,  bool function( const ref string ), yStartsWith, bool 
function( const ref string ), yEndsWith) ;

TL tl;
tl.xStartsWith = __traits(getAttributes, T.x)[0].startsWith;
tl.xEndsWith   = __traits(getAttributes, T.x)[0].endsWith;
tl.yStartsWith = __traits(getAttributes, T.y)[0].startsWith;
tl.yEndsWith   = __traits(getAttributes, T.y)[0].endsWith;
alias toTuple = tl;



This produce an error at mixin step:

attribute.d-mixin-62(62): Error: no identifier for declarator 
tl.xStartsWith

attribute.d-mixin-62(62): Error: Declaration expected, not '='
attribute.d-mixin-62(62): Error: no identifier for declarator 
tl.xEndsWith

attribute.d-mixin-62(62): Error: Declaration expected, not '='
attribute.d-mixin-62(62): Error: no identifier for declarator 
tl.yStartsWith

attribute.d-mixin-62(62): Error: Declaration expected, not '='
attribute.d-mixin-62(62): Error: no identifier for declarator 
tl.yEndsWith

attribute.d-mixin-62(62): Error: Declaration expected, not '='
;


Re: Building 32bit program with MSVC?

2014-06-04 Thread Kagamin via Digitalmars-d-learn
LLVM never supported OMF. LDC uses msvcrt runtime, and MS claims 
that whatever can link with msvcrt, it also can link with later 
versions of msvcrt.


Re: Kernel in D

2014-06-04 Thread Kagamin via Digitalmars-d-learn

On Saturday, 31 May 2014 at 23:27:45 UTC, Qox wrote:

On Saturday, 31 May 2014 at 07:57:18 UTC, Kagamin wrote:

http://www.xomb.org/ ?


seems to be outdated, but its another OS written in D.


It's dead for only a year, the developer have probably graduated.


Re: why it said no identifier for declarator …

2014-06-04 Thread Jesse Phillips via Digitalmars-d-learn

I take it the output looks something like this:

struct S {
int a;
}

S s;
s.a = 3;

void main() {
}

Hope this clears up this next problem. Module scope doesn't get 
to utilize a variable, it can only initialize at compile-time.


Arrays as template parameters

2014-06-04 Thread cal via Digitalmars-d-learn
I have the following code (on dpaste, 
http://dpaste.dzfl.pl/636c04430a33):


enum : uint { a, b, c }
enum list = [a, b];

void foo(T...)()
{
  pragma(msg, T[0].length); // fine
  pragma(msg, T[0][0]); // fine
  pragma(msg, T[0][1]); // fine

  foreach(i; Iota!(0,T[0].length)) // fine
pragma(msg, T[0][i]);

  //foreach(c; T[0]) // not fine
  //pragma(msg, c);
}

template Iota(size_t i, size_t n)
{
  import std.typetuple : TypeTuple;
  static if (n == 0) alias TypeTuple!() Iota;
else alias TypeTuple!(i, Iota!(i + 1, n - 1)) Iota;
}

void main()
{
  foo!list;
}

Just trying to pass a statically known array as a template 
parameter. The foreach marked 'not fine' doesn't work, saying c 
cannot be read at compile time, despite the length and all values 
being known at compile time. The foreach above it uses a template 
to generate a static tuple of indices, which are used to index 
into the array, which works but seems unnecessary.


Is there a better way to do this?


how to get line number after readln

2014-06-04 Thread Robert Hathaway via Digitalmars-d-learn
I've got a program that reads a text file line by line (using 
std.stdio readln()) and I'd like to refer to the line number when 
I send a message to stderr upon finding a mis-formatted line.  Is 
there a way to get the current line number?  Of course, I could 
create a counter and increment it with each call to readln, but 
is there a cool way of doing this?


Okay, call me lazy... just don't call me late for dinner! :-)

Robert


Re: how to get line number after readln

2014-06-04 Thread Ali Çehreli via Digitalmars-d-learn

On 06/04/2014 05:05 PM, Robert Hathaway wrote:

I've got a program that reads a text file line by line (using std.stdio
readln())


Consider using byLine() instead. (Important: byLine uses an internal 
buffer for the line; so, don't forget to make a copy if you want to 
store the line for later use.)


 and I'd like to refer to the line number when I send a message

to stderr upon finding a mis-formatted line.  Is there a way to get the
current line number?  Of course, I could create a counter and increment
it with each call to readln, but is there a cool way of doing this?

Okay, call me lazy... just don't call me late for dinner! :-)

Robert


One cool way is a zipped sequence:

import std.stdio;
import std.range;

void main()
{
foreach (i, line; zip(sequence!n, File(deneme.txt).byLine)) {
writefln(%s: %s, i, line);
}
}

Ali



Re: how to get line number after readln

2014-06-04 Thread Brad Anderson via Digitalmars-d-learn

On Thursday, 5 June 2014 at 00:33:26 UTC, Ali Çehreli wrote:

On 06/04/2014 05:05 PM, Robert Hathaway wrote:
I've got a program that reads a text file line by line (using 
std.stdio

readln())


Consider using byLine() instead. (Important: byLine uses an 
internal buffer for the line; so, don't forget to make a copy 
if you want to store the line for later use.)


 and I'd like to refer to the line number when I send a message
to stderr upon finding a mis-formatted line.  Is there a way 
to get the
current line number?  Of course, I could create a counter and 
increment
it with each call to readln, but is there a cool way of 
doing this?


Okay, call me lazy... just don't call me late for dinner! :-)

Robert


One cool way is a zipped sequence:

import std.stdio;
import std.range;

void main()
{
foreach (i, line; zip(sequence!n, 
File(deneme.txt).byLine)) {

writefln(%s: %s, i, line);
}
}

Ali


Once this[1] gets merged you'll be able to do this:

foreach (lineNum, line; File(deneme.txt).byLine().enumerate(1))
  writefln(%s: %s, lineNum, line);

Which is a bit more clear about the intent.

1. https://github.com/D-Programming-Language/phobos/pull/1866