Why TypeTuple can be assigned to a variable

2013-06-12 Thread Zhenya

Hi!

I was just surprised when realized, that this code compiles and 
runs:


import std.typetuple;
import std.stdio;

void main()
{
 auto foo = TypeTuple!(foo,bar);
writeln(typeid(typeof(foo)));
 writeln(foo);
}

If I were compiler expert,I'd say that it's a bug.But I am not)
So, can anybody explain why it's work?

Thank you.


Re: Why TypeTuple can be assigned to a variable

2013-06-12 Thread Zhenya

On Wednesday, 12 June 2013 at 08:14:06 UTC, Simen Kjaeraas wrote:
On Wed, 12 Jun 2013 10:01:59 +0200, Zhenya zh...@list.ru 
wrote:



Hi!

I was just surprised when realized, that this code compiles 
and runs:


import std.typetuple;
import std.stdio;

void main()
{
 auto foo = TypeTuple!(foo,bar);
writeln(typeid(typeof(foo)));
 writeln(foo);
}

If I were compiler expert,I'd say that it's a bug.But I am not)
So, can anybody explain why it's work?


It is the equivalent of:

  TypeTuple!(string, string) foo;
  foo[0] = foo;
  foo[1] = bar;

The ability to have a tupetuple as a variable is very useful - 
if that

had not been possible one would need something akin to this:

struct Tuple(T...) {
T[0] head;
static if (T.length) {
Tuple!(T[1..$]) tail;
}
}

And to access the third element of a tuple one'd need to write:

myTuple.tail.tail.head = foo;

Clearly this is suboptimal, so D has better ways of doing such 
things.



OK,you say that TypeTuple!(foo,bar) is a cool value of type
TypeTuple!(string,string),right?

Then could you tell me what type has [TypeTuple!(foo,bar)]?

Currently it is evaluated to string[].And I think if TypeTuple

had value semantic, this type would be TypeTuple!(...)[].

This behaviour confuses me a bit.

And I just don't understand why do we need TypeTuple's value
semantic
to implement std.Tuple,because AFAIK it use variadic template
parameter pack
  != TypeTuple.
Sorry for my english.


Re: Why TypeTuple can be assigned to a variable

2013-06-12 Thread Zhenya

On Wednesday, 12 June 2013 at 10:09:07 UTC, Simen Kjaeraas wrote:
On Wed, 12 Jun 2013 11:44:19 +0200, Zhenya zh...@list.ru 
wrote:



OK,you say that TypeTuple!(foo,bar) is a cool value of type
TypeTuple!(string,string),right?


Well, yes and no, not really. It's a bit magical. In your case,
it's assigned to an auto variable, and that variable gets that 
type.
There are other ways to use a TypeTuple where it has other 
semantics,

as you write yourself.

As explained below, a TypeTuple is just a bag of template 
parameters,

and thus obeys the rules for a bag of template parameters.



This behaviour confuses me a bit.


Understandable. It's not entirely straightforward, because the
concerns of usability weigh heavier than those of consistency.



And I just don't understand why do we need TypeTuple's value
semantic
to implement std.Tuple,because AFAIK it use variadic template
parameter pack
  != TypeTuple.


The definition od std.typetuple.TypeTuple is:

template TypeTuple(T...){
alias TypeTuple = T;
}

So a TypeTuple is exactly the same as a variadic template
parameter pack.



Sorry for my english.


No need to be, your English is great.


Thank you for your explaination,I understand.



Re: issue 9122

2013-05-12 Thread Zhenya

On Sunday, 12 May 2013 at 10:49:40 UTC, Tofu Ninja wrote:

http://d.puremagic.com/issues/show_bug.cgi?id=9122

Is anyone working on this or know of a work around, it is 
breaking my project and I can't move on until its fixed.


see 
http://forum.dlang.org/thread/puqbpycvisbjrahao...@forum.dlang.org


Re: Collections

2013-03-12 Thread Zhenya

On Tuesday, 12 March 2013 at 11:57:29 UTC, ZILtoid1991 wrote:

Is there any equaliaments of java collections in D?


https://github.com/Domain/java/tree/master/java


Re: Collections

2013-03-12 Thread Zhenya

On Tuesday, 12 March 2013 at 12:02:03 UTC, Zhenya wrote:

On Tuesday, 12 March 2013 at 11:57:29 UTC, ZILtoid1991 wrote:

Is there any equaliaments of java collections in D?


https://github.com/Domain/java/tree/master/java


Oh sorry,it seems to be unfinished.


Re: question with compile-time reflection

2013-03-10 Thread Zhenya
And since alias can represent any symbol,i think that it's 
correct usage.




Re: question with compile-time reflection

2013-03-10 Thread Zhenya

On Monday, 11 March 2013 at 00:10:46 UTC, Rob T wrote:

template NDimensionalArrayType(T,alias size)

I'm wondering what alias size does? I can't find that form 
documented anywhere, but it seems to be valid.


Thanks.

--rt


It's just a little hack for known bug - only string and integer 
value can be passed into template.
size is array,and it's really size of n-dimensional array,every 
element matches

its dimension.


question with compile-time reflection

2013-03-09 Thread Zhenya

Hi!

this code fails with message:NDimensionalArray.doIndex(Array) if 
(is(Array _ : NDimensionalArrayType!(T,s),const(int[]) s)) cannot 
deduce template function from argument types 
!()(int[2u][2u],const(int[]))


Tell me please,am I wrong?
And if yes,what should I change to make it work?

template NDimensionalArrayType(T,alias size)
if(is(typeof(size) _ : const int[])  size.length  0)
{
static if(size.length  1)
		alias NDimensionalArrayType!(T,size[1..$])[size[0]] 
NDimensionalArrayType;

else
alias T[size[0]] NDimensionalArrayType;
}

struct NDimensionalArray(T,alias size)
{
private NDimensionalArrayType!(T,size) m_array;
static ref T doIndex(Array)(Array array,const int[] index) //HERE
if(is(Array _ : NDimensionalArrayType!(T,s),const int[] s))
{
assert(index.length == s.length);
if(index.length == 1)
return array[index[0]];
else
return doIndex(array[index[0]],index[1..$]);
}
ref T opIndex(const int[] index)
{
return doIndex(m_array,index);
}
alias m_array this;
}

void main()
{
NDimensionalArray!(int,[2,2]) array;
}


Re: question with compile-time reflection

2013-03-09 Thread Zhenya

On Saturday, 9 March 2013 at 23:05:56 UTC, Ivan Kazmenko wrote:
this code fails with message:NDimensionalArray.doIndex(Array) 
if (is(Array _ : NDimensionalArrayType!(T,s),const(int[]) s)) 
cannot deduce template function from argument types 
!()(int[2u][2u],const(int[]))


Tell me please,am I wrong?
And if yes,what should I change to make it work?


I'm no D expert, but here's what I got.

1. One problem is returning the wrong type from doIndex.  When 
the condition if(index.length == 1) is false, the subsequent 
line is still compiled but fails to return a ref T since 
array[index[0]] is an array of T, not a T.


The correct way (and better for performance, too) would be a 
compile-time static if there, like in the code below.  (I 
don't quite get the syntax of the template constraint for 
doIndex, so I've just commented that out for now.)


2. The next problem is that fixed-size arrays obey value 
semantics, so they should be passed by reference and not by 
value.


Here's what works for me (DMD 2.062):

-
import std.stdio;

template NDimensionalArrayType(T,alias size)
if(is(typeof(size) _ : const int[])  size.length  0)
{
static if(size.length  1)
alias NDimensionalArrayType!(T,size[1..$])[size[0]]
NDimensionalArrayType;
else
alias T[size[0]] NDimensionalArrayType;
}

struct NDimensionalArray(T,alias size)
{
private NDimensionalArrayType!(T,size) m_array;
	static ref T doIndex(Array)(ref Array array,const int[] index) 
//HERE

//  if(is(Array _ : NDimensionalArrayType!(T,s),const int[] s))
{
//  assert(index.length == s.length);
assert(index.length = size.length);
debug {writefln(doIndex (%s, %s = %s),
typeof(array).stringof,
typeof(index).stringof,
index);}
static if(is(Array : T[]))
{
assert(index.length == 1);
return array[index[0]];
}
else
{
assert(index.length  1);
return doIndex(array[index[0]],index[1..$]);
}
}
ref T opIndex(const int[] index)
{
return doIndex(m_array,index);
}
alias m_array this;
}

void main()
{
NDimensionalArray!(int,[5,2]) array;
array[[4,1]] = 1;
assert(array[[4,1]] == 1);
}
-

With the debug mode on, it successfully prints the following:

-
doIndex (int[2u][5u], const(int[]) = [4, 1])
doIndex (int[2u], const(int[]) = [1])
doIndex (int[2u][5u], const(int[]) = [4, 1])
doIndex (int[2u], const(int[]) = [1])
-

On a side note, this still looks like too much effort to do one 
opIndex in terms of performance.  I doubt that any current 
compiler would optimize all that into a simple multiply + 
bounds-check + add loop.


-
Ivan Kazmenko.


Thank you very much for your time)
Your answer is very useful.


Re: question with compile-time reflection

2013-03-09 Thread Zhenya
Although of course I would want to know what's wrong with 
constrait,because it protect doArray against incorrect using.
I don't worry about effiency very much,I just wanted to right 
n-dimensional tree of intervals.And for doing it I needed 
n-dimensional array with opIndex like it.


Re: question with compile-time reflection

2013-03-09 Thread Zhenya

On Saturday, 9 March 2013 at 23:40:25 UTC, Zhenya wrote:
Although of course I would want to know what's wrong with 
constrait,because it protect doArray against incorrect using.
I don't worry about effiency very much,I just wanted to right 
n-dimensional tree of intervals.And for doing it I needed 
n-dimensional array with opIndex like it.


