LNK2019 error from using a function pointer to core.bitop functions?

2015-07-15 Thread Matthew Gamble via Digitalmars-d-learn
This member function of my struct uses a function pointer btx. 
When the line declaring the function pointer is present I get a 
LNK2019 error: unresolved external symbol.


bool opIndexAssign(bool value, size_t[2] inds)
{
	int function(size_t*, size_t) btx = (value) ? bts : btr; // 
error is here


// other stuff here

	for (size_t i = startBitInd; i  startWordBitDone; ++i) 
btx(bitArray[startWord], i);


// other stuff here

	if (startWord != stopWord) for (size_t i = 0; i  stopBitInd; 
++i) btx(bitArray[stopWord], i);

return value;
}


However, when I don't use the function pointer and instead call 
bts directly (as outlined below, the program compiles and links 
just fine.


bool opIndexAssign(bool value, size_t[2] inds)
{

// other stuff here

	for (size_t i = startBitInd; i  startWordBitDone; ++i) 
bts(bitArray[startWord], i);


// other stuff here

	if (startWord != stopWord) for (size_t i = 0; i  stopBitInd; 
++i) bts(bitArray[stopWord], i);

return value;
}

Any ideas how to fix this behavior? I was trying to use the 
function pointer so I wouldn't need to write essentially the same 
code block replacing bts with btr in if and else blocks


Any help would be appreciated. Thanks


Re: Profile Ouput

2015-07-15 Thread Mike Parker via Digitalmars-d-learn

On Tuesday, 14 July 2015 at 13:35:40 UTC, Mike Parker wrote:



--
 4000   _Dmain
_D4main6selectFZk   40002736471
 4000   _D3std6random27__T7uniformVAyaa2_5b29TkTkZ7uniformFNfkkZk
--




Got it now. The 2736 and 471 are the call tree time and function 
call time, respectively, in *ticks*. They are converted to 
seconds then displayed as milliseconds int the table at the 
bottom of the file.





Re: Weird behavior of this in a subclass, I think?

2015-07-15 Thread Mike Parker via Digitalmars-d-learn

On Thursday, 16 July 2015 at 00:39:29 UTC, H. S. Teoh wrote:


If you want to simulate overriding of class variables, you can 
use a @property method instead:


class Animal {
@property string voice() { return Wah!; }
void speak() { writeln(voice); }
}

class Dog : Animal {
override @property string voice() { return Whoof!; }
}



Alternatively:

class Animal {
 protected string voice;
 void speak() { writeln(voice); }
 }

 class Dog : Animal {
 this() { voice = Whoof!; }
 }




Re: Weird behavior of this in a subclass, I think?

2015-07-15 Thread seashell86 via Digitalmars-d-learn

On Thursday, 16 July 2015 at 00:39:29 UTC, H. S. Teoh wrote:
On Thu, Jul 16, 2015 at 12:18:30AM +, seashell86 via 
Digitalmars-d-learn wrote:

[...]


The reason is that class variables cannot be overridden, only 
class methods can.


If you want to simulate overriding of class variables, you can 
use a @property method instead:


class Animal {
@property string voice() { return Wah!; }
void speak() { writeln(voice); }
}

class Dog : Animal {
override @property string voice() { return Whoof!; }
}


T


Wow, a quick and thorough response! Thank you!  I'm actually 
curious as to why this feature would not be in the language?  Is 
it for performance reasons?


Re: static class vs. static struct

2015-07-15 Thread creiglee via Digitalmars-d-learn
In simple words, Singleton is a pattern and not a keyword. The 
Singleton pattern has several advantages over static classes. A 
singleton allows a class for which there is just one, persistent 
instance across the lifetime of an application. That means, it 
created a single instance and that instance (reference to that 
instance) can be passed as a parameter to other methods, and 
treated as a normal object. While a static class allows only 
static methods and and you cannot pass static class as parameter. 
More about.


http://net-informations.com/faq/netfaq/singlestatic.htm

Lee




Re: Covariant callback functions, or assigning base class members through a subclass reference

2015-07-15 Thread Jacob Carlborg via Digitalmars-d-learn

On 2015-07-14 17:28, Rene Zwanenburg wrote:

Given the following code:

class Base
{
 alias CallbackType = void delegate(Base);

 CallbackType callback;

 void foo()
 {
 callback(this);
 }
}

class Derived : Base
{

}

void main()
{
 auto d = new Derived();
 d.callback = (Derived d) { /* Do something */ }
}

Obviously this won't compile, since the callback function needs to have
Base as parameter, not Derived.


You can cast the delegate. It's probably unsafe but a simple example works.

--
/Jacob Carlborg


Re: Understanding Safety of Function Pointers vs. Addresses of Functions

2015-07-15 Thread Laeeth Isharc via Digitalmars-d-learn

On Tuesday, 14 July 2015 at 17:24:41 UTC, anonymous wrote:
This fails with Error: None of the overloads of 'cos' are 
callable using argument types (int[]).


The problem is that template mixins cannot add to existing 
overload sets. The spec says: If the name of a declaration in 
a mixin is the same as a declaration in the surrounding scope, 
the surrounding declaration overrides the mixin one [1]. That 
means, the `cos` from `alias cos = std.math.cos;` completely 
overrides the one from `mixin t!();`. I guess this is a measure 
against function hijacking again.


I'm not sure if it's supposed to work like it does when the 
alias is removed, two implicitly imported/generated symbols 
forming an overload set. But I can't immediately see a problem 
with it either.


[1] http://dlang.org/template-mixin.html - see Mixin Scope




Now - is there a way to rewrite my code without mixins?


Re: Profile Ouput

2015-07-15 Thread Mike Parker via Digitalmars-d-learn

On Tuesday, 14 July 2015 at 13:35:40 UTC, Mike Parker wrote:


--
 4000   _Dmain
_D4main6selectFZk   40002736471
 4000   _D3std6random27__T7uniformVAyaa2_5b29TkTkZ7uniformFNfkkZk
--



OK, I've finally realized that the top part of trace.log is an 
inverted (with main at the bottom) call tree. I've got everything 
now except that 2736 and 471 on the second line. Anyone?


Re: Template function that accept strings and array of strings

2015-07-15 Thread Vlad Levenfeld via Digitalmars-d-learn

On Wednesday, 15 July 2015 at 21:57:50 UTC, badlink wrote:
Hello, I can't figure how to write a template function that 
accept either strings or array of strings.


This is my current code:

bool hasItemParent(T)(const(char)[] itemId, const(T)[] parentId)
if (is(typeof(T) == char) || (isArray!T  is(typeof(T[]) == 
char)))

{...}

I used const(T)[] because I'd like to accept immutable and 
mutable strings.

But calling it with an immutable string generate this error:

Error: template cache.MetadataCache.hasItemParent cannot deduce 
function from argument types !()(string, string), candidates 
are:
cache.MetadataCache.hasItemParent(T)(const(char)[] itemId, 
const(T)[] parentId) if (is(typeof(T) == char))


Any suggestions ?


T is already a type, so typeof(T) is an error, which makes the 
constraint fail.


Try
hasItemParent(T)(const(char)[] itemId, const(T)[] parentId)
if (is(T == char) || is (T == char[]))

at least I think that's what you meant. typeof(anything[]) will 
never == char.


Re: Template function that accept strings and array of strings

2015-07-15 Thread Yuxuan Shui via Digitalmars-d-learn

On Wednesday, 15 July 2015 at 21:57:50 UTC, badlink wrote:
Hello, I can't figure how to write a template function that 
accept either strings or array of strings.


This is my current code:

bool hasItemParent(T)(const(char)[] itemId, const(T)[] parentId)
if (is(typeof(T) == char) || (isArray!T  is(typeof(T[]) == 
char)))

{...}

I used const(T)[] because I'd like to accept immutable and 
mutable strings.

But calling it with an immutable string generate this error:

Error: template cache.MetadataCache.hasItemParent cannot deduce 
function from argument types !()(string, string), candidates 
are:
cache.MetadataCache.hasItemParent(T)(const(char)[] itemId, 
const(T)[] parentId) if (is(typeof(T) == char))


Any suggestions ?


T is already a type, you don't need to typeof() it.

This should work:

bool hasItemParent(T)(const(char)[] itemId, const(T)[] parentId)
if (is(T == char) || (isArray!T  is(ElementType!T == char)))



Re: Profile Ouput

2015-07-15 Thread jmh530 via Digitalmars-d-learn

On Wednesday, 15 July 2015 at 11:47:53 UTC, Mike Parker wrote:

On Tuesday, 14 July 2015 at 13:35:40 UTC, Mike Parker wrote:


--
 4000   _Dmain
_D4main6selectFZk   40002736471
	 
4000	_D3std6random27__T7uniformVAyaa2_5b29TkTkZ7uniformFNfkkZk

--



OK, I've finally realized that the top part of trace.log is an 
inverted (with main at the bottom) call tree. I've got 
everything now except that 2736 and 471 on the second line. 
Anyone?


I've been confused by this too. The only thing I can find is this
http://www.digitalmars.com/ctg/trace.html

I think it would be cool to write something that takes the output 
and puts it in a prettier format.


Weird behavior of this in a subclass, I think?

2015-07-15 Thread seashell86 via Digitalmars-d-learn
So I've been mostly just toying around with D as it seems like it 
will end up being a strong language for game development both now 
and even moreso in the future.  That being said, I'm perplexed by 
using this code and not receiving the result I would imagine. 
Here is the source code of a basic sandbox.d file:


import std.stdio;

class Animal {
string voice;

void speak() {
writeln(this.voice);
}
}

class Dog : Animal {
string voice = Whoof!;
}

int main() {
auto a = new Animal();
auto d = new Dog();

a.speak(); // Prints 
d.speak(); // Prints  instead of Whoof!

return 0;
}

I know that C++ behaves this way.  However, Dlang impresses me by 
having a very no duh approach to things where this type of 
example seems very no duh.  Anyways, please be gentle as I am 
hardly what most would consider a skilled programmer and, as 
such, was something I wanted to bounce off the pros :)


