Re: returning struct, destructor

2016-12-21 Thread John C via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner 
wrote:
This prints 3 times "Destruct" with dmd 0.072.1. If I remove 
the if block, it prints "Destruct" only 2 times - the behavior 
I'm expecting. Why?


Possibly to do with named return value optimisation.


Re: Proper generic way to get the hash of something?

2016-12-04 Thread John C via Digitalmars-d-learn

On Sunday, 4 December 2016 at 06:51:24 UTC, Tofu Ninja wrote:

So what is the proper way to get the hash of something now?


This question came up a few days ago: 
http://forum.dlang.org/post/o1igoc$21ma$1...@digitalmars.com


Returning structs from COM

2016-12-03 Thread John C via Digitalmars-d-learn
Some DirectX methods return structs by value, but when I try 
calling them I either get garbage or an access violation.


Usually COM methods return structs by pointer as a parameter, but 
these are returning the struct as the actual return value, as in 
this definition:


  extern(Windows):
  struct D2D1_SIZE_F { float width, height; }

  interface ID2D1Bitmap : ID2D1Image {
D2D1_SIZE_F GetSize();
  }

If I rewrite GetSize to return by pointer as a parameter, it 
appears to work and I get the correct width and height without an 
AV being thrown. And I can add a helper method that returns by 
value:


  interface ID2D1Bitmap : ID2D1Image {
void GetSize(D2D1_SIZE_F* size);

final D2D1_SIZE_F GetSize() {
  D2D1_SIZE_F size;
  GetSize();
  return size;
}
  }