sorry for my english(


typeof([2,2]) !=? int[2]

2013-03-08 Thread Zhenya

Hi!
Explain me please,what's wrong with this code:

struct NDimensionalArray(T,alias size)
if(is(typeof(size) _ == int[n],int n) 
   n  0)
{
static if(n  1)
NDimensionalArray!(T,n - 1,size[1..$]) m_array[size[0]];
else
T m_array[size[0]];
alias m_array this;
}

void main()
{
NDimensionalArray!(int,[2,2]) array;
array[0][1] = 1;
}

It fails with message:
Error: template instance NDimensionalArray!(int, [2, 2]) 
NDimensionalArray!(int, [2, 2]) does not match template 
declaration NDimensionalArray(T, alias size) if (is(typeof(size) 
_ == int[n],int n)  n  0)

on dmd git-head.


Re: typeof([2,2]) !=? int[2]

2013-03-08 Thread Zhenya

On Friday, 8 March 2013 at 22:59:40 UTC, bearophile wrote:

This is an answer to just your title question.

A lot of time ago typeof([2,2]) was int[2]. This was efficient, 
but in most cases this was a source of troubles and bugs. So 
now a [2,2] is a heap-allocated dynamic array of type int[]. 
Some persons have askes for a fixed-sized array litera, like 
s[2,2] but nothing has happened on this so far.


Bye,
bearophile


Thank you very much.


Re: typeof([2,2]) !=? int[2]

2013-03-08 Thread Zhenya

On Friday, 8 March 2013 at 23:03:47 UTC, Zhenya wrote:

On Friday, 8 March 2013 at 22:59:40 UTC, bearophile wrote:

This is an answer to just your title question.

A lot of time ago typeof([2,2]) was int[2]. This was 
efficient, but in most cases this was a source of troubles and 
bugs. So now a [2,2] is a heap-allocated dynamic array of type 
int[]. Some persons have askes for a fixed-sized array litera, 
like s[2,2] but nothing has happened on this so far.


Bye,
bearophile


Thank you very much.


Although it's a bit strange,since we can always pass heap 
allocated array by

using new.


Re: typeof([2,2]) !=? int[2]

2013-03-08 Thread Zhenya

On Friday, 8 March 2013 at 23:09:07 UTC, cal wrote:

On Friday, 8 March 2013 at 23:03:47 UTC, Zhenya wrote:

Your constraint could be:

if(is(typeof(size) _ == int[])  size.length  0)

Also it looks like you are passing 1 too many template args?

static if(n  1)
  NDimensionalArray!(T,n - 1,size[1..$]) m_array[size[0]];
 ^^


Yes,it's a typo.But it seems to be ugly pass dynamically 
allocated array through
a template parameter,because it's size should be compile-time 
constant.


Re: typeof([2,2]) !=? int[2]

2013-03-08 Thread Zhenya

On Friday, 8 March 2013 at 23:18:52 UTC, cal wrote:

On Friday, 8 March 2013 at 23:15:30 UTC, Zhenya wrote:
Yes,it's a typo.But it seems to be ugly pass dynamically 
allocated array through
a template parameter,because it's size should be compile-time 
constant.


Its size is a compile-time constant because it is an array 
literal. The size is known inside the constraint.

Yes,really.
I thought that dynamic allocation is runtime operation


Re: Automatic dynamic dispatch

2013-02-06 Thread Zhenya
On Wednesday, 6 February 2013 at 04:37:17 UTC, Andrej Mitrovic 
wrote:
Someone asked about how to invoke a function with the dynamic 
type of
an object. Essentially the user wanted to implement functions 
external
to a class without touching the class vtable (he might not have 
access
to it if it's in another library), but he explicitly wanted to 
work on

the derived type and not the base type, for example:

class A { }
class B : A { }
class C : B { }

void foo(B b) { }  // requires B or derived from B, not A
void foo(C c) { }  // requires C or derived from C, not A

Since all classes have a TypeInfo_Class associated with them, 
we can
create a few helper templates which figure out the entire class 
tree
from a set of leaf classes, and then tries to dynamically 
dispatch to

the appropriate function at runtime.

Here's the code to do just that: http://dpaste.dzfl.pl/8338067b

And pasted here for convenience:

import std.stdio;
import std.typetuple;
import std.traits;
import std.string;

class A { }
class B : A { }
class C : B { }
class D : B { }

template ClassTreeImpl(Leaves...)
{
static if (Leaves.length  1)
{
alias TypeTuple!(Leaves[0], 
BaseClassesTuple!(Leaves[0]),
 ClassTreeImpl!(Leaves[1..$])) 
ClassTreeImpl;

}
else
static if (Leaves.length == 1)
{
alias TypeTuple!(Leaves[0], 
BaseClassesTuple!(Leaves[0])) ClassTreeImpl;

}
else
{
alias TypeTuple!() ClassTreeImpl;
}
}

template ClassTree(Leaves...)
{
alias 
DerivedToFront!(NoDuplicates!(ClassTreeImpl!(Leaves))) 
ClassTree;

}

void callFunc(alias func, Args...)(Args args)
if (Args.length = 1  is(Args[0] == class))
{
auto objInfo = typeid(args[0]);
foreach (Base; ClassTree!(C, D))
{
if (objInfo == Base.classinfo)
{
static if (__traits(compiles,  // avoid CT errors 
due to

unrolled static foreach
{ return func(cast(Base)(cast(void*)args[0]),
args[1..$]); }() ))
{
return func(cast(Base)(cast(void*)args[0]), 
args[1..$]);

}
}
}

assert(0, format(function '%s' is not callable with object 
of

dynamic type '%s',
 __traits(identifier, func), 
objInfo.toString()));

}

void foo(C c, int x) { writefln(foo(C) : received %s, x); }
void foo(D d, int x, int y) { writefln(foo(D) : received %s 
%s, x, y); }


void main()
{
A c = new C;
A d = new D;
A a = new A;

callFunc!foo(c, 1);// ok
callFunc!foo(d, 2, 3); // ok
callFunc!foo(a, 3);// will assert at runtime
}

It would have been a good blog entry, but I don't blog so.. :)

Hi!
When I read your post I remembered,that some time ago I had 
written in D
an n-dimensional dispatcher simular to the one,described by 
Andrei in Modern C++ Design.But my dispatcher allow casting of 
arguments:

import std.stdio;

class Foo {}
class Bar : Foo {}
class Gun {}

void main()
{
Dispatcher!2 d;
d.add((Foo f,Gun g) {writeln(FooXGun)});
d(new Bar,new Gun);//prints FooXGun,because Bar can be 
converted to Foo

}

Maybe my code has more mixin's than needed,but it works,and it 
has some disadvantages,but

I would be glad if it was useful:
http://dpaste.dzfl.pl/2ff50a12



Re: Building DFL

2013-01-25 Thread Zhenya

On Friday, 25 January 2013 at 09:07:44 UTC, SaltySugar wrote:

On Friday, 25 January 2013 at 09:06:30 UTC, SaltySugar wrote:

Hey guys. I have a problem when building DFL.
My Console:

C:\D\dmd2\packages\dfl\examples\d2dfl hello.d
DFL lib files not found.
Would you like to build the DFL lib files now? [Y/n]

I press y.

Then:

Compiling debug DFL...

C:\D\dmd2\import\dflC:\D\dmd2\windows\bin\dmd -c -debug -g
-I.. all.d base.d
application.d internal/dlib.d internal/clib.d internal/utf.d 
internal/com.d con
trol.d form.d registry.d drawing.d menu.d notifyicon.d 
commondialog.d filedialog
.d folderdialog.d panel.d textbox.d richtextbox.d picturebox.d 
listbox.d groupbo
x.d splitter.d usercontrol.d button.d label.d collections.d 
internal/winapi.d in
ternal/wincom.d event.d socket.d timer.d environment.d 
messagebox.d tooltip.d co
mbobox.d treeview.d tabcontrol.d colordialog.d listview.d 
data.d clipboard.d fon
tdialog.d progressbar.d resources.d statusbar.d imagelist.d 
toolbar.d
application.d(1845): Deprecation: use of typedef is 
deprecated; use alias instea

d
application.d(1845): Deprecation: use of typedef is 
deprecated; use alias instea

d
internal\winapi.d(1726): Deprecation: use of typedef is 
deprecated; use alias in

stead
internal\winapi.d(1726): Deprecation: use of typedef is 
deprecated; use alias in

stead
socket.d(89): Error: module intrinsic is in file 
'std\intrinsic.d' which cannot

be read
import path[0] = ..
import path[1] = C:\D\dmd2\windows\bin\..\..\src\phobos
import path[2] = 
C:\D\dmd2\windows\bin\..\..\src\druntime\import


Failed.

Done.
Could Not Find C:\D\dmd2\import\dfl\*.obj

DFL lib files not found.
Error: dfl_debug.lib not found

How to fix It? :( I'm using dmd 2.061

Maybe you tried to compile an old version of dfl.If yes,try this:
https://github.com/Rayerd/dfl


Re: Building DFL

2013-01-25 Thread Zhenya

Which version of dmd are you using?


Re: Building DFL

2013-01-25 Thread Zhenya

On Friday, 25 January 2013 at 10:17:48 UTC, SaltySugar wrote:

On Friday, 25 January 2013 at 10:15:33 UTC, Zhenya wrote:

Which version of dmd are you using?


dmd 2.061. I tried building DFL on dmd 2.059 too.


I am using dmd 2.060.
I just have built it.
Try to open
...\dfl-master\win32\dfl\makelib.bat


Re: Building DFL

2013-01-25 Thread Zhenya

On Friday, 25 January 2013 at 10:21:27 UTC, Zhenya wrote:

On Friday, 25 January 2013 at 10:17:48 UTC, SaltySugar wrote:

On Friday, 25 January 2013 at 10:15:33 UTC, Zhenya wrote:

Which version of dmd are you using?


dmd 2.061. I tried building DFL on dmd 2.059 too.


I am using dmd 2.060.
I just have built it.
Try to open
...\dfl-master\win32\dfl\makelib.bat


If the same error occur,you should edit the file environment.d


Re: Building DFL

2013-01-25 Thread Zhenya

On Friday, 25 January 2013 at 10:28:26 UTC, SaltySugar wrote:

On Friday, 25 January 2013 at 10:21:27 UTC, Zhenya wrote:

On Friday, 25 January 2013 at 10:17:48 UTC, SaltySugar wrote:

On Friday, 25 January 2013 at 10:15:33 UTC, Zhenya wrote:

Which version of dmd are you using?


dmd 2.061. I tried building DFL on dmd 2.059 too.


I am using dmd 2.060.
I just have built it.
Try to open
...\dfl-master\win32\dfl\makelib.bat


Maybe I don't know how to install it? I replaced 
C:\D\dmd2\import\dfl with dfl_master\win_32\dfl

Then copied dfl_master\win_32\dflexe to C:\D\dmd2\import

I installed it correctly?

Sorry I am not very familiar with that.
But I build it succefuly
by only executing ...\dfl-master\win32\dfl\makelib.bat.
Your errors may be caused by new dmd
In that case you should manually change the file environment.d 
and everything will be allright.


Re: Building DFL

2013-01-25 Thread Zhenya

On Friday, 25 January 2013 at 10:37:00 UTC, SaltySugar wrote:

On Friday, 25 January 2013 at 10:33:42 UTC, Zhenya wrote:

On Friday, 25 January 2013 at 10:28:26 UTC, SaltySugar wrote:

On Friday, 25 January 2013 at 10:21:27 UTC, Zhenya wrote:

On Friday, 25 January 2013 at 10:17:48 UTC, SaltySugar wrote:

On Friday, 25 January 2013 at 10:15:33 UTC, Zhenya wrote:

Which version of dmd are you using?


dmd 2.061. I tried building DFL on dmd 2.059 too.


I am using dmd 2.060.
I just have built it.
Try to open
...\dfl-master\win32\dfl\makelib.bat


Maybe I don't know how to install it? I replaced 
C:\D\dmd2\import\dfl with dfl_master\win_32\dfl

Then copied dfl_master\win_32\dflexe to C:\D\dmd2\import

I installed it correctly?

Sorry I am not very familiar with that.
But I build it succefuly
by only executing ...\dfl-master\win32\dfl\makelib.bat.
Your errors may be caused by new dmd
In that case you should manually change the file environment.d 
and everything will be allright.


maybe you can send me yours environment.d file?


My is the same.I just use dmd 2.060.
But it is quite simple to edit it.
I think you should just add goto statements:
int a = 1;
switch(a)
{
case 1:
goto case 2;
case 2:
{}
goto default;
default:
{}
}
Sorry if I am useless(


Re: D popularity

2013-01-21 Thread Zhenya
Oh? What's so unreliable about delegates? I find that they work 
quite

well in general.


This for example:
http://d.puremagic.com/issues/show_bug.cgi?id=5710


enum function can't be passed into template?

2013-01-20 Thread Zhenya

Hi!
Am I doing something wrong?

import std.stdio;

template gun(alias f)
{
void gun()
{
f();
}
}

void main()
{
auto str = hello;
enum fun = (){writeln(str);};//replace enum - auto to compile
gun!fun();
}

Error:delegate c634.main.__lambda1 is a nested function and 
cannot be accessed from c634.gun!(delegate @system void()

{
writeln(str);
}
).gun


Re: enum function can't be passed into template?

2013-01-20 Thread Zhenya

On Sunday, 20 January 2013 at 14:51:51 UTC, Philippe Sigaud wrote:

On Sun, Jan 20, 2013 at 3:21 PM, Zhenya zh...@list.ru wrote:

Hi!
Am I doing something wrong?

import std.stdio;

template gun(alias f)
{
void gun()
{
f();
}
}

void main()
{
auto str = hello;
enum fun = (){writeln(str);};//replace enum - auto to 
compile

gun!fun();
}


fun depends on str, which is a runtime value. Either make str 
an enum
or put them in the module scope (which will make the auto's 
enum's)

Thank you!


Re: does alias this work correctly?

2013-01-14 Thread Zhenya
On Sunday, 13 January 2013 at 22:36:03 UTC, Jonathan M Davis 
wrote:

On Sunday, January 13, 2013 20:41:48 Zhenya wrote:

On Sunday, 13 January 2013 at 19:35:08 UTC, Maxim Fomin wrote:
 According to spec http://dlang.org/class.html#AliasThis
 undefined lookups are forwarded to AliasThis member. But in
 your case Foo struct has bar member, so no further resolution
 is performed. Rename the member and the code compiles.
 
 Note, this seems to come from neighbor thread

 http://forum.dlang.org/thread/uelmpwwckcveimpbd...@forum.dlang.org.
 I think it is simpler to rename functions than create bunch 
 of
 structs just to be able to have eponymous non-static and 
 static

 functions.

I just want very much avoid renaming function,it's principle 
for

me.
So I would like to know is my sample right or no.


It functions exactly like it's supposed to. As Maxim said, 
undefined lookups go
to the alias this member. If Foo already has a bar and you try 
and call it, it
doesn't matter what Foo is aliased to, it's Foo's bar that's 
going to be used.


- Jonathan M Davis

Now it's clear for me,thank you.


Re: does alias this work correctly?

2013-01-14 Thread Zhenya

On Sunday, 13 January 2013 at 23:21:20 UTC, Andrey wrote:
I just want very much avoid renaming function,it's principle 
for me.

So I would like to know is my sample right or no.


I think that the main overall principle here is that is it 
impossible to have two functions which differ only by static 
attribute. I even do not imagine the use case of this.


Well, here is the most natural way to achieve similar effect, I 
suppose:


struct MyStruct {

struct Static {

static void myfun() {
writeln(static myfun);
}
}

void myfun() {
writeln(myfun);
}

static Static opCall() {
return Static();
}
}


MyStruct obj;

obj.myfun(); //dynamic;
MyStruct().myfun(); //static;

I do not agree with you, static attribute is not a small detail.
this reference is the same argument, like all the others. And 
despite the fact that
function signatures are the same, the actual list of arguments 
are different.


Re: does alias this work correctly?

2013-01-14 Thread Zhenya
On Sunday, 13 January 2013 at 22:36:03 UTC, Jonathan M Davis 
wrote:

On Sunday, January 13, 2013 20:41:48 Zhenya wrote:

On Sunday, 13 January 2013 at 19:35:08 UTC, Maxim Fomin wrote:
 According to spec http://dlang.org/class.html#AliasThis
 undefined lookups are forwarded to AliasThis member. But in
 your case Foo struct has bar member, so no further resolution
 is performed. Rename the member and the code compiles.
 
 Note, this seems to come from neighbor thread

 http://forum.dlang.org/thread/uelmpwwckcveimpbd...@forum.dlang.org.
 I think it is simpler to rename functions than create bunch 
 of
 structs just to be able to have eponymous non-static and 
 static

 functions.

I just want very much avoid renaming function,it's principle 
for

me.
So I would like to know is my sample right or no.


It functions exactly like it's supposed to. As Maxim said, 
undefined lookups go
to the alias this member. If Foo already has a bar and you try 
and call it, it
doesn't matter what Foo is aliased to, it's Foo's bar that's 
going to be used.


- Jonathan M Davis


import std.stdio;

struct Bar
{
void opDispatch(string op)()
if(op == bar)
{
if(this !is m_init)
writeln(non-static);
else
writeln(maybe static);
}
static @property Bar m_init()
{
return Bar.init;
}
alias m_init this;
}

void main()
{
Bar b;
Bar.bar();
readln;
}

If I understood you correctly, it must be compiled since there 
isn't some 'bar' that is going to be used, right?

But it doesn't compile.


Re: does alias this work correctly?

2013-01-14 Thread Zhenya
On Tuesday, 15 January 2013 at 00:04:15 UTC, Jonathan M Davis 
wrote:

On Monday, January 14, 2013 17:57:03 Zhenya wrote:

import std.stdio;

struct Bar
{
void opDispatch(string op)()
if(op == bar)
{
if(this !is m_init)
writeln(non-static);
else
writeln(maybe static);
}
static @property Bar m_init()
{
return Bar.init;
}
alias m_init this;
}

void main()
{
Bar b;
Bar.bar();
readln;
}

If I understood you correctly, it must be compiled since there
isn't some 'bar' that is going to be used, right?
But it doesn't compile.


I honestly have no idea how opDispatch and alias this are 
supposed to
interact. They're both used as fallbacks when the type doesn't 
directly define
a function. That being said The reason that your code doesn't 
work here is the
fact that you'r ecalling bar is if it were a static function, 
but your
opDispatch isn't static. opDispatch would have to be static for 
it to be used
as a static function (it can't be both static and non-static), 
and in that

case, the if condition that you have in it wouldn't compile.

- Jonathan M Davis

:( If compiler tried alias this before opDispatch all would be OK.
Maybe I'm going to give up.
Thank you for comprehensive explanation.


static vs non-static

2013-01-13 Thread Zhenya

Hi!
Sorry,if it already was discussed,but

import std.stdio;

struct Foo
{
static void bar()
{
writeln(static);
}
void bar()
{
writeln(non-static);
}
}

int main()
{
Foo gun;
gun.bar();//fails here
}

Is it all right,that compiler dosn't prefer non-static function 
in ths case?


Re: static vs non-static

2013-01-13 Thread Zhenya

On Sunday, 13 January 2013 at 16:39:22 UTC, bearophile wrote:

Maxim Fomin:


dmd allows to call static functions on instance.


I think that's a D design mistake (and I think Jonathan Davis 
agrees with me), but Walter prefers the current behavour.


Bye,
bearophile


Maybe you could suggest some workaround?
for example I tried to use global function,but
in that case it never will be executed...


Re: static vs non-static

2013-01-13 Thread Zhenya

On Sunday, 13 January 2013 at 17:17:54 UTC, Maxim Fomin wrote:

On Sunday, 13 January 2013 at 16:23:27 UTC, Zhenya wrote:

On Sunday, 13 January 2013 at 16:18:36 UTC, Maxim Fomin wrote:
Yes, it is a problem - dmd allows to call static functions on 
instance. When both match, it issues ambiguity error.

Hmmm...So it will remain as it is?
It hurts me a little bit(


Frankly speaking I do not know - I keep an eye on D project for 
some period of time and remember how some features I considered 
stable were easily changed. If Walter is for current behavior - 
than it will remain as it is (unless Walter is overpersuaded).


What I would do is not writing code which is looking for 
problems.


BTW, take look at this thread 
http://forum.dlang.org/thread/pkodcsxwwehumhtkr...@forum.dlang.org 
(posts 1-3).


Thank you.


Re: static vs non-static

2013-01-13 Thread Zhenya

On Sunday, 13 January 2013 at 17:59:28 UTC, Andrey wrote:
Don't know if this will be useful in any manner, but it came 
this silly way:


class MyClass {

struct _static {

static void myfun() {
writeln(static myfun);
}
}

void myfun() {
writeln(myfun);
}

}

void main() {

auto obj = new MyClass();
obj.myfun(); //myfun
obj._static.myfun(); //static
MyClass._static.myfun(); //static

}

Aha...Thank you,very interesting idea to hide function in struct:


struct Foo
{
static struct bar
{
static void opCall()
{
writeln(static);
}
void bar()
{
writeln(non-static);
}
}
}

This is the workaround I looked for.


Re: static vs non-static

2013-01-13 Thread Zhenya

On Sunday, 13 January 2013 at 18:16:40 UTC, Zhenya wrote:

On Sunday, 13 January 2013 at 17:59:28 UTC, Andrey wrote:
Don't know if this will be useful in any manner, but it came 
this silly way:


class MyClass {

struct _static {

static void myfun() {
writeln(static myfun);
}
}

void myfun() {
writeln(myfun);
}

}

void main() {

auto obj = new MyClass();
obj.myfun(); //myfun
obj._static.myfun(); //static
MyClass._static.myfun(); //static

}
Aha...Thank you,very interesting idea to hide function in 
struct:



struct Foo
{
static struct bar
{
static void opCall()
{
writeln(static);
}
void bar()
{
writeln(non-static);
}
}
}

This is the workaround I looked for.


Oops...I made mistake:struct and function with the same name 
isn't allowed.

Okay, I will try to get around it somehow...


does alias this work correctly?

2013-01-13 Thread Zhenya

Hi!
Is it all right with it:
struct Foo
{
struct Bar
{
void opCall()
{
writeln(non-static);
}
}
Bar bar;
static struct Anotherbar
{
static void bar()
{
writeln(static);
}
}
alias Anotherbar this;
}

void main()
{
Foo f;
f.bar();
Foo.bar();//fils with message:type Bar is not an expression
Foo.Bar b;
b();//this works however
}
?


Re: does alias this work correctly?

2013-01-13 Thread Zhenya

On Sunday, 13 January 2013 at 19:35:08 UTC, Maxim Fomin wrote:

On Sunday, 13 January 2013 at 19:16:36 UTC, Zhenya wrote:

Hi!
Is it all right with it:
struct Foo
{
struct Bar
{
void opCall()
{
writeln(non-static);
}
}
Bar bar;
static struct Anotherbar
{
static void bar()
{
writeln(static);
}
}
alias Anotherbar this;
}

void main()
{
Foo f;
f.bar();
Foo.bar();//fils with message:type Bar is not an expression
   Foo.Bar b;
   b();//this works however
}
?


According to spec http://dlang.org/class.html#AliasThis 
undefined lookups are forwarded to AliasThis member. But in 
your case Foo struct has bar member, so no further resolution 
is performed. Rename the member and the code compiles.


Note, this seems to come from neighbor thread 
http://forum.dlang.org/thread/uelmpwwckcveimpbd...@forum.dlang.org. 
I think it is simpler to rename functions than create bunch of 
structs just to be able to have eponymous non-static and static 
functions.
I just want very much avoid renaming function,it's principle for 
me.

So I would like to know is my sample right or no.


Passing member-function to template

2013-01-12 Thread Zhenya

Hi!
Tell me please,is there any way to pass member-function to 
template?

I need something like that:

template execute(alias obj,alias mfun)
{
void execute()
{
obj.mfun();
}
}

struct Foo
{
   void nothing()
   {
   }
}

void main()
{
Foo f;
execute!(f,Foo.nothing)();
}

I could pass mfun by string,but I think it's a little bit ugly.


Re: Passing member-function to template

2013-01-12 Thread Zhenya

On Saturday, 12 January 2013 at 19:06:27 UTC, Maxim Fomin wrote:

On Saturday, 12 January 2013 at 18:17:47 UTC, Zhenya wrote:

Hi!
Tell me please,is there any way to pass member-function to 
template?


Probably this can help: (you can manually construct a delegate 
combining function and context pointers)


import std.stdio;

template execute(alias func)
{
void execute(void* ptr)
{
void delegate() dg;
  dg.funcptr = func;
  dg.ptr = ptr;
  dg();
}
}

struct Foo
{
   void nothing()
   {
writeln(reached);
   }
}

void main()
{
Foo f;
execute!(Foo.nothing)(f);
}


Thank you,it really can help me.
But I would like to handle not only member-function,but global 
function too(by UFCS) if it's possible.


Re: Passing member-function to template

2013-01-12 Thread Zhenya

On Saturday, 12 January 2013 at 19:24:04 UTC, Maxim Fomin wrote:

On Saturday, 12 January 2013 at 19:16:02 UTC, Zhenya wrote:
But I would like to handle not only member-function,but global 
function too(by UFCS) if it's possible.


Global functions rewritten as UFCS functions are not delegates, 
they are still functions, so they can be handled just as other 
functions. In such case it is enough to supply null to void* 
parameter.


Understood,thank you.


Re: is(...) with alias this

2013-01-06 Thread Zhenya

On Saturday, 5 January 2013 at 22:17:08 UTC, monarch_dodra wrote:
On Saturday, 5 January 2013 at 22:09:45 UTC, Philippe Sigaud 
wrote:
I think the alias this transformation is done 'before' any 
usual
conversion. I guess it's a real replacement inside the code 
(I'm not sure

how to explain my feeling).

But in any case, since OhWhy!(float) type is ... 
OhWhy!(float), I agree

this should fail.


Agreed. This warrants a ticket in bugzilla.


http://d.puremagic.com/issues/show_bug.cgi?id=9274


is(...) with alias this

2013-01-05 Thread Zhenya

Hi!
I just read the David's post 
http://forum.dlang.org/thread/kc9e74$bg7$1...@digitalmars.com


This code worked with dmd 2.060:

import std.stdio;
import std.traits;

struct OhWhy(S) {
   S[] arr;
   alias arr this;
}

void main() {
static assert(isArray!(OhWhy!(float)));

}

But fails with dmd 2.061:
ss.d(10): Error: static assert  (isArray!(OhWhy!(float))) is false

(same happens with alias this to static arrays and 
isArray/isStaticArray)



Is this intended or a regression? If latter, I'll submit a 
bug-ticket.


When I read this I understood that I don't know what should return
is(OhWhy!float unused == T[],T)

Of course OhWhy!float implicit convertable into float[].But on 
the other hand

is with == (not :) should check the type without conversions.
What do you think?

DMD 2.060 HEAD evaluate is(OhWhy!float unused == T[],T) - true


Re: Visual D 0.3.35 released - semantic analysis considered stable

2013-01-01 Thread Zhenya

On Tuesday, 1 January 2013 at 17:11:31 UTC, thedeemon wrote:
After switching to this version it started to build my windows 
app as a console one. It seems to ignore the subsystem choice, 
I don't see any mention of -L/SUBSYSTEM: in generated build 
scripts anymore.


Previously I had 0.3.33, I guess, which compiled and linked in 
one go, not in separate steps, it worked fine.


+1
I have the same problem.But it was alright when I used 0.3.34.


Re: typeid + toString = runtime error

2012-12-30 Thread Zhenya

On Sunday, 30 December 2012 at 16:04:48 UTC, Ali Çehreli wrote:

On 12/30/2012 07:32 AM, Zhenya wrote:

Hi!
Explain me please why this code fails in runtime:

import std.stdio;

class Foo
{
~this() {writeln(typeid(this).toString ~ is dead);}
}

void main()
{
new Foo;
}

Application error:
core.exception.InvalidMemoryOperationError


My guess is that by the time that destructor is executed, the 
runtime has been shut down sufficiently that the ~ operator 
cannot work. The following has the same error:


import std.stdio;

string foo()
{
return abc;
}

class Foo
{
~this() {writeln(foo() ~ xyz);}
}

void main()
{
new Foo;
}

Ali


Thank you,now all is clear for me.


DList!(Tuple!(TypeInfo_Class)) causes compilation error

2012-12-30 Thread Zhenya

Hi!
I just need DList!(Tuple!(TypeInfo_Class)) for my n-dimensional 
dispatcher,

but compiler says:

Error: function 
std.typecons.Tuple!(TypeInfo_Class).Tuple.opEquals!(const(Tuple!(TypeInfo_Class))).opEquals 
(const(Tuple!(TypeInfo_Class)) rhs) is not callable using 
argument types (const(Tuple!(TypeInfo_Class))) const


Could you give me some advice/workaround for this?


Re: DList!(Tuple!(TypeInfo_Class)) causes compilation error

2012-12-30 Thread Zhenya

On Sunday, 30 December 2012 at 20:09:57 UTC, Zhenya wrote:

Hi!
I just need DList!(Tuple!(TypeInfo_Class)) for my n-dimensional 
dispatcher,

but compiler says:

Error: function 
std.typecons.Tuple!(TypeInfo_Class).Tuple.opEquals!(const(Tuple!(TypeInfo_Class))).opEquals 
(const(Tuple!(TypeInfo_Class)) rhs) is not callable using 
argument types (const(Tuple!(TypeInfo_Class))) const


Could you give me some advice/workaround for this?


Used

struct Wrapper(T)
{
T t;
this(T a)
{
t = a;
}
alias t this;
}

Ugly a little bit,but works.

Could anyone explain me please what's wrong with 
Tuple!TypeInfo_Class opEquals?


Re: DList!(Tuple!(TypeInfo_Class)) causes compilation error

2012-12-30 Thread Zhenya

On Sunday, 30 December 2012 at 21:30:04 UTC, monarch_dodra wrote:

On Sunday, 30 December 2012 at 20:09:57 UTC, Zhenya wrote:

Hi!
I just need DList!(Tuple!(TypeInfo_Class)) for my 
n-dimensional dispatcher,

but compiler says:

Error: function 
std.typecons.Tuple!(TypeInfo_Class).Tuple.opEquals!(const(Tuple!(TypeInfo_Class))).opEquals 
(const(Tuple!(TypeInfo_Class)) rhs) is not callable using 
argument types (const(Tuple!(TypeInfo_Class))) const


Could you give me some advice/workaround for this?


Sounds like a bug in Tuple.opEqual: It, and its arguments, 
should be const qualified, but aren't. I'll try and get it 
fixed for 2.061.


In the mean time, you *could* try changing std.typecons.Tuple's 
opEqual:


From:
bool opEquals(R)(R rhs) if (isTuple!R)
To:
bool opEquals(R)(const R rhs) const if (isTuple!R)

But I have not thoroughly tested this, so use at your own risk.

Thank you,that works.



checking whether the number is NaN

2012-12-28 Thread Zhenya

Hi!
Tell me please,are there any way to check whether number is NaN?


Re: checking whether the number is NaN

2012-12-28 Thread Zhenya

On Friday, 28 December 2012 at 15:59:35 UTC, bearophile wrote:

Zhenya:

Tell me please,are there any way to check whether number is 
NaN?


http://dlang.org/phobos/std_math.html#isNaN

Bye,
bearophile


Thank you!


Re: private with alias this

2012-12-25 Thread Zhenya

On Tuesday, 25 December 2012 at 07:21:12 UTC, evilrat wrote:

On Tuesday, 25 December 2012 at 05:28:54 UTC, Zhenya wrote:

On Tuesday, 25 December 2012 at 02:43:52 UTC, r_m_r wrote:

On 12/25/2012 07:42 AM, r_m_r wrote:

assert(!__traits(compiles, b._bar));


sorry, i made a typo: it should be bar_ instead of _bar.

Interestingly, the below assertion fails at run time:

assert(!__traits(compiles, b.bar_));


but this won't compile (as expected):

assert(b.bar_ == 20); //main.d(15): Error: undefined 
identifier 'bar_'



regards,
r_m_r


So,should I file a new bug?Or it's alright and I don't 
understand something?


i don't think it's a bug, because you are just setting alias 
for a member. if you remove private from field it would work. 
i'm just wonder why do you want do something like this?


After your explaination I understood that it's really 
meaningless.Thank you)


Re: auto ref and non-templated functions

2012-12-24 Thread Zhenya
On Tuesday, 25 December 2012 at 01:40:16 UTC, Peter Alexander 
wrote:
On Tuesday, 25 December 2012 at 00:56:44 UTC, Peter Alexander 
wrote:
On Monday, 24 December 2012 at 17:40:54 UTC, Jonathan M Davis 
wrote:
And if that doesn't work, can we simply make it so that the 
compiler
automatically creates a variable when you pass an rvalue to a 
non-templated

auto ref function?


I don't see any problems with this, but I admittedly haven't 
thought too much about it.


If there are no problems with this way, then what I want to 
know is why the template version of auto ref wasn't implemented 
this way. The way auto ref is currently implemented for 
templates is a bit of a mess.
Maybe it's difficult to generate both versions because for the 
function like this


void foo(auto ref S s1,auto ref S s2,...,auto ref s10)

compiler should generate 2^10 versions of function foo.


Re: private with alias this

2012-12-24 Thread Zhenya

On Tuesday, 25 December 2012 at 02:43:52 UTC, r_m_r wrote:

On 12/25/2012 07:42 AM, r_m_r wrote:

assert(!__traits(compiles, b._bar));


sorry, i made a typo: it should be bar_ instead of _bar.

Interestingly, the below assertion fails at run time:

assert(!__traits(compiles, b.bar_));


but this won't compile (as expected):

assert(b.bar_ == 20); //main.d(15): Error: undefined identifier 
'bar_'



regards,
r_m_r


So,should I file a new bug?Or it's alright and I don't understand 
something?


const property don't wont return const reference to value

2012-12-23 Thread Zhenya

Hi!
Explain me please what's wrong with this code:

module main;

struct foo
{
int m_bar;
@property const ref int bar() const
{
return m_bar;
}
}

void main()
{
}

Error: cast(int)this.m_bar is not an lvalue


Re: const property don't wont return const reference to value

2012-12-23 Thread Zhenya
On Sunday, 23 December 2012 at 13:37:33 UTC, Rainer Schuetze 
wrote:



On 23.12.2012 14:20, Zhenya wrote:

@property const ref int bar() const


The first const does not bind to the return type, but to the 
whole declaration, so it does the same as the const at the end. 
You should use


@property ref const(int) bar() const


Thank you very much!


opDispatch bug?

2012-12-15 Thread Zhenya

Hi!
Is it a bug?

class Foo
{
int m_bar;
char m_gun;
@property auto ref opDispatch(string s)()
{
return mixin(m_~s); 
}
this(int i,char c)
{
		bar = i;//Error: undefined identifier bar, did you mean 
variable m_bar?

this.gun = c;
}
}


Re: opDispatch bug?

2012-12-15 Thread Zhenya

On Saturday, 15 December 2012 at 18:09:00 UTC, Ali Çehreli wrote:

On 12/15/2012 10:07 AM, Ali Çehreli wrote:

 Otherwise any type would go to opDispatch.

Wow. I made a typo in typo. :) That should be:

Otherwise any _typo_ would go to opDispatch.

Ali


It's pretty reasonable,thank you.


Re: Type value

2012-12-11 Thread Zhenya

On Tuesday, 11 December 2012 at 07:50:10 UTC, js.mdnq wrote:

On Tuesday, 11 December 2012 at 07:15:38 UTC, Zhenya wrote:

I'm sorry for my english.
I know that D != Python and my example don't need
any RTTI.

D has alias this feature.My example shows that we can use 
alias this with types.

And I just want use this:

struct Type(T)
{
  alias T m_type;
  alias m_type this;
}

int main()
{
  Type!int Int;
  Int i;//should compile by definition of alias this
}


My I ask, why not just use int directly?

I'm not proficient enough with D yet but it seems to me that 
the example does not make sense.


alias T m_type;
alias m_type this;

should be equivalent to

alias T this;

which, in some sense makes struct Type(T) equal to T... which 
is what you are thinking, I think.


But! I also believe that structs are meant as value types and 
are addressable.


So when you do

Int i; you are essentially doing something like

int x;
x i;

which makes no sense.

If you do this instead,

alias Type!int Int;
Int i;

then it will compile, of course.

But your way, when you do Int x; you are specifically trying to 
create a instance of an object. Yet you are not wanting it to 
be an object and I'm not sure what you are wanting x to be. 
(another type? a type of a type?)


In my example Int is value of Type!int.
When I write
Int i;
If Int had not alias this it really would not have sense,because 
type expected
instead of 'Int'.But Int have alias this 'int',and can be 
replaced by it.

My reason is simple:
Operators are functions.But if I want declare some struct like 
std.TypeTuple

I will need opSlice that return type.
It is impossible.
But if my example compiled,I could return value,that can be used 
as type.

It's something like Andrei's Type2Type.





Re: Type value

2012-12-11 Thread Zhenya

On Tuesday, 11 December 2012 at 09:40:08 UTC, Ali Çehreli wrote:

On 12/11/2012 01:05 AM, Ali Çehreli wrote:

 /* Fundamental types must be made differently; probably due
to syntax
 * issues. I would have expected for the following to work:
 *
 * return Type!index(args); // -- compilation error
 */

My mind must have been in C++ mode there. :) The following is 
not legal in D:


int foo()
{
return int(42);// not D syntax!
}

But of course the simpler code below works:

/* A generic function that can make an object of any type. */
Type!index make(size_t index, T...)(T args)
{
// ...

} else {
return args[0];// simply return the argument
}
}

Ali

Thank you for you example,I understand.
But how do you think does it make sense to open enhancement
allow alias this with types?
Or it is completely useless?


Type value

2012-12-10 Thread Zhenya

Hi!
In some previous post I asked about possibility of declar 
opIndex,that return type.

I thoght about it,and I understood that code like this is legal:

struct Type(T)
{
alias T m_type;
alias m_type this;
}

void main()
{
Type!int Int;
//  Int a;
}

Is it hard to implement possibility of using Int as a type?
It would allow functions,that return types:)


Re: Type value

2012-12-10 Thread Zhenya

I'm sorry for my english.
I know that D != Python and my example don't need
any RTTI.

D has alias this feature.My example shows that we can use alias 
this with types.

And I just want use this:

struct Type(T)
{
   alias T m_type;
   alias m_type this;
}

int main()
{
   Type!int Int;
   Int i;//should compile by definition of alias this
}


VisualD solution configuration

2012-12-02 Thread Zhenya

Hi!
I'm sorry,maybe it is a little bit stupid question,but how to 
configure VisualD project to get it compile not only main.d but 
all files in project?


Re: VisualD solution configuration

2012-12-02 Thread Zhenya

On Sunday, 2 December 2012 at 14:52:24 UTC, Lubos Pintes wrote:
Not sure if I understand what you ask here, but you probably 
need to add all files you want to compile to the project. Thus 
not only the main.d, but all modules.

Dňa 2. 12. 2012 11:56 Zhenya  wrote / napísal(a):

Hi!
I'm sorry,maybe it is a little bit stupid question,but how to 
configure
VisualD project to get it compile not only main.d but all 
files in project?


I added 2 modules in project.But compiled only one.



Re: VisualD solution configuration

2012-12-02 Thread Zhenya

On Sunday, 2 December 2012 at 14:58:24 UTC, Zhenya wrote:

On Sunday, 2 December 2012 at 14:52:24 UTC, Lubos Pintes wrote:
Not sure if I understand what you ask here, but you probably 
need to add all files you want to compile to the project. Thus 
not only the main.d, but all modules.

Dňa 2. 12. 2012 11:56 Zhenya  wrote / napísal(a):

Hi!
I'm sorry,maybe it is a little bit stupid question,but how to 
configure
VisualD project to get it compile not only main.d but all 
files in project?


I added 2 modules in project.But compiled only one.


I have the following errors:

Error 42: Symbol Undefined _D10dispatcher7__arrayZ
Debug\space.obj(space)
 Error 42: Symbol Undefined _D10dispatcher12__ModuleInfoZ



Re: VisualD solution configuration

2012-12-02 Thread Zhenya

On Sunday, 2 December 2012 at 15:03:26 UTC, Zhenya wrote:

On Sunday, 2 December 2012 at 14:58:24 UTC, Zhenya wrote:

On Sunday, 2 December 2012 at 14:52:24 UTC, Lubos Pintes wrote:
Not sure if I understand what you ask here, but you probably 
need to add all files you want to compile to the project. 
Thus not only the main.d, but all modules.

Dňa 2. 12. 2012 11:56 Zhenya  wrote / napísal(a):

Hi!
I'm sorry,maybe it is a little bit stupid question,but how 
to configure
VisualD project to get it compile not only main.d but all 
files in project?


I added 2 modules in project.But compiled only one.


I have the following errors:

Error 42: Symbol Undefined _D10dispatcher7__arrayZ
Debug\space.obj(space)
 Error 42: Symbol Undefined _D10dispatcher12__ModuleInfoZ


I added line module dispatcher; in file dispatcher.d and all 
compiled.

Strange,I thought module name inferred from filename.


Re: VisualD solution configuration

2012-12-02 Thread Zhenya

On Sunday, 2 December 2012 at 20:12:08 UTC, Regan Heath wrote:
On Sun, 02 Dec 2012 15:08:51 -, Zhenya zh...@list.ru 
wrote:



On Sunday, 2 December 2012 at 15:03:26 UTC, Zhenya wrote:

On Sunday, 2 December 2012 at 14:58:24 UTC, Zhenya wrote:
On Sunday, 2 December 2012 at 14:52:24 UTC, Lubos Pintes 
wrote:
Not sure if I understand what you ask here, but you 
probably need to add all files you want to compile to the 
project. Thus not only the main.d, but all modules.

Dňa 2. 12. 2012 11:56 Zhenya  wrote / napísal(a):

Hi!
I'm sorry,maybe it is a little bit stupid question,but how 
to configure
VisualD project to get it compile not only main.d but all 
files in project?


I added 2 modules in project.But compiled only one.


I have the following errors:

Error 42: Symbol Undefined _D10dispatcher7__arrayZ
Debug\space.obj(space)
Error 42: Symbol Undefined _D10dispatcher12__ModuleInfoZ


I added line module dispatcher; in file dispatcher.d and all 
compiled.

Strange,I thought module name inferred from filename.


What folder was main.d in, and what folder was dispatcher.d in?

R


These are in the same folder




Can operators return type?

2012-11-29 Thread Zhenya

Hi!
It would useful for some my project,if operators could be a 
template,that return

type.Something like

alias TypeTuple!(int,char) types;

static assert(types[1] == char) //opIndex

So can I define something like that?


Re: Can operators return type?

2012-11-29 Thread Zhenya

On Thursday, 29 November 2012 at 16:55:01 UTC, bearophile wrote:

Zhenya:

It would useful for some my project,if operators could be a 
template,that return

type.


D operators are functions, and D functions return values. And 
in D types are not values (unlike Python and several other 
languages), unless you use a Typeinfo or something.


Maybe if you explain better what you are trying to do someone 
will be able to suggest you an alternative solution. D is not 
as flexible as Lisp, but it doesn't lack 
meta-programming/type-processing capabilities.


Bye,
bearophile


Of course I understand,that type is not value in D.I meant that 
maybe we can declare non-function template operators.For example:


struct MyIntType
{
alias int type;
template opSlice()
{
alias type opSlice;
}


Re: Can operators return type?

2012-11-29 Thread Zhenya

On Thursday, 29 November 2012 at 21:53:20 UTC, bearophile wrote:

Zhenya:


For example:

struct MyIntType
{
alias int type;
template opSlice()
{
alias type opSlice;
}


As you guess, this is not supported in D.

Bye,
bearophile


Thank you,understood(


Re: SList/DList ranges

2012-11-27 Thread Zhenya
On Tuesday, 27 November 2012 at 07:51:16 UTC, Jonathan M Davis 
wrote:

On Monday, November 26, 2012 19:49:51 Zhenya wrote:

Hi!
I read the spec,but I didn't find any function,that removes
concrete element from
list.I am not familiar with D's ranges,so could you help me
please?


What do you mean by removing concrete elements? How is that any 
different from
any other element in the list? If you want to remove elements 
from the list,
then use one of the *remove functions. [] on the list to get a 
range over all
of the elements in the list, find to find the element that you 
want, and then
take to get a range with just the elements from the front of 
the range that

you want to remove. Hopefully this will enlighten you somewhat:

import std.algorithm;
import std.container;
import std.range;
import std.stdio;

void main()
{
auto list = make!(SList!int)(4, 5, 6, 7, 22, 9, 5, 4);
assert(equal(list[], [4, 5, 6, 7, 22, 9, 5, 4]));
auto found = find(list[], 6);
assert(equal(found.save, [6, 7, 22, 9, 5, 4]));
list.linearRemove(take(found, 3));
assert(equal(list[], [4, 5, 9, 5, 4]));
list.linearRemove(take(find(list[], 5), 1));
assert(equal(list[], [4, 9, 5, 4]));
}

Unfortunately, std.container needs a fair bit of work in terms 
of some of the
details - particularly with its functions which accept and/or 
return ranges,
so it doesn't always work like it should yet. It's very much a 
work in
progress right now. But the above should give you the basic 
idea.


As for ranges in general, the best resource on them at this 
point would be

this chapter from an online book on D:

http://ddili.org/ders/d.en/ranges.html

If you're going to be using Phobos much (which is likely if 
you're using D),
then you're going to need to understand ranges, because Phobos 
uses them quite

heavily.

- Jonathan M Davis


Thank you,understood.I just want a container with fast 
insert/remove.Something like C++ std::list.But when I look at you 
example I understand that remove is linear,so maybe I need 
assosiative array or RedBlackTree.

And thank you for a link,I will learn ranges surely.



SList/DList ranges

2012-11-26 Thread Zhenya

Hi!
I read the spec,but I didn't find any function,that removes 
concrete element from
list.I am not familiar with D's ranges,so could you help me 
please?


Re: std.signals2 proposal

2012-11-05 Thread Zhenya

On Monday, 5 November 2012 at 13:35:26 UTC, Robert wrote:

Hi there!

I just developed a proof-of-concept implementation of an 
improved

std.signals.

Things I did not like about the current implementation:

1. Passing a delegate to connect, which basically is an 
extended (void*)
and assuming it is an object delegate, does not feel quite 
right in D,
where we usually try to guarantee correctness by the compiler 
wherever

possible.

2. The restriction that only objects can be connected.

Point 2 does not really bother me, because from my little 
experience I
never really had connected anything else than objects to a 
signal. But
the restriction that the connected delegate must not be some 
wrapper, is
quite a restriction I came to dislike about Qt and even more so 
with
this implementation because unlike Qt the signatures have to 
match

exactly, you can't omit parameters, like:
// Qt code
connect(button, SIGNAL(clicked(bool)), this, 
SLOT(buttonClicked());


- In the current implementation buttonClicked would have to 
take a

bool.

In addition boost::signals together with boost::bind offered 
even more
comfort like passing additional parameters to a slot, which is 
really

very, very useful:

for(int i=0; ibuttons.length(); i++) {
buttons[i].clicked.connect(boost::bind(MyObj::addNumber, this,
 i));
}

So I tried to improve std.signals, code:
(warning at least one bug is remaining, explained below)

https://github.com/eskimor/phobos/tree/new_signal

You can easily connect to an object's method with:

obj.signal.connect!someMethod(obj);

instead of the old implementation:

obj.signal.connect(obj.someMethod);

- The interface is clean and type safe, all the ugly details 
are hidden
from the user. And it is just one character more typing. Simple 
things

stay simple.

In addition a method allowing wrapper delegates was added:

class Observer {
void addNumber(int i) {
sum+=i;
}
int sum;
}

class Button {
Signal!(bool) clicked;
// ...
}

void main() {
auto b=new Button;
auto o=new Observer;
// Ignore boolean parameter and pass some int:
b.connect!Observer(o, (o, p) { o.addNumber(7); });
// or even:
b.connect!Observer(o, (o, p) = o.addNumber(7));
// For some reason the compiler is not able to deduce o being
// Observer, so the !Observer is needed, but it is still very
// neat and readable.
}

Thanks to D's lamdas the syntax is even more concise as 
boost::bind and

far more powerful.

By passing the object explicitly to the delegate, it is 
possible to
maintain the 'weak ref' semantics to the target object, while 
ensuring

that the delegates context won't be freed.

As a side effect it is now even possible to use struct 
delegates or even
any non object delegate. Simply pass null for the obj 
parameter. It is
completely safe, the only drawback is that the struct won't be 
deleted
until the Button gets destroyed. (Because it holds a reference 
to the
struct, by means of the delegate.) But for long lived structs 
this

usually is perfectly acceptable.

Implementation:

In my implementation I changed the Signal mixin to be a simple 
template
struct, because I hit strange compiler errors with it being a 
mixin. The

most prominent:

std/signals.d(443): Error: no overload matches for 
connect(string

method,T2) if (is(T2 : Object))

You can find the version triggering these errors at:

https://github.com/eskimor/phobos/tree/new_signal_mixin

Also I did not really get the point why a mixin was used in the 
first
place, it does not really gain us anything? What was the 
reasoning about

it?
I almost thought I found the reason, because my implementations 
suffers
from unhook not being called, although it was properly 
registered with
rt_attachDisposeEvent(obj, unhook);, thus causing a 
segmentation

fault when the observer gets deleted. I did not really find any
difference from the original version that could explain this 
behavior,
despite the original implementation being a mixin. So I 
thought, well
maybe the delegate passed to rt_attachDisposeEvent(obj, 
unhook); must
be an object delegate (that's would be why the mixin was 
needed), but
after digging in object_.d I did not find any code assuming 
that the

delegate was an object delegate. Any ideas on this?

Another thing I'd like to ask Walter, is what the L1: label 
is for in
connect(), is it just some left over or has it some special 
internal

compiler thing meaning?

What do you think?

Best regards,

Robert


Hi!Could you write some examples for struct and non-object 
delegates?




Re: std.signals2 proposal

2012-11-05 Thread Zhenya

On Monday, 5 November 2012 at 19:07:13 UTC, Robert wrote:


Hi!Could you write some examples for struct and non-object 
delegates?




Sure!

Something like:

struct Observer {
void observe(int a, int b) {
// ...
}
}

void main() {
Signal!(int, int) s1;
Signal!int s2
Observer o;
	s1.connect!Object(null, (null_object, a, b) = o.observe(a, 
b));

s2.connect!Object(null, (null_object, a) = o.observe(7, a));

}

Having the delegate accept a null parameter might not be 
pretty, but I
consider this a good thing, because of the changed semantics: 
The signal
will keep a reference to the struct now, so the signals weak 
reference
semantics are no longer in place. (If struct is allocated on 
the heap,

it won't be freed as long as the signal is alive.)
But it is possible and safe. And if you know what you are doing 
also

very reasonable.

But the main benefit is not that you can connect to structs 
(which is a
side effect), but that you can use wrapping delegates which do 
parameter
adoptions. That's the killer feature that proved to be so 
indispensable

and neat for me and others.

If really required it would not be to hard to provide an 
overload of
connect() which takes a struct pointer directly, just like the 
one
taking an object, but because of the changed semantics and the 
rare uses
I'd expect, probably not worthwhile. But comments are 
appreciated.


I am embarrassed a little that a member function of the structure 
looks like a static function,maybe it would be better if connect 
took struct pointer directly.I think if struct become immortal it 
will not be a big trouble,in other

case we have disconnect,that will help us.

Sorry for my awful english


Re: Unexpected OPTLINK Termination while building dwt helloworld

2012-10-31 Thread Zhenya
On Wednesday, 31 October 2012 at 18:05:07 UTC, Jacob Carlborg 
wrote:

On 2012-10-31 16:08, Zhenya wrote:


Thank you)
Maybe I will try it,although I do not know how.


1. Create a new folder, dlang
2. In the dlang folder clone DMD, druntime and Phobos using 
this command git clone address where address is one of 
the git addresses below

3. Install DVM
4. From dlang run dvm compile .
5. You will have a compiled binary in dlang\dmd\bin32\dmd

DMD: git://github.com/D-Programming-Language/dmd.git
druntime: git://github.com/D-Programming-Language/druntime.git
Phobos: git://github.com/D-Programming-Language/phobos.git
DVM: https://bitbucket.org/doob/dvm/wiki/Home


Thank you very much:)


Re: Unexpected OPTLINK Termination while building dwt helloworld

2012-10-31 Thread Zhenya

On Wednesday, 31 October 2012 at 18:48:20 UTC, Zhenya wrote:
On Wednesday, 31 October 2012 at 18:05:07 UTC, Jacob Carlborg 
wrote:

On 2012-10-31 16:08, Zhenya wrote:


Thank you)
Maybe I will try it,although I do not know how.


1. Create a new folder, dlang
2. In the dlang folder clone DMD, druntime and Phobos using 
this command git clone address where address is one of 
the git addresses below

3. Install DVM
4. From dlang run dvm compile .
5. You will have a compiled binary in dlang\dmd\bin32\dmd

DMD: git://github.com/D-Programming-Language/dmd.git
druntime: git://github.com/D-Programming-Language/druntime.git
Phobos: git://github.com/D-Programming-Language/phobos.git
DVM: https://bitbucket.org/doob/dvm/wiki/Home


Thank you very much:)


Unfortunally this don't help.If anybody build dwt heloworld under 
windows succesfully,write please short howto.


DFL Button.backColor

2012-10-30 Thread Zhenya

Hi!

Explain me please,why this code doesn't work

import dfl.all;

void main()
{
auto form = new Form;
auto button = new Button;
button.backColor = Color(0,0,0);
button.foreColor = Color(0,0,0);
form.controls.add(button);
Application.run(form);
}

It displays usualy button in system style,but should display 
black button


Re: DFL Button.backColor

2012-10-30 Thread Zhenya

On Tuesday, 30 October 2012 at 13:34:21 UTC, Zhenya wrote:

Hi!

Explain me please,why this code doesn't work

import dfl.all;

void main()
{
auto form = new Form;
auto button = new Button;
button.backColor = Color(0,0,0);
button.foreColor = Color(0,0,0);
form.controls.add(button);
Application.run(form);
}

It displays usualy button in system style,but should display 
black button


Oh,sorry,I should check it 
http://wiki.dprogramming.com/Dfl/BugList

Thank you:)


__traits(compiles,...) = ? is(typeof(...))

2012-10-29 Thread Zhenya

Hi!

Tell me please,in this code first and second static if,are these 
equivalent?
with arg = 1, __traits(compiles,check(arg);) = true, 
is(typeof(check(arg))) = false.


template ArgType(alias arg)
{
void check(T)(ref T t) {};
//  static if(__traits(compiles,check(arg);))
static if(is(typeof(check(arg
{
struct ArgType
{
typeof(arg)* m_ptr;
this(ref typeof(arg) r)
{
m_ptr = r;
}
@property ref typeof(arg) get()
{
return *m_ptr;
}
alias get this;
}
}
else
alias typeof(arg) ArgType;
}


Re: __traits(compiles,...) = ? is(typeof(...))

2012-10-29 Thread Zhenya

On Monday, 29 October 2012 at 10:58:51 UTC, Philippe Sigaud wrote:
Tell me please,in this code first and second static if,are 
these equivalent?

with arg = 1, __traits(compiles,check(arg);) = true,
is(typeof(check(arg))) = false.


__traits(compiles, ...) takes an expression, not a string. From 
the
spec: The arguments can be symbols, types, or expressions that 
are

syntactically correct. The arguments cannot be statements or
declarations.

(http://dlang.org/traits.html#compiles)

So, in your case, that would be:

__traits(compiles, {check(arg);})

Note that you can also use the type of arg:

__traits(compiles, {check(typeof(arg).init);})

or

is(typeof({ check(typeof(arg).init);})

Thank you,understood.



Re: TypeInfo manipulation

2012-10-27 Thread Zhenya

On Friday, 26 October 2012 at 19:57:14 UTC, Zhenya wrote:

On Thursday, 25 October 2012 at 15:05:05 UTC, Zhenya wrote:

Hi!
Tell me please,are any TypeInfo/typeid/classinfo manipulations 
possible?
For example I need a struct that overload typeid, or something 
like that?


Some time ago I tried to write some smart pointer that overlad 
classinfo property  in accordance with the real type of hold 
object,but I failed to do this becouse of
not having possibility to declar static and non-static version 
of the one function.Maybe there are better ways to do this?


Or maybe there are a possibility to parametrize class by 
TypeInfo?


Is my question is so meaningless, that no one even say why? :(


Re: TypeInfo manipulation

2012-10-27 Thread Zhenya

What do you mean: to parametrize class by Typeinfo?

class A { }
A!TypeInfo var;

If you mean this, than how it can help?


It would be well if I could create object that inherits class 
with this typeinfo.


Re: TypeInfo manipulation

2012-10-27 Thread Zhenya

I have double dispatcher:

template Dispatcher(R)
{
	R execute(Left,Right)(R delegate(Left,Right) f,Object 
left,Object right)

{
return f(cast(Left)left,cast(Right)right);
}
struct Dispatcher
{
		private R 
delegate(Object,Object)[Tuple!(TypeInfo_Class,TypeInfo_Class)] 
m_callbacks;

void add(Left,Right)(R delegate(Left,Right) f)
{
			m_callbacks[tuple(Left.classinfo,Right.classinfo)] = (Object 
l,Object r){return execute!(Left,Right)(f,l,r);};

}
void remove(Left,Right)()
{

m_callbacks.remove(tuple(Left.classinfo,Right.classinfo));
}
R opCall(Object l,Object r)
{
			R delegate(Object,Object)* callback = 
tuple(l.classinfo,r.classinfo) in m_callbacks;

return (*callback)(l,r);
}
}
}

And I have some class ierarhy:

class GameObject
{
}

class Foo : GameObject
{
}

class Bar : GameObject
{
}

problem is to create some wrapper to this classes

struct GameHandle(Type)

and this code work


void main()
{
Dispatcher!void d;

d.add!(GameHandle!Foo,GameHandle!Bar)((GameHandle!Foo,GameHandle!Bar){writeln(haha);});
GameHandle!GameObject a;a.m_hold = new Foo;
GameHandle!GameObject b;b.m_hold = new Bar;
d(a,c);
readln();
}


Re: TypeInfo manipulation

2012-10-26 Thread Zhenya

On Thursday, 25 October 2012 at 15:05:05 UTC, Zhenya wrote:

Hi!
Tell me please,are any TypeInfo/typeid/classinfo manipulations 
possible?
For example I need a struct that overload typeid, or something 
like that?


Some time ago I tried to write some smart pointer that overlad 
classinfo property  in accordance with the real type of hold 
object,but I failed to do this becouse of
not having possibility to declar static and non-static version 
of the one function.Maybe there are better ways to do this?


Or maybe there are a possibility to parametrize class by TypeInfo?


TypeInfo manipulation

2012-10-25 Thread Zhenya

Hi!
Tell me please,are any TypeInfo/typeid/classinfo manipulations 
possible?
For example I need a struct that overload typeid, or something 
like that?


Some time ago I tried to write some smart pointer that overlad 
classinfo property  in accordance with the real type of hold 
object,but I failed to do this becouse of
not having possibility to declar static and non-static version of 
the one function.Maybe there are better ways to do this?


static and non-static version of the one function

2012-10-24 Thread Zhenya

Hi!
Tell me please,are there any way to declare two versions of one 
member function,

something like this:

int a;

int.init //static version
a.init //non-static version


Re: static and non-static version of the one function

2012-10-24 Thread Zhenya
On Wednesday, 24 October 2012 at 20:02:20 UTC, Jacob Carlborg 
wrote:

On 2012-10-24 21:32, Zhenya wrote:

Hi!
Tell me please,are there any way to declare two versions of 
one member

function,
something like this:

int a;

int.init //static version
a.init //non-static version


No, not currently.

http://d.puremagic.com/issues/show_bug.cgi?id=3345


Thank you,understood.


Re: SFML-D working example

2012-10-21 Thread Zhenya

On Sunday, 21 October 2012 at 03:37:20 UTC, Ellery Newcomer wrote:

On Saturday, 20 October 2012 at 20:25:09 UTC, Zhenya wrote:
Hi!I have a little problem with building example.I downloaded 
SFML-D working example at this adress 
https://github.com/krzat/SFML-D/downloads.But then I tried to 
build it myself,I received many errors like that:
Symbol Undefined 
_D2sf8graphics7Texture6__ctorMFPxaPS2sf8graphics7IntRectZC2sf8graphics7Texture


I don't understand why compiler isn't satisfied,becouse I 
include all .lib files,that is in example.Can anybody help my?


dmd_build.bat works for me. Are you using it?


No,but I created VisualD solution,and configured its import path 
and lib path.




Re: SFML-D working example

2012-10-21 Thread Zhenya

On Sunday, 21 October 2012 at 07:34:29 UTC, Zhenya wrote:
On Sunday, 21 October 2012 at 03:37:20 UTC, Ellery Newcomer 
wrote:

On Saturday, 20 October 2012 at 20:25:09 UTC, Zhenya wrote:
Hi!I have a little problem with building example.I downloaded 
SFML-D working example at this adress 
https://github.com/krzat/SFML-D/downloads.But then I tried to 
build it myself,I received many errors like that:
Symbol Undefined 
_D2sf8graphics7Texture6__ctorMFPxaPS2sf8graphics7IntRectZC2sf8graphics7Texture


I don't understand why compiler isn't satisfied,becouse I 
include all .lib files,that is in example.Can anybody help my?


dmd_build.bat works for me. Are you using it?


No,but I created VisualD solution,and configured its import 
path and lib path.

Oh,I'm sorry I made some mistakes.
Thank you for your answer)



SFML-D working example

2012-10-20 Thread Zhenya
Hi!I have a little problem with building example.I downloaded 
SFML-D working example at this adress 
https://github.com/krzat/SFML-D/downloads.But then I tried to 
build it myself,I received many errors like that:
 Symbol Undefined 
_D2sf8graphics7Texture6__ctorMFPxaPS2sf8graphics7IntRectZC2sf8graphics7Texture


I don't understand why compiler isn't satisfied,becouse I include 
all .lib files,that is in example.Can anybody help my?




non-const reference to const instance of class

2012-10-10 Thread Zhenya

Hi!

I thought that this should compile:
class Foo{}

const(Foo) foo = new Foo;// the same that const Foo foo?
foo = new Foo;

but compiler say that foo is const reference and it can't modify 
it.
It is normally?If yes,how can I declare non-const reference to 
const instance of class?


Re: non-const reference to const instance of class

2012-10-10 Thread Zhenya
On Wednesday, 10 October 2012 at 17:35:48 UTC, Jonathan M Davis 
wrote:

On Wednesday, October 10, 2012 19:02:31 Zhenya wrote:

Hi!

I thought that this should compile:
class Foo{}

const(Foo) foo = new Foo;// the same that const Foo foo?
foo = new Foo;

but compiler say that foo is const reference and it can't 
modify

it.
It is normally?If yes,how can I declare non-const reference to
const instance of class?


const Foo and const(Foo) are the same thing. They both create a 
const
reference to const data. This is in contrast to a pointer where 
const Bar* and
const(Bar)* are different. With a reference, there is no way to 
indicate that
the object is const but not the reference. The type system just 
doesn't
support the idea of a class object existing separately from a 
reference, so

there's no way to make that distinction.

If you want to have a mutable reference to a const object, then 
you need a
wrapper around the reference where the wrapper is mutable but 
the reference
isn't. std.typecons.Rebindable does this. It's what you should 
use.


- Jonathan M Davis


Thank you)


this() in struct

2012-10-09 Thread Zhenya

Hi!
I'm sorry,maybe this topic already was discussed,but could 
anybody explain me

why default constructor was disallowed in structs?


Re: this() in struct

2012-10-09 Thread Zhenya

On Tuesday, 9 October 2012 at 17:21:47 UTC, Zhenya wrote:

Hi!
I'm sorry,maybe this topic already was discussed,but could 
anybody explain me

why default constructor was disallowed in structs?


And if I have to do some initialization of data members,what is 
the way to do it?


Re: this() in struct

2012-10-09 Thread Zhenya
On Tuesday, 9 October 2012 at 18:29:18 UTC, Jonathan M Davis 
wrote:

On Tuesday, October 09, 2012 19:08:35 Zhenya wrote:

On Tuesday, 9 October 2012 at 17:21:47 UTC, Zhenya wrote:
 Hi!
 I'm sorry,maybe this topic already was discussed,but could
 anybody explain me
 why default constructor was disallowed in structs?

And if I have to do some initialization of data members,what is
the way to do it?


It's because of the init property. All types in D are 
default-initialized to
their init property, and that property must be known at compile 
time (meaning
that it doesn't work very well for it to involve a 
constructor). For structs,
the init property is decided by what all of the member 
variables are directly

initialized to. So,

struct S
{
 int i = 5;
 string s = hello;
}

assert(S.init == S(5, hello));

S s;
assert(s == S.init);

This has the unfortunate result that we don't get a default 
constructor. The

workaround is to declare a static opCall function.

struct S
{
 int i = 5;
 string s = hello;

 static S opCall()
 {
 return S(22, catch);
 }
}

S s;
assert(s == S.init);

S s2 = S();
auto s3 = S();
assert(s2 == S(22, catch));
assert(s2 == s3);

It doesn't make it so that the struct is default-initialized to 
the result of
static opCall, and it doesn't guarantee that the result of 
static opCall is
used when no constructor is called, but if you use S() 
explicitly, then it

will be used.

A solid design decision in the language (e.g. requiring that 
everything be
default-initialized) can have the unfortunate side effect of 
restricting other
choices, and in this case, mucks up default constructors for 
structs.


- Jonathan M Davis


Ok.Then can I do my own .init property that can be executed in 
compile-time?




Re: this() in struct

2012-10-09 Thread Zhenya
On Tuesday, 9 October 2012 at 18:29:18 UTC, Jonathan M Davis 
wrote:

On Tuesday, October 09, 2012 19:08:35 Zhenya wrote:

On Tuesday, 9 October 2012 at 17:21:47 UTC, Zhenya wrote:
 Hi!
 I'm sorry,maybe this topic already was discussed,but could
 anybody explain me
 why default constructor was disallowed in structs?

And if I have to do some initialization of data members,what is
the way to do it?


It's because of the init property. All types in D are 
default-initialized to
their init property, and that property must be known at compile 
time (meaning
that it doesn't work very well for it to involve a 
constructor). For structs,
the init property is decided by what all of the member 
variables are directly

initialized to. So,

struct S
{
 int i = 5;
 string s = hello;
}

assert(S.init == S(5, hello));

S s;
assert(s == S.init);

This has the unfortunate result that we don't get a default 
constructor. The

workaround is to declare a static opCall function.

struct S
{
 int i = 5;
 string s = hello;

 static S opCall()
 {
 return S(22, catch);
 }
}

S s;
assert(s == S.init);

S s2 = S();
auto s3 = S();
assert(s2 == S(22, catch));
assert(s2 == s3);

It doesn't make it so that the struct is default-initialized to 
the result of
static opCall, and it doesn't guarantee that the result of 
static opCall is
used when no constructor is called, but if you use S() 
explicitly, then it

will be used.

A solid design decision in the language (e.g. requiring that 
everything be
default-initialized) can have the unfortunate side effect of 
restricting other
choices, and in this case, mucks up default constructors for 
structs.


- Jonathan M Davis


Ok.Then,can I do my own .init property,that can be executed in 
compile time?


Re: this() in struct

2012-10-09 Thread Zhenya
On Tuesday, 9 October 2012 at 19:04:40 UTC, Jonathan M Davis 
wrote:

On Tuesday, October 09, 2012 20:09:56 Zhenya wrote:

Ok.Then can I do my own .init property that can be executed in
compile-time?


No. You directly initialize the member variables to what you 
want them to be,
and that's the values that they have in the init property. You 
can't have
anything like a function or constructor to initialize them all 
together.
However, you _can_ use the results of functions to initialize 
the member

variables if the functions will work at compile time. e.g.

struct S
{
 int i = foo();
 string s = bar(joe);
}

int foo()
{
 return 7;
}

string bar(string str)
{
 return str ~  schmoe;
}

assert(S.init == S(7, joe schmoe));

- Jonathan M Davis


Understood.Thank you very much guys =)




CFTE+DevIL=?

2012-10-08 Thread Zhenya

Hi!
I need to load some textures for my game,but I woud like to do it 
in compile time.
I know that CTFE imposes restrictions on functions.So can I 
execute some DevIL(Derelict3) functions?


Re: compiler assertion

2012-09-29 Thread Zhenya
On Saturday, 29 September 2012 at 13:03:31 UTC, Philippe Sigaud 
wrote:

On Fri, Sep 28, 2012 at 10:32 PM, Zhenya zh...@list.ru wrote:


Thank you,understood.


This should work, hopefully:

import std.stdio;
import std.typetuple;

template sum(U...)
{
static if(U.length == 0)
enum sum = 0;
else
enum sum = U[0]+sum!(U[1..$]);
}

void main()
{
enum s = sum!(1,2,3,4);
writeln(s);
}

You can also do it with a standard function, and forcing its 
execution

at compile-time by using 'enum':


U[0] sum(U...)(U u) if (U.length  0)
{
U[0] result =0;
foreach(value;u)
result += value;
return result;
}

void main()
{
enum s = sum(1,2,3,4);
writeln(s);
}

Note that doing that without any check on the U's is a bit 
dangerous.


I just wanted to do something like template,that takes value 
parametres and deduce it's types,so I guess that I can use code 
that you wrote for it with some type checking.




compiler assertion

2012-09-28 Thread Zhenya

Hi!
Is it normally,that this simple code does'nt compile with this 
assertion:
dmd: template.c:5542: Identifier* 
TemplateInstance::genIdent(Objects*): Assertion `global.errors' 
failed.


import std.stdio;
import std.typetuple;

template sum(T:TypeTuple!U,U...)
{
static if(U.length == 0)
enum sum = 0;
else
enum sum = U[0]+sum!(U[1..$]);
}

void main()
{
enum s = sum!(1,2,3,4);
writeln(s);
}

?



Re: compiler assertion

2012-09-28 Thread Zhenya
On Friday, 28 September 2012 at 20:27:07 UTC, Jonathan M Davis 
wrote:

On Friday, September 28, 2012 22:19:56 Zhenya wrote:

Hi!
Is it normally,that this simple code does'nt compile with this
assertion:
dmd: template.c:5542: Identifier*
TemplateInstance::genIdent(Objects*): Assertion `global.errors'
failed.


It's always a bug in the compiler if you see a compiler 
assertion. What you're

seeing is probably this bug:

http://d.puremagic.com/issues/show_bug.cgi?id=8421

- Jonathan M Davis


Thank you,understood.


  1   2   >