Re: Weird behavior of this in a subclass, I think?

2015-07-15 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jul 16, 2015 at 12:18:30AM +, seashell86 via Digitalmars-d-learn 
wrote:
 So I've been mostly just toying around with D as it seems like it will
 end up being a strong language for game development both now and even
 moreso in the future.  That being said, I'm perplexed by using this
 code and not receiving the result I would imagine. Here is the source
 code of a basic sandbox.d file:
 
 import std.stdio;
 
 class Animal {
 string voice;
 
 void speak() {
 writeln(this.voice);
 }
 }
 
 class Dog : Animal {
 string voice = Whoof!;
 }
 
 int main() {
 auto a = new Animal();
 auto d = new Dog();
 
 a.speak(); // Prints 
 d.speak(); // Prints  instead of Whoof!
 
 return 0;
 }
 
 I know that C++ behaves this way.  However, Dlang impresses me by
 having a very no duh approach to things where this type of example
 seems very no duh.  Anyways, please be gentle as I am hardly what
 most would consider a skilled programmer and, as such, was something
 I wanted to bounce off the pros :)

The reason is that class variables cannot be overridden, only class
methods can.

If you want to simulate overriding of class variables, you can use a
@property method instead:

class Animal {
@property string voice() { return Wah!; }
void speak() { writeln(voice); }
}