But does anyone know why the original definition works in C++ but 
not D? Is it a bug? (I'm compiling with -m64.)


Re: problem with isnan

2016-11-11 Thread John C via Digitalmars-d-learn

On Friday, 11 November 2016 at 20:55:52 UTC, Charles Hixson wrote:

Thank you.  Unfortunately:
importstd.math;
...
assert(isNan (c.curActivation), "cell has unexpected 
curActivation: %s".format(c.curActivation));


yields:
cell.d(292): Error: undefined identifier 'isNan', did you mean 
overloadset 'isnan'?


It should be isNaN.


Re: Error 42 When Trying to Interop with a C# Libary

2016-11-05 Thread John C via Digitalmars-d-learn

On Saturday, 5 November 2016 at 21:02:14 UTC, Adam D. Ruppe wrote:

coff2omf works with .obj files


Well, that's not what the docs say! 
http://www.digitalmars.com/ctg/coff2omf.html


  "The input files can be either object files (.obj) or library 
files (.lib)"


Apparently you have to convert them to an older COFF format first 
using Microsoft's linker. Not sure if I ever tried that though.


Re: Error 42 When Trying to Interop with a C# Libary

2016-11-05 Thread John C via Digitalmars-d-learn

On Saturday, 5 November 2016 at 16:13:18 UTC, Sarcross wrote:
If you're wondering, I did use the COFF2OMF tool on the library 
I got from the C# code, which is why in WordFileParser.d you'll 
see "pragma(lib, "Parser2")".


I have never had success trying to link with a lib file converted 
by COFF2OMF.


Are you able to use -m32mscoff? I was able to call a .NET DLL 
using the technique in the tutorial when I tried it a few months 
ago, using that DMD option instead.


Re: How to get sqlite3.lib x64?

2016-10-24 Thread John C via Digitalmars-d-learn

On Monday, 24 October 2016 at 05:43:00 UTC, Andre Pany wrote:

Hi,

I try to get sqlite3.lib for 64 Bit windows os.

I tried implib with the def file and the 64 Bit dll:
implib sqlite3_implib.lib sqlite3.def /system
-> App crash (Windows 10)

With the dll file defined:
implib sqlite3_implib.lib sqlite3.dll /system
-> Error message: Error(10): Error: cannot read DLL input file

I have the MS Build tools installed, but the example from
the SQLite site only shows, how to build a X86 dll file from
the C source code but not how to build the X86_64 lib file
for windows:
cl sqlite3.c -link -dll -out:sqlite3.dll

I tried different combinations but without success.
Could you give me some hints?

Kind regards
André


Do you have the Windows 10.0.10586 SDK installed? It includes a 
.lib file to link against the 64-bit DLL. It's called 
winsqlite3.lib.


Re: How to correctly display accented characters at the Windows prompt?

2016-10-10 Thread John C via Digitalmars-d-learn
On Monday, 10 October 2016 at 14:14:08 UTC, Cleverson Casarin 
Uliana wrote:
Hello, when I run a compiled Windows executable at the console, 
letters outside the ascii range such as ç and ã do not display 
propperly. Is there any d function to change the console code 
page on the fly? My Windows console code page, which is for 
Brazilian Portuguese, is at 850. Perhaps an alternative would 
be to convert d strings from Unicode to the 850 code page on 
the fly.


Thanks,
Cleverson


Call SetConsoleOutputCP(CP_UTF8).


Re: Basic sounds' playing

2016-10-08 Thread John C via Digitalmars-d-learn
On Saturday, 8 October 2016 at 13:35:57 UTC, Cleverson Casarin 
Uliana wrote:
Thank you Vadim and John, the PlaySound function is enough for 
now. I'm not yet developing an app, just experimenting with 
some exercises.


The wasapi library seems interesting, does it also implement a 
playSound-like function?


Greetings
Cleverson


WASAPI looks more complicated - see this sample for playing a 
file 
https://msdn.microsoft.com/en-us/library/windows/desktop/dd316756(v=vs.85).aspx


Re: Basic sounds' playing

2016-10-08 Thread John C via Digitalmars-d-learn
On Saturday, 8 October 2016 at 01:00:20 UTC, Cleverson Casarin 
Uliana wrote:
Hello all, starting to learn d, apreciating it so far. I'd like 
to play/stop wave sound files assynchronously on Windows. Can I 
get a module for that by installing a particular compiler, or 
is there any package for it instead?


Thank you,
Cleverson


For basic playback of wave files, you could use the native 
PlaySound function, imported in core.sys.windows.mmsystem. 
https://msdn.microsoft.com/en-us/library/windows/desktop/dd743680(v=vs.85).aspx


Re: Convert type tuple to array?

2016-10-05 Thread John C via Digitalmars-d-learn

On Wednesday, 5 October 2016 at 11:46:14 UTC, Adam D. Ruppe wrote:

On Wednesday, 5 October 2016 at 11:43:12 UTC, John C wrote:
Is there a way to convert something like AliasSeq!(int, int, 
int) to an int[] - the opposite of aliasSeqOf?


If it is a legal array (matching types), just put [] around it.


int[] arr = [ AliasSeq!(1,2,3) ];


An AliasSeq is considered by the language to be basically the 
same as a comma-separated list... you can almost "copy/paste" 
it into any context where those are legal, including function 
calls and array literals.


Great to know!


Convert type tuple to array?

2016-10-05 Thread John C via Digitalmars-d-learn
Is there a way to convert something like AliasSeq!(int, int, int) 
to an int[] - the opposite of aliasSeqOf?


Re: URI parsing

2016-10-05 Thread John C via Digitalmars-d
On Wednesday, 5 October 2016 at 10:28:44 UTC, rikki cattermole 
wrote:
I developed[0] with the hopes that at some point in the future 
I could bring that over to Phobos. But before that I'm waiting 
for allocators to be moved out of experimental first.


[0] 
https://github.com/rikkimax/alphaPhobos/blob/master/source/std/experimental/uri.d


Looks good, thanks.


URI parsing

2016-10-05 Thread John C via Digitalmars-d
What packages do people use when they want to parse URIs? I 
rolled my own but it's incomplete and as it's a fairly common 
need there must be one out there? In fact, I'm surprised there 
isn't one in Phobos yet.


Re: Member not accessible in delegate body

2016-09-23 Thread John C via Digitalmars-d-learn

On Friday, 23 September 2016 at 18:20:24 UTC, Martin Nowak wrote:
Please file a bug report issues.dlang.org, shouldn't be 
difficult to fix.


Done: https://issues.dlang.org/show_bug.cgi?id=16531


Member not accessible in delegate body

2016-09-23 Thread John C via Digitalmars-d-learn
If I try to call the protected method of a superclass from inside 
the body of a delegate, the compiler won't allow it.


void layoutTransaction(Control c, void delegate() action) {
  // do stuff
  action();
  // do more stuff
}

class Control {
  protected void onTextChanged() {}
}

class Label : Control {
  protected override void onTextChanged() {
layoutTransaction(this, {
  super.onTextChanged(); // <--- Error here
  changeSize();
});
  }
  private void changeSize() {}
}

Output: class Control member onTextChanged is not accessible.

How is it possible that "onTextChanged" isn't accessible but the 
private method "changeSize" *is*?


Re: Enum name convention consistency

2009-08-19 Thread John C

Sam Hu wrote:

From std.range:
enum StoppingPolicy
{
/// Stop when the shortest range is exhausted
shortest,
/// Stop when the longest range is exhausted
longest,
/// Require that all ranges are equal
requireSameLength,
}

From std.thread:( phobos webpage) 
enum State; 
A fiber may occupy one of three states: HOLD, EXEC, and TERM. The HOLD state applies to any fiber that is suspended and ready to be called. The EXEC state will be set for any fiber that is currently executing. And the TERM state is set when a fiber terminates. Once a fiber terminates, it must be reset before it may be called again. 


HOLD

EXEC

TERM

From std.stream:
enum FileMode {
  In = 1,
  Out = 2,
  OutNew = 6, // includes FileMode.Out
  Append = 10 // includes FileMode.Out
}

Would not this be better if we keep the name convention consistent.
Regards,
Sam


Note that std.socket also uses the SHOUTY style. But I think Phobos 
should stick to the std.stream style. The std.range style seems to be 
one of Andrei's peccadilloes.


Re: win32 capCreateCaptureWindow problem

2009-08-14 Thread John C

Sam Hu wrote:

John C Wrote:


Sam Hu Wrote:


John C Wrote:


Ivan Boritsky wrote:

i work on a win32 application. i try to access my web camera.
when i use this api funtion;
capCreateCaptureWindow(cam, WS_VISIBLE + WS_CHILD, 10, 10,266, 252, hWnd, 0);

That should be: WS_VISIBLE | WS_CHILD.


i get this error:
Error: undefined identifier capCreateCaptureWindow
You need a header module that imports the function. Try the vfw.d module 
at the project Jarrett pointed you to.



my compile comman is:
dmd webcam.d gdi32.lib advapi32.lib
You'll also need vfw32.lib, which isn't part of the DMD distribution, 
but will be on your system if you've downloaded the Platform SDK - in 
which case, you'll need to run it through coffimplib 
(ftp://ftp.digitalmars.com/coffimplib.zip).


Hi John,

I also downloaded the said win32 zip but don't know how to compile to lib or 
dll,doesn't need to compile? or how to compile?

Thanks.
Regards,
Sam
Huh? You just run 


  coffimplib -f vfw32.lib

and specify the .lib on your build command line - something like:

  dmd webcam vfw32.lib


Hi John,

Thank you.I have win2003 SDK+vc++2008 EX installed in my machine.When I tried 
the winsamp.d in the example folder of dmd(ver2031):

/coffimplib -f vfw32.lib
/dmd winsamp.d gdi32.lib vfw32.lib winsamp.def
** also tried bud winsamp.d gdi32.lib vfw32.lib 


I got below error:

F:\laguangeD\WindowsApi-CMakeD\demobud winsampw.d -cleanup gid32.lib vfw32.lib 
OPTLINK (R) for Win32  Release 8.00.1

Copyright (C) Digital Mars 1989-2004  All rights reserved.
mpr.lib
 Warning 2: File Not Found mpr.lib
imm32.lib
 Warning 2: File Not Found imm32.lib
lz32.lib
 Warning 2: File Not Found lz32.lib
rpcns4.lib
 Warning 2: File Not Found rpcns4.lib

What's that?Was I missing something?

Regards,
Sam


Those are warnings, not errors. Ignore them, it will compile anyway.


Re: win32 capCreateCaptureWindow problem

2009-08-12 Thread John C

Ivan Boritsky wrote:

i work on a win32 application. i try to access my web camera.
when i use this api funtion;
capCreateCaptureWindow(cam, WS_VISIBLE + WS_CHILD, 10, 10,266, 252, hWnd, 0);


That should be: WS_VISIBLE | WS_CHILD.



i get this error:
Error: undefined identifier capCreateCaptureWindow


You need a header module that imports the function. Try the vfw.d module 
at the project Jarrett pointed you to.




my compile comman is:
dmd webcam.d gdi32.lib advapi32.lib


You'll also need vfw32.lib, which isn't part of the DMD distribution, 
but will be on your system if you've downloaded the Platform SDK - in 
which case, you'll need to run it through coffimplib 
(ftp://ftp.digitalmars.com/coffimplib.zip).




what am i doing wrong? how can i acces my web cam?


Re: property syntax strawman

2009-08-04 Thread John C

Sjoerd van Leent wrote:

Andrei Alexandrescu Wrote:


Nick Sabalausky wrote:
Walter Bright newshou...@digitalmars.com wrote in message 
news:h53g3i$el...@digitalmars.com...

  bool empty { ... }
  void empty=(bool b) { ... }

What do you think?
I think that if D starts to make a habit of aping the ugly C++ approach to 
adding new features (as this does), then we may as well just use C++. 
Let's not forget that C++ got very conservative about adding keywords 
after a keyword spree (class that is essentially same as struct, 
namespace, xyz_cast, oh yes typename, and, or, not plus other useful 
names that I probably forgot). We don't want to get there.



Andrei


I understand your point of view, but I am afraid that using an awkward syntax 
makes things difficult to understand. However, I could certainly live with the 
current proposal. But this has to do that I'm not afraid of symbolic ways to 
achieve something. Others might find it more difficult.

I think that a setter and a getter are two different things, but I would avoid 
to have an inconsistent property, such as setting an int and getting a bool, or 
something similar. Perhaps a compiler check could intercept?

But I think I can do it with the keywords that we already have in our 
possession:

bool in empty
{
}

bool out empty
{
}

Although perhaps this could be argued against, as in and out are used for 
contract programming. But this clearly is a different use.


in and out are also used for function parameters, remember.


Re: DIP6: Attributes

2009-08-04 Thread John C

Steven Schveighoffer wrote:


For instance you could do this exact thing in C#, even though C# doesn't 
support named parameters.


But they're coming: 
http://msdn.microsoft.com/en-us/library/dd264739(VS.100).aspx


Re: property syntax strawman

2009-08-03 Thread John C

Steven Schveighoffer wrote:

On Sun, 02 Aug 2009 15:23:44 -0400, John C johnch_a...@hotmail.com wrote:


Andrei Alexandrescu wrote:

Michiel Helvensteijn wrote:

Andrei Alexandrescu wrote:


Then in a later message you mention:

bool empty.get() { ... }
void empty.set(bool b) { ... }

which I like and which does not seem to have difficulties; the names
get and set will be never used as such.


Yes, those are two notations for the same thing, and they have the same
problem. Let me demonstrate:

--
struct S {
int get() { return 42; }
};

struct T {
S _s;
S property.get() { return _s; }
void property.set(S s) { _s = s; }
}

T t;

auto X = t.property.get();
--

What is the type of X? It can be either S or int, depending on how D 
handles

the situation.

The ambiguity is in the possibility to directly reference the getter 
and

setter methods. In that other subthread (the older one) I listed some
possible solutions.

The first is to make such a program an error.

The second is not to allow a direct reference to a getter/setter (so 
X is an

int).

The third is to let the getter/setter overshadow S members (so X is 
an S).
 I see. My view is that .get and .set are simply symbolic 
placeholders, but indeed some may get confused.

 Andrei


Does the ambiguity go away if we replace the '.' with a space?

Declaration:

bool empty get();
void empty set(bool);

Declaration:

bool empty get() { return empty_; }
void empty set(bool b) { empty_ = b; }


The ambiguity of declaration, not the ambiguity of usage, unless you 
want to use the space to denote the usage also?


-Steve


Not sure where the ambiguity is with usage. To keep things simple, 'get' 
and 'set' should only be valid at the point of declaration. They're not 
really part of the function name.


Re: property syntax strawman

2009-08-03 Thread John C

Steven Schveighoffer wrote:
On Mon, 03 Aug 2009 10:25:02 -0400, Michiel Helvensteijn 
m.helvensteijn.rem...@gmail.com wrote:



Steven Schveighoffer wrote:


So the poster who started this trail of the thread is assuming that

t.property.get()

identifies the property getter directly.  But what if the return type of
t.property.get() contains a method get()?  Since t.property is an 
alis for

t.property.get(), Should t.property.get() map to:

...

That is the ambiguity.


I myself see great value in the ability to access the getter and setter
functions directly. But because of the ambiguity you described, this is
problematic for D.

Andrei sees 'get' and 'set' as nothing more than declaration-side
indications of the getter and setter. Not real functions. In that 
case, the

ambiguity doesn't exist.

To alleviate possible confusion, it has been suggested that a space be 
used,
not a dot, between the name of the property and get/set in the 
declaration.




So your answer is, there is no ambiguity because it's not possible to 
access the getter/setter directly?  That poses a problem for existing 
code which uses delegates to such functions.  I'm not sure we want to 
lose that ability.


-Steve



Back to the drawing board.


Re: property syntax strawman

2009-08-02 Thread John C

Walter Bright wrote:
Having optional parentheses does lead to unresolvable ambiguities. How 
much of a problem that really is is debatable, but let's assume it 
should be resolved. To resolve it, a property must be distinguishable 
from a regular function.


One way is to simply add a property attribute keyword:

  property bool empty() { ... }
  property void empty(bool b) { ... }

The problem is that:

1. there are a lot of keywords already
2. keywords are global things


Well, property probably isn't too common a variable name, so it 
shouldn't impact existing code.




The alternative is to have a unique syntax for properties. Ideally, the 
syntax should be intuitive and mimic its use. After much fiddling, and 
based on n.g. suggestions, Andrei and I penciled in:


  bool empty { ... }
  void empty=(bool b) { ... }

The only problem is when a declaration but not definition is desired:

  bool empty;

but oops! That defines a field. So we came up with essentially a hack:

  bool empty{}

i.e. the {} means the getter is declared, but defined elsewhere.


Would unimplemented setters look like this:

void empty = (bool b) {}

?



What do you think?


Looks a bit odd. Perhaps it's just the unfamiliarity of the syntax. Most 
of us got used to templates, eventually.


My own syntax suggestion requires no new keywords (contextual or otherwise).

interface INameable {

  string name {
();
(value);
  }

}

class Person : INameable {

  private string name_;

  string name {
() { return name_; }
(value) { name_ = value; }
  }

}

In the above example, value could be anything the class designer wants 
- to the compiler, empty parentheses () represent the getter, (blah) the 
setter.


Re: property syntax strawman

2009-08-02 Thread John C

Marianne Gagnon wrote:
The alternative is to have a unique syntax for properties. Ideally, the 
syntax should be intuitive and mimic its use. After much fiddling, and 
based on n.g. suggestions, Andrei and I penciled in:


   bool empty { ... }
   void empty=(bool b) { ... }

The only problem is when a declaration but not definition is desired:

   bool empty;

but oops! That defines a field. So we came up with essentially a hack:

   bool empty{}

i.e. the {} means the getter is declared, but defined elsewhere.

What do you think?


I liked the original idea... but this declaration syntax is a total can of 
worms. Like others pointed out, would the compiler automatically turn all 
functions with empty bodies into declarations? Would empty setters be 
considered a declaration? What if I actually *want*
 to use an empty body as definition? Or what if I accidentally leave a body 
empty when I didn't want to?

Like someone else pointed out, existing keywords could be reused :

bool empty
{
in(bool value)
{
_my_empty = value;
}
out
{
return _my_empty;
}
}

I like this quite a bit.  I never wrote any compiler, granted, but I don't 
think it'd be that hard to implement - and doesn't introduce any new keyword.


I'd be more than happy with that.



Or, alternatively, if you really wish to keep them separate, bool empty= { ... 
} isn't intuitive, as Andrei pointed out, but is already less error-prone than 
the empty-body declaration idea I believe


Re: property syntax strawman

2009-08-02 Thread John C

Andrei Alexandrescu wrote:

Michiel Helvensteijn wrote:

Andrei Alexandrescu wrote:


Then in a later message you mention:

bool empty.get() { ... }
void empty.set(bool b) { ... }

which I like and which does not seem to have difficulties; the names
get and set will be never used as such.


Yes, those are two notations for the same thing, and they have the same
problem. Let me demonstrate:

--
struct S {
int get() { return 42; }
};

struct T {
S _s;
S property.get() { return _s; }
void property.set(S s) { _s = s; }
}

T t;

auto X = t.property.get();
--

What is the type of X? It can be either S or int, depending on how D 
handles

the situation.

The ambiguity is in the possibility to directly reference the getter and
setter methods. In that other subthread (the older one) I listed some
possible solutions.

The first is to make such a program an error.

The second is not to allow a direct reference to a getter/setter (so X 
is an

int).

The third is to let the getter/setter overshadow S members (so X is an 
S).


I see. My view is that .get and .set are simply symbolic placeholders, 
but indeed some may get confused.


Andrei


Does the ambiguity go away if we replace the '.' with a space?

Declaration:

bool empty get();
void empty set(bool);

Declaration:

bool empty get() { return empty_; }
void empty set(bool b) { empty_ = b; }


Re: Properties: problems

2009-07-30 Thread John C

Chad J wrote:

John C wrote:

Here's a couple of annoying problems I encounter quite often with D's
properties. Would having some form of property syntax fix them?

1) Array extensions:

  class Person {

string name_;

string name() {
  return name_;
}

  }

  auto person = getPerson();
  auto firstAndLast = person.name.split(' ');

The above line currently requires parentheses after 'name' to compile.



This one is weird.  After defining getPerson() I was able to rewrite the
last line into this and make it compile:

auto firstAndLast = split(person.name, );


Yes, that's D's special array syntax, where free functions can be called 
as if they were methods of an array.




Note that you need qoutes, not just ' '.


My mistake.


But even

auto firstAndLast = person.name.split( );

does not compile.
main2.d(36): Error: function expected before (), not
split(person.name()) of type immutable(char)[][]

This is probably a compiler bug.


This is my point. The compiler can't tell that name is a property, so 
it expects parentheses name() to work. That's the problem.




I don't think property syntax is truly necessary for this example.  You
are fortunate enough to be using strings, which are passed by reference.


Sorry, I don't see how this statement is relevant at all.


Re: Properties: problems

2009-07-30 Thread John C

Chad J wrote:

John C wrote:


2) Indexing:

  struct Map(K, V) {

void opIndexAssign(V value, K key) { ... }
V opIndex(K key) { ... }

  }

  class WebClient {

private Map!(string, string) headers_;

Map!(string, string) headers() {
  return headers_;
}

  }

  auto client = new WebClient();
  client.headers[User-Agent] = MyWebClient;

The compiler says client.headers() is not an lvalue (adding 'ref' in D2
changes nothing).


This is nearly the same thing as the a.b.c = 3; example given in the
a.b.c = 3; thread.  The .b is your .headers.  It's slightly more
forgiving though, since you are calling a function on the returned
struct and not accessing a field.  The setter never needs to be called
in your example.

I'll use the compiler's rewritting technique to show you what it looks like:

client.headers[User-Agent] = MyWebClient;
client.headers.opIndexAssign(User-Agent,MyWebClient);
client.headers().opIndexAssign(User-Agent,MyWebClient);

client.headers() creates a /new/ Map!(...) struct, so the opIndexAssign
will not be called on the one you want it to be called on.

Adding 'ref' should change that.  That sounds like a bug.

In this specific example, property syntax is not truly necessary.  That
ref returns were added should make this doable.


It actually works if the ref is attached not to the WebClient.headers 
property, but to the Map.opIndex operator (and opIndexAssign is removed, 
or course).


Re: poll for properties

2009-07-29 Thread John C

Steven Schveighoffer wrote:

Please respond to this poll before reading other responses.

Read the following function:

void foo(Box b)
{
  if(b.xxx)
b.fill(5);
  else
b.yyy;
}

Assuming you have no idea what type Box is, what of the following 
options looks most natural for xxx in order to test to see if b has an 
element in it?


a) if(b.empty)
b) if(b.empty())
c) if(b.clear)
d) if(b.clear())