class Dog : Animal {
override @property string voice() { return Whoof!; }
}


T

-- 
Indifference will certainly be the downfall of mankind, but who cares? -- 
Miquel van Smoorenburg


Re: Profile Ouput

2015-07-15 Thread Mike Parker via Digitalmars-d-learn

On Wednesday, 15 July 2015 at 18:02:11 UTC, jmh530 wrote:


I've been confused by this too. The only thing I can find is 
this

http://www.digitalmars.com/ctg/trace.html

I think it would be cool to write something that takes the 
output and puts it in a prettier format.


Yeah, I eventually stumbled on to that, but it unfortunately 
tells me nothing I don't already know. I suppose I'll just email 
Walter and ask him.


Template function that accept strings and array of strings

2015-07-15 Thread badlink via Digitalmars-d-learn
Hello, I can't figure how to write a template function that 
accept either strings or array of strings.


This is my current code:

bool hasItemParent(T)(const(char)[] itemId, const(T)[] parentId)
if (is(typeof(T) == char) || (isArray!T  is(typeof(T[]) == 
char)))

{...}

I used const(T)[] because I'd like to accept immutable and 
mutable strings.

But calling it with an immutable string generate this error:

Error: template cache.MetadataCache.hasItemParent cannot deduce 
function from argument types !()(string, string), candidates are:
cache.MetadataCache.hasItemParent(T)(const(char)[] itemId, 
const(T)[] parentId) if (is(typeof(T) == char))


Any suggestions ?


Re: Understanding Safety of Function Pointers vs. Addresses of Functions

2015-07-15 Thread jmh530 via Digitalmars-d-learn

On Wednesday, 15 July 2015 at 11:45:00 UTC, Laeeth Isharc wrote:

Now - is there a way to rewrite my code without mixins?


Not sure that is possible. It would be interesting if someone 
could figure it out though.


I'm more focused on making the givemeabettername a bit more 
general. Someone above had sort of asked why bother for the 
simple case. True enough, but if I can write something generic 
enough to work on a wide variety of function types, then I would 
consider it a win. E.g., below.