Answer:
=


a).

But b.isEmpty would be a better design (yes, ranges, that's you).


=

What would you guess looks most natural for yyy in order to remove all 
elements from b?


a) b.empty;
b) b.empty();
c) b.clear;
d) b.clear();

Answer:
=


d).


=

Which of the following functions looks incorrect?

void a(Box box)
{
   if(box.empty())
 box.fill(5);
   else
 box.clear();
}

void b(Box box)
{
   if(box.empty)
 box.fill(5);
   else
 box.clear;
}

void c(Box box)
{
if(box.clear)
   box.fill(5);
else
   box.empty;
}

void d(Box box)
{
if(box.clear())
   box.fill(5);
else
   box.empty();
}


Answer:
==


All look wrong.


==

You read the documentation for Box, and it looks like this:

/**
 * check to see if a box is clear
 */
 bool clear();

/**
 * empty a box, returning true if the box had contents before emptying
 */
 bool empty();

Now knowing what the actual meaning of clear and empty are, indicate 
which version(s) of the function in the previous question would surprise 
you if it compiled.


Here are the functions again for reference:

void a(Box box)
{
   if(box.empty())
 box.fill(5);
   else
 box.clear();
}

void b(Box box)
{
   if(box.empty)
 box.fill(5);
   else
 box.clear;
}

void c(Box box)
{
if(box.clear)
   box.fill(5);
else
   box.empty;
}

void d(Box box)
{
if(box.clear())
   box.fill(5);
else
   box.empty();
}

Answer:
==


If we're assuming parameterless functions are not allowed, all of them.


==

Do you think the meaning of a symbol with parentheses suggests something 
different than that same symbol without parentheses for the following 
symbols:


a) select
b) rock
c) keyboard
d) elevate

Answer:
==


Yes.


==

Thank you for taking the poll.  I tried to be as objective as possible, 
if you don't think I was, please indicate why:


Properties: problems

2009-07-29 Thread John C
Here's a couple of annoying problems I encounter quite often with D's 
properties. Would having some form of property syntax fix them?


1) Array extensions:

  class Person {

string name_;

string name() {
  return name_;
}

  }

  auto person = getPerson();
  auto firstAndLast = person.name.split(' ');

The above line currently requires parentheses after 'name' to compile.

2) Indexing:

  struct Map(K, V) {

void opIndexAssign(V value, K key) { ... }
V opIndex(K key) { ... }

  }

  class WebClient {

private Map!(string, string) headers_;

Map!(string, string) headers() {
  return headers_;
}

  }

  auto client = new WebClient();
  client.headers[User-Agent] = MyWebClient;