template givemeabettername(alias fun)
{
static if (arity!fun == 1)
{
T givemeabettername(T)(T x)
if (isDynamicArray!(T))
{
return x.map!fun.array;
}

T givemeabettername(T)(T x)
if (isStaticArray!(T))
{
T result = x.dup;
foreach(ref elem; result)
{
elem = fun(elem);
}
return result;
}
}
}


Casting random type to random struct - is this a bug?

2015-07-15 Thread rumbu via Digitalmars-d-learn

struct S { int a, b; }
auto s = cast(S)10;
//compiles and sets s.a to 10.

It works also for any other type, if the structure contains a 
member of that type in the first position.


Is this normal behaviour?


Re: Casting random type to random struct - is this a bug?

2015-07-15 Thread Daniel Kozák via Digitalmars-d-learn

On Wed, 15 Jul 2015 11:57:01 -0400
Steven Schveighoffer schvei...@yahoo.com wrote:

 On 7/15/15 11:45 AM, rumbu wrote:
  struct S { int a, b; }
  auto s = cast(S)10;
  //compiles and sets s.a to 10.
 
  It works also for any other type, if the structure contains a
  member of that type in the first position.
 
  Is this normal behaviour?
 
 I would say this is a bug. As far as I know, it's not defined in the
 spec.
 
 -Steve
It is defined:

Casting a value v to a struct S, when value is not a struct of the same
type, is equivalent to:

S(v)


Re: Casting random type to random struct - is this a bug?

2015-07-15 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/15/15 11:45 AM, rumbu wrote:

struct S { int a, b; }
auto s = cast(S)10;
//compiles and sets s.a to 10.

It works also for any other type, if the structure contains a member of
that type in the first position.

Is this normal behaviour?


I would say this is a bug. As far as I know, it's not defined in the spec.

-Steve


Re: Casting random type to random struct - is this a bug?

2015-07-15 Thread Daniel Kozák via Digitalmars-d-learn

On Wed, 15 Jul 2015 15:45:43 +
rumbu ru...@rumbu.ro wrote:

 struct S { int a, b; }
 auto s = cast(S)10;
 //compiles and sets s.a to 10.
 
 It works also for any other type, if the structure contains a 
 member of that type in the first position.
 
 Is this normal behaviour?

Yes, this is OK

If you need to cast against diferent types you can try pointers:

import std.stdio;
struct S
{
ubyte a;
ubyte b;
}

void main() {
ushort m = 65535;
auto s = *(cast(S*)m);
writeln(s);
}


Re: Covariant callback functions, or assigning base class members through a subclass reference

2015-07-15 Thread Ali Çehreli via Digitalmars-d-learn

On 07/14/2015 08:28 AM, Rene Zwanenburg wrote:

 But the CallbackType should be able to prevent such unsafe assignments.

The following struct applies what others have recommended only if an 
actual derived type is provided. However, it is still unsafe as the 
direct assignment to 'callback' cannot know that the object is the same 
as template parameter D.


struct CallbackBased(B)
{
alias Func = void delegate(B);
Func func;

void opAssign(D)(void delegate(D) arg)
if (is (D : B))
{
func = cast(Func)(arg);
}

void opCall(B obj)
{
func(obj);
}
}

class Base
{
alias CallbackType = CallbackBased!Base;

CallbackType callback;

void foo()
{
callback(this);
}
}

class Derived : Base
{
void derivedFunc()
{
import std.stdio;
writeln(Derived object in action...);
}
}

void main()
{
auto d = new Derived();
d.callback = (Derived d) { d.derivedFunc(); };
d.foo();
}

Ali



question about the semantics of unshared variables

2015-07-15 Thread aki via Digitalmars-d-learn

I want to make sure about the semantics of unshared variables.

import std.concurrency;
import core.thread;
ubyte[1024 * 1024] buf1MB;
void fun() { Thread.sleep(5000.msecs); }
void testThread() {
foreach(i; 0..2000) {
spawn(fun);
}
}

Are instances of buf1MB created for every threads?
Even if it is never accessed by the threads?
If some static variable is defined in other module
or in some library, all of them also instantiated
for every threads?
BTW, calling testThread() above causes following
run time error. Is this an expected result?

core.thread.ThreadError@src\core\thread.d(2903): Error creating 
thread


0x0047B1A7
0x0042ABD4
0x0042AB3B
0x0042AB27
0x0042ADF9
0x00474C22
0x00474BF7
0x00474B0F
0x0042AE13
0x757A7C04 in BaseThreadInitThunk
0x7735AD1F in RtlInitializeExceptionChain
0x7735ACEA in RtlInitializeExceptionChain