The compiler says client.headers() is not an lvalue (adding 'ref' in D2 
changes nothing).


Re: properties

2009-07-28 Thread John C

Andrei Alexandrescu wrote:

Again, most complaints have been directed towards writeln = 5. I think 
that's the major problem to be resolved.




Here's another, one that's pretty common:

class Person {

  private string name_;

  string name() {
return name_;
  }

}

string[] split(string s, char separator) {
  ...
}

auto p = getPerson();
auto names = p.name.split(' '); // Doesn't compile - you need to do 
p.name().split(' ');


Re: Reddit: why aren't people using D?

2009-07-27 Thread John C
Steven Schveighoffer Wrote:

 On Mon, 27 Jul 2009 12:16:59 -0400, Andrei Alexandrescu  
 seewebsiteforem...@erdani.org wrote:
 
  Steven Schveighoffer wrote:
  On Mon, 27 Jul 2009 10:54:00 -0400, Andrei Alexandrescu  
  seewebsiteforem...@erdani.org wrote:
 
 
  That's why properties are not
  functions.
 
  This post had a negative effect on me: I now think properties are  
  functions even more than before.
   Let me bring you back to the issue at hand:
   writefln = hi;
   :)
 
  That wart must be eliminated. But the shortcomings of the current design  
  are not a proof that we need a specialized facility for what is at the  
  end of the day nothing but a notational convenience.
 
 I personally am OK with any solution that allows me to specify that a  
 function is actually a property, thereby restricting the compiler from  
 allowing assignment calls with normal functions.
 
 The most straightforward solution I can think of is marking a function  
 with a keyword, i.e.:
 
 property int x() {return _x;}
 property void x(int n) {_x = n;}

Agreed. And enabling the function pair to be enclosed in a block would reduce 
repitition a tad as well as indicate they are one entity.

property {
  int x() { return _x; }
  void x(int n) { _x = n; }
}

 
 The other syntax suggestions provide different added bonuses, but I think  
 all of those bonuses are not as critical as code working as expected.
 
 For example:
 
 - implied hidden storage
 - documenting property getters/setters as one entity
 - being able to execute a getter during a debug session.
 
 -Steve



Re: Reddit: why aren't people using D?

2009-07-27 Thread John C

Andrei Alexandrescu wrote:

Nick Sabalausky wrote:
Andrei Alexandrescu seewebsiteforem...@erdani.org wrote in message 
news:h4kkn3$14p...@digitalmars.com...
But what I want is to come with a new design that adds minimum 
aggravation on the learning programmer. If they know how to define a 
method, they must know how to define a property. None of that 
property blah { get ... set ... } crap is necessary.




I can't be nice about this: Any programmer who has *any* aggrivation 
learning any even remotely sane property syntax is an idiot, period. 
They'd have to be incompetent to not be able to look at an example 
like this:


// Fine, I'll throw DRY away:
int _width;
int width
{
get { return _width; }
set(v) { _width = v; }
}

And immediately know exactly how the poroperty syntax works.


Sure. My point is that with using standard method definition syntax 
there's no need for even looking over an example.




Which is why Steven Schveighoffer's is suggestion is the most pragmatic 
so far. You just add a property attribute to a regular function 
definition. (And it doesn't look ugly, unlike the opGet_/opSet_ idea.)


Re: DIP4: Properties

2009-07-25 Thread John C
Nick Sabalausky Wrote:

  An alternate usage/definition syntax for properties.
 
 http://prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP4
 
 Note that there are a few parts marked NEED HELP, that could use assistance 
 from someone with more expertise in this than me.
 
 

I'm not sure about combining a property with its backing store - unless the 
shorthand version is being used, in which case it seems OK.

And if the concern is about magic values and new keywords, this version needs 
neither:

class Book {

  // explicitly declared backing store for author property.
  private string author_;

  string author {
string() { return author_; }
void(string value) { author_ = value; }
  }

  // Shorthand property, backing store is generated by compiler.
  string title {
string();
void(string);
  }

}

Slightly more verbose, but it's not hideous and doesn't feel alien.

John.


Re: Creating ActiveX in D

2009-07-24 Thread John C
BLS Wrote:

 John C wrote:
  BLS Wrote:
  
  Hi,
  Sergey G. ask (by accident ;) ) about : How to create an ActiveX control 
  in D.
  Now, I am curious : Is it possible to create an ActiveX Control In D ?
 
  Something along :
  class Component : IDispatch
  {
  }
 
  instead of :
 
  class Component : IUnknown
  {
  }
 
  and if so, how to build an AX control in D, guess as DLL + D runtime ???
 
  Thank's for enlighten me.
  björn
  
  I wouldn't, certainly not merely out of curiosity. It's a mountain of work, 
  and you have to implement lots of pretty obscure interfaces. If you really 
  must do it, find a C++ example and translate it to D.
 
 
 Thanks John,
 
 If YOU don't give it a chance... then everything is lost.
 
 another leak in D
 +

Well, I wouldn't go that far. It's just that some other languages have either 
built-in language support for ActiveX (VB, C#, Delphi) or libraries that do 
much of the legwork for you (eg, MFC/ATL/VCL). But I don't think these belong 
in the D language or its standard library.

It's relatively straightforward to create a COM server in a DLL and register a 
few classes with the system. Juno's (http://www.dsource.org/projects/juno) COM 
package shows how it can be done.

If you want to do something useful like show a window have allow the user to 
interact with it, you need to implement IOleObject, IOleControl, 
IOleInPlaceActiveObject, and so on 
(http://msdn.microsoft.com/en-us/library/aa751972(VS.85).aspx). And maybe 
IDispatch. And you need a UI library. So if you're feeling bold...

John.


Nested Foreach

2009-07-17 Thread John C
Is nesting foreach statements supposed to work? I hit a problem today that I've 
never encountered before, even though I'm sure I've nested foreach before. 
Here's an example that illustrates the problem:


class Collection {

  int opApply(int delegate(ref Object) a) {
return 0;
  }

}

Object testForeach(Collection level1, Collection level2) {
  foreach (first; level1) {
foreach (second; level2) {
  return second;
}
  }
  return null;
}

void main() {
  testForeach(new Collection, new Collection);
}

Error: cannot implicitly convert expression (second) of type object.Object to 
int

If someone else can repro this, I'll enter it into Bugzilla.

John.


Re: Nested Foreach

2009-07-17 Thread John C
Nick Sabalausky Wrote:

 I came across a problem much like that a while back. Does this describe your 
 problem?:
 http://d.puremagic.com/issues/show_bug.cgi?id=2192
 
 It looks like you're using iterable objects instead of AAs though. If you 
 think it seems like basically the same bug, maybe update the ticket to 
 indicate the problem is more general than just AAs. 
 
 

Looks like it's related, yes. I see that someone else has already noted that 
the problem happens with opApply, too.

Cheers.


Re: D2 phobos std.date question

2009-07-15 Thread John C
Sam Hu Wrote:

 Stewart Gordon Wrote:
 
  
  Read the source of std.date and see for yourself.  If it's getting it 
  wrong, it suggests either your system is misconfigured or you're using 
  it wrongly.  But since you still haven't posted your code, I still can't 
  comment further.
  
  Stewart.
 
 Thank you so much again.
 Actually I just tried the example based on the one inside the std.date source:
 /*
  * Converts UTC time into a text string of the form:
  * Www Mmm dd hh:mm:ss GMT+-TZ .
  * For example, Tue Apr 02 02:04:57 GMT-0800 1996.
  * If time is invalid, i.e. is d_time_nan,
  * the string Invalid date is returned.
  *
  * Example:
  * 
  */
   d_time lNow;
   string lNowString;//char[] lNowString;
 
   // Grab the date and time relative to UTC
   lNow = std.date.getUTCtime();
   // Convert this into the local date and time for display.
   lNowString = std.date.toString(lNow);
  /* 
  */
 //And add testing below:
 d_time localTime=std.date.UTCtoLocalTime(lNow);
 string localTimeString=std.date.toString(localTime);
 
 writefln(lNowString);
 writefln(localTimeString);
 
 ===
 output:
 ===
 Wed Jul 15 01:47:42 GMT+ 2009
 Wed Jul 15 01:47:42 GMT+ 2009
 
 
 

There's a bug in Phobos where the value used to calculate timezone offsets 
(localTZA) never gets initialised, because std_date_static_this() is not called.

A temporary fix is to import std.datebase.

John.


D2 MemoryStream

2009-06-30 Thread John C
MemoryStream's constructors are currently defined like this:

  this(ubyte[] buf);
  this(byte[] buf);
  this(char[] buf);

Does anyone use byte (as opposed to ubyte)? And if the purpose of the char[] 
overload is to accept a string, it should just take a string -- ie, 
immutable(char)[] -- instead. Bug?


Re: Object.factory create instance failed?

2009-06-26 Thread John C
Sam Hu Wrote:

 John C Wrote:
 
  Object.factory requires a fully qualified class name, so if class Dog 
  resides in module animals, call Object.factory(animals.Dog).
 
 It works now.Great!Thanks so much!
 
 But...may I ask how do you know that?(requires full name).I went through the 
 object.di but nothing relative found.
 

Common sense, to be honest.


Re: Object.factory create instance failed?

2009-06-25 Thread John C
Sam Hu Wrote:

 
 In below code snippet:
 
 class Dog
 {
 public void bark(){}
 }
 int main(string[] args)
 {
 auto obj=Object.factory(Dog);
 Dog doggie=cast(Dog)obj;
 doggie.bark;
 
 return 0;
 }
 
 Compiled successfully but failed when run:
 Object.Error:Access Violation.
 
 I also tried ClassInfo.find(string classname),ClassInfo.create() but caused 
 the same error.
 

Object.factory requires a fully qualified class name, so if class Dog resides 
in module animals, call Object.factory(animals.Dog).


Porting 1.x libraries to 2.x

2009-06-10 Thread John C
What strategies do library authors have for maintaining two versions of a their 
code - one for D 1.0 and another for 2.0? When they make changes to one 
version, do they manually copy them into the other branch? Or is there a way of 
automating the process?

Version blocks don't seem to help much.

I'm finding it very tedious and somewhat haphazard going the manual route - 
it's easy to forget, and sometimes I get so carried away adding new code to my 
1.0 library that it's a struggle to recall every line I've changed.

Ideally, I'd just update the 1.0 branch and run a script to make the necessary 
changes for it to compile with DMD 2.0. Do Unix/Linix people use the patch 
command for this (and is there a Windows equivalent)?

Thanks,
John. 


Re: sqlserver2000 for d2 api ?

2009-05-26 Thread John C
dolive Wrote:

 Who have the sqlserver2000 for d2 api ?  
 Can you give me ?  thank you !
 
 doliv...@sina.com

There's no API for SQL Server 2000, as such. You usually interface with SQL 
Server through the ODBC, OLEDB or ADO interfaces - the last two are COM-based.

I think older versions of DBI over on DSource supported ODBC - alas no longer, 
it seems. I couldn't find any other general database libraries for D. Anyone 
else?


Re: std.string and std.algorithm: what to do?

2009-05-14 Thread John C
Andrei Alexandrescu Wrote:

 Cool! So then how do I rename find, ifind, rfind, and irfind in std.string?
 
 Andrei

indexOf(bool ignoreCase = false), lastIndexOf(bool ignoreCase = false).


Re: A couple of questions

2009-05-13 Thread John C
Sam Hu Wrote:

 Thanks.The construct is clear now.
 
 Still leaves Q1,that is ,the *if* expression after the template definition,I 
 want to learn more about the usage,where can I find more information?

It is in the spec: http://www.digitalmars.com/d/2.0/template.html#Constraint


Re: 3 variant questions

2009-05-12 Thread John C
Saaa Wrote:

 
  import std.stdarg;
 
  assert( _arguments[0] is typeid(int*) );
  auto arg = va_arg!(int*)(_argptr);
  *arg = 10;
 
  Probably.
 
   -- Daniel
 
 Calling the following returns an Access Violation Error after
 correctly writing the two lines.
 
 void main()
 {
 int i;
 get( file, `i`, i);
 }
 
 public void get(in char[][] file, in char[] identifier, ...)
 {
 assert( _arguments[0] is typeid(int) );
 writefln(`assert done`);
 auto arg = va_arg!(int*)(_argptr);
 writefln(`assign done`);
 *arg = 7;
 return;
 } 
 


You get an AV because you're passing the argument by value. You need to pass 
its address instead.

Try this:

void main() {
  int i;
  get(file, i, i);
  writeln(i);
}

It will print 7.


Re: D2 string conversion

2009-05-10 Thread John C
Sam Hu Wrote:

 Hi Sivo  John,
 
 Thank you very much!!!The two versions all work.
 
 But I am a bit confused with the key word *in* in both versions.

It's simple, really. Strings are immutable in D2. And in is equivalent to 
immutable.

By the way, best to use wstring (for UTF-16) or string (for UTF-8) in your 
parameter declarations.


Re: convert *void to void[]

2009-05-06 Thread John C
gabrielsylar Wrote:

 can anybody please tell me how to properly convert from void* to void[]
 as the code below?
 
 void[] get_bytes(int n) { return sqlite3_column_blob(stmt, n); }

return sqlite3_column_blob(stmt, n)[0 .. n];


Re: Resource availability: fonts

2009-05-06 Thread John C
Tyro[a.c.edwards] Wrote:

 When I do this, how do I ensure that the program is able to locate the 
 font after extraction without installing it?
 

I think AddFontResource from the SDK will do that. 
http://msdn.microsoft.com/en-us/library/dd183326(VS.85).aspx


Re: line drawing package?

2009-05-03 Thread John C
BCS Wrote:

 I find my self in need of a line drawing package. I need to pop a window 
 and draw lines and points. Text would be nice but I can live without it. 
 Most importantly, I need something that is dirt simple to get running. I 
 don't have time to dink around with libs (if I did have time I'd be willing 
 but I'm already way behind as it is).
 
 Windows XP, D1.0, Phobos
 
 I'd also be able to use an out of process solution as in: generate input 
 file, call program if anyone knows of a windows program like that.
 
 

Had you ruled out CreateWindowEx, LineTo and DrawText from the Win32 API?


Re: convert and assign to the BSTR char

2009-04-18 Thread John C
new Wrote:

 hi,
 
 how does one convert char[] to BSTR and back? i am looking to use a dll that 
 contains function such as
 
 BSTR tester = gettest(BSTR *in, BSTR in1);
 
 any help is appreciated.

  wchar* bstr = SysAllocString(std.utf.toUTF16z(s));

and

  char[] s = std.utf.toUTF8(bstr[0 .. SysStringLen(bstr)]);
  SysFreeString(bstr);


Re: Template function : use the array's one !

2009-02-18 Thread John C
TSalm Wrote:

 Hello,
 
 I'm searching to have a generic comparator function.
 
 I done this :
 
 /*  CODE  */
 import tango.io.Stdout;
 
 /***
   * Not for arrays
   ***/
 int compare(T)(T o1,T o2)
 {
static if  ( is( T  bar == class )  || is(  T bar == struct )  )
  return o1.opCmp(o2);
 
else
  return o2 - o1;
 }
 
 /***
   * For arrays
   ***/
 int compare(T:T[])(T[] o1,T[] o2)

Change this line to:
  int compare(T:T[])(T o1, T o2)

 {
size_t minLength = ( o1.lengtho2.length ? o1.length : o2.length) ;
 
for (size_t i=0;iminLength;i++)
{
  int result = compare(o1[i],o2[i]);
 
  if (result != 0)
return result;
}
 
return o2.length - o1.length ;
 
 }
 
 /***
   * Test
   ***/
 void main()
 {
byte[] a = [10,12,13];
byte[] b = [15,16,17];
Stdout( compare(a,b) ).newline ;
 }
 /*  END CODE  */
 
 
 
 But this code return a compilation error :
 test_cmp.d(12): Error: cannot implicitly convert expression (o2 - o1) of  
 type byte[] to int
 test_cmp.d(41): template instance test_cmp.compare!(byte[]) error  
 instantiating
 
 It seems it's the first compare function (without array in parameter)  
 which is hook.
 
 How can I declare this compare function to use specific code to compare  
 arrays ?
 Thanks in advance,
 TSalm



Re: DMD 1.037 and 2.020 releases

2008-11-26 Thread John C
Kagamin Wrote:

  - The 'this' parameter to struct member functions is now a reference 
  type, I know this was discussed, but how does this change code? Does this 
  forces to change C code when it is ported to D? How to do such porting? Few 
  examples of situations may be useful.
 
 I'm affraid, this breaks my resource parser
 
 struct ResourceTable
 {
   ushort Shift; //alignment shift count
   ResourceType* FirstType()
   {
   return cast(ResourceType*)(this+1);
   }
 }

Try this:

  return cast(ResourceType*)(this + 1);