Re: Using replaceInPlace, string and char[]

2015-08-16 Thread TSalm via Digitalmars-d-learn

 Must create a ticket for it ?

I think so. Unless others object in 10 minutes... :)

:-)
Done : https://issues.dlang.org/show_bug.cgi?id=14925

Thanks for your help


Using replaceInPlace, string and char[]

2015-08-15 Thread TSalm via Digitalmars-d-learn

Hi,

A newbie question :

I wrote this simple code :

import std.array;
import std.stdio;

void main()
{

 char[] a = mon texte 1.dup;
 char[] b = abc.dup;
 size_t x   = 4;
 size_t y   = 9;
 replaceInPlace( a, x , y, b );
 writeln( a );
}

But compilation fails :

/usr/include/dmd/phobos/std/array.d(2052): Error: template 
std.algorithm.remove cannot deduce function from argument types 
!()(char[], Tuple!(immutable(uint), uint)), candidates are:
/usr/include/dmd/phobos/std/algorithm.d(8542):
std.algorithm.remove(SwapStrategy s = SwapStrategy.stable, Range, 
Offset...)(Range range, Offset offset) if (s != 
SwapStrategy.stable  isBidirectionalRange!Range  
hasLvalueElements!Range  hasLength!Range  Offset.length = 1)
/usr/include/dmd/phobos/std/algorithm.d(8623):
std.algorithm.remove(SwapStrategy s = SwapStrategy.stable, Range, 
Offset...)(Range range, Offset offset) if (s == 
SwapStrategy.stable  isBidirectionalRange!Range  
hasLvalueElements!Range  Offset.length = 1)
/usr/include/dmd/phobos/std/algorithm.d(8757):
std.algorithm.remove(alias pred, SwapStrategy s = 
SwapStrategy.stable, Range)(Range range) if 
(isBidirectionalRange!Range  hasLvalueElements!Range)
inout.d(13): Error: template instance 
std.array.replaceInPlace!(char, char[]) error instantiating


Don't understand why this doesn't work: it compiles fine and runs 
perfectly if I change char[] by string ... don't understand 
why since the documentation says :

  String literals are immutable (read only).

How this function can change a type that is immutable ?

Thanks for your help.
TSalm






Re: Using replaceInPlace, string and char[]

2015-08-15 Thread TSalm via Digitalmars-d-learn

On Saturday, 15 August 2015 at 08:07:43 UTC, Ali Çehreli wrote:
This looks like a bug to me. The template constraints of the 
two overloads are pretty complicated. This case should match 
only one of them.


Yes I understand. I've used ldc2. With DMD (v0.067.1) the error 
is more clear :
inout.d(11): Error: std.array.replaceInPlace called with argument 
types (char[], uint, uint, char[]) matches both:
/usr/include/dmd/phobos/std/array.d(2214): 
std.array.replaceInPlace!(char, char[]).replaceInPlace(ref char[] 
array, uint from, uint to, char[] stuff)

and:
/usr/include/dmd/phobos/std/array.d(2247): 
std.array.replaceInPlace!(char, char[]).replaceInPlace(ref char[] 
array, uint from, uint to, char[] stuff)

Must create a ticket for it ?


 Don't understand why this doesn't work: it compiles fine and
runs
 perfectly if I change char[] by string

You mean, this:

import std.array;
import std.stdio;

void main()
{
string a = mon texte 1;// -- now string
writeln(a.ptr);  // added
char[] b = abc.dup;
size_t x   = 4;
size_t y   = 9;
replaceInPlace( a, x , y, b );
writeln( a );
writeln(a.ptr);  // added
}

The output:

4BC480
mon abc 1
7FC2AB867210-- different

 ... don't understand why
 since the documentation says :
String literals are immutable (read only).

 How this function can change a type that is immutable ?

It cannot change the characters of the original string. 
replaceInPlace takes its first parameter by reference. What 
changes is 'a' itself. As evidenced by the output of the 
program, 'a' is now a slice to a new set of immutable 
characters.


If there were other slices to mon texte 1, they wouldn't see 
a change.


Yes I understand, thanks. In the other hand using string is not 
efficient since this certainly make a copy of the original 
string. Right ?
This is better to use replaceInPlace with char[], but this 
doesn't actually work :-(





Searching for a tool to use D for web development with Apache

2014-10-12 Thread TSalm via Digitalmars-d-announce

Hello,

Does it exist a project to use D in web development with Apache; 
not for CGI scripts, but rather like what PHP does ?


Thanks in advance for your help.

I hope this is the correct forum to post my question on. Sorry if 
not.


Regards.
-TSalm


C equivalent for the D float type

2009-09-18 Thread TSalm

Hello,

What is the C equivalent for the D float type ?

Thanks in advance,
TSalm


Re: C equivalent for the D float type

2009-09-18 Thread TSalm

What is the C equivalent for the D float type ?


float and double is double.

in c, real support is compiler dependant, it may or may not
be available.


Thanks !


Re: const argument

2009-03-28 Thread TSalm
Le Sat, 28 Mar 2009 12:21:52 +0100, Jarrett Billingsley  
jarrett.billings...@gmail.com a écrit:



On Sat, Mar 28, 2009 at 6:12 AM, TSalm ts...@free.fr wrote:

Hello,

Is there a way to specifie a constant argument ( I would say an  
argument for

which his value is evaluate at compile time )

For example, something like this :

/*  CODE - */
import tango.io.Stdout;

void func(const bool constArg)
{
   static if (constArg)
       Stdout(Yes).newline;
   else
       Stdout(No).newline;
}

void main()
{
   func( true );
   func( false );
}
/*  END CODE - */


You have to do it with templates:

void func(bool constArg)()
{
static if(constArg) ... else ...
}

func!(true)();
func!(false)();


Thanks.


Walter suggested, in the D2 presentation at the conference in 2007,
that there should be static parameters which would work the way your
code should work.

good idea IMO.


Re: Generic functions to convert to void* and from void*

2009-02-25 Thread TSalm
Le Wed, 25 Feb 2009 01:03:32 +0100, Daniel Keep  
daniel.keep.li...@gmail.com a écrit:




TSalm wrote:

In my case, there's also no possibility to get the wrong type, because
it is managed by the type of the ColumnMem.


You still have to get the code right.  There's a surprising number of
corner cases trying to store arbitrary types.


And about Object, if I want to store base type like int,double,etc...,
if I do something like :

 Object o;
 int a = 30 ;
 o = cast(Object) a ;

is this syntax is GC safe ?


It's not safe, period.  If the compiler lets you do that, I'd be
tremendously surprised; even more surprised if it doesn't cause major
problems later.  This is what I'm worried about; you're doing dangerous
things with a type system you don't understand.  Don't do this.

Here's the problem: void* isn't going to work for everything.  It'll
work for Object references, other pointers, and that's it.  You can't
store arrays, and you can't store value types like structs or primitive
types.  For that, you need to allocate heap storage, copy the value and
then store the pointer to that.  Oh, and don't forget that fixed-length
arrays have value semantics whereas dynamic arrays and slices have
reference semantics; although you generally solve that issue by having a
special template for your type which rewrites T[n] as T[].  Also,
delegates won't fit, but function pointers will.

This is why I was pointing you at Variant because I already went through
the trouble to solve all this once.  :P

If you still want to do this with void*, build that code in isolation
and test the heck out of it.


You are right.
I will first use Variant to implements all functionnalities, and  
insolating the storage type.

And in the future, I will be able to use void* as an improvement.


Re: Generic functions to convert to void* and from void*

2009-02-24 Thread TSalm



TSalm wrote:
I'm trying to build function which have the hability to convert a  
type

to void* and from void*.


First of all, I have to ask: have you looked at std.variant /
tango.core.Variant?


Yes, but it seems that Variant class uses more memory than void* .


The Phobos Variant will use however much space you reserve as the
maximum, plus 4 bytes for a function pointer, but it can only store
types as big as you allow for.  The Tango version will use
max(real.sizeof,void[].sizeof) + 4 bytes for the typeid and can store
anything you throw at it.

For that extra space, both of these will give you runtime type safety,
meaning you can't accidentally get the types wrong.  They're MUCH safer
than void*.

In my case, there's also no possibility to get the wrong type, because it  
is managed by the type of the ColumnMem.




[...]



I get the distinct impression that you're seriously over-thinking this.
 Both of these functions could be rewritten as casts.  Aside from that,
you've given no context for me to have any idea what you're trying to
accomplish here.



I'm really a newbie concerning the use of void* ( I think you have
notice this ;-)  )
Thanks for your usefull remarks.


void* is just a pointer like any other.  It doesn't have any special
properties except that you cannot dereference it; that's it.  If you're
not sure how to use pointers, then don't.

For example, you could store objects instead; this takes the same amount
of storage in the DBMS, and allows for safe casting back to the original
type.  Plus, you don't have to stuff about with casting things to void*
and back.




I'm simply trying to make a little and lightweight DBMS in memory.
Simply made by classes like TableMem, RowMem and ColumnMem(T).
There's also a private class which aims to store datas, using  
void*[][].


Unless you really need to store small value types like integers, etc. in
that private data, objects might be the best bet for now.



Thanks.

And about Object, if I want to store base type like int,double,etc..., if  
I do something like :


 Object o;
 int a = 30 ;
 o = cast(Object) a ;

is this syntax is GC safe ?



Re: Generic functions to convert to void* and from void*

2009-02-23 Thread TSalm

I'm trying to build function which have the hability to convert a type
to void* and from void*.


First of all, I have to ask: have you looked at std.variant /
tango.core.Variant?


Yes, but it seems that Variant class uses more memory than void* .


[...]



I get the distinct impression that you're seriously over-thinking this.
 Both of these functions could be rewritten as casts.  Aside from that,
you've given no context for me to have any idea what you're trying to
accomplish here.



I'm really a newbie concerning the use of void* ( I think you have notice  
this ;-)  )

Thanks for your usefull remarks.

I'm simply trying to make a little and lightweight DBMS in memory.
Simply made by classes like TableMem, RowMem and ColumnMem(T).
There's also a private class which aims to store datas, using void*[][].


Generic functions to convert to void* and from void*

2009-02-22 Thread TSalm

Hello,

I'm trying to build function which have the hability to convert a type to  
void* and from void*.

I must use ref in the toPtr function because of this :
http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.comgroup=digitalmars.D.learnartnum=15600

Do you think that what I done is correct ?


/* - CODE - */

/***
 * Convert a value to a void*
 * Params:
 * val =
 * Returns:
 */
void* toPtr(T)(ref T val)
{
  void* p ;

  static if ( is( T b : Type ) )
  {
p = new T ;
p = val ;
  }
  else
  {
p = val ;
  }

  return p ;
}


/***
 * Convert a void* to his value
 * Params:
 * ptr =
 * Returns:
 */
T fromPtr(T)(void* ptr)
{
  return *(cast(T*)ptr) ;
}
/* --- END CODE  */


Thanks in advance,
TSalm


Re: Generic functions to convert to void* and from void*

2009-02-22 Thread TSalm


I'm trying to build function which have the hability to convert a type  
to void* and from void*.

I must use ref in the toPtr function because of this :
http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.comgroup=digitalmars.D.learnartnum=15600

Do you think that what I done is correct ?


/* - CODE - */

/***
  * Convert a value to a void*
  * Params:
  * val =
  * Returns:
  */
void* toPtr(T)(ref T val)
{
   void* p ;

   static if ( is( T b : Type ) )
   {
 p = new T ;
 p = val ;
   }
   else
   {
 p = val ;
   }

   return p ;
}


/***
  * Convert a void* to his value
  * Params:
  * ptr =
  * Returns:
  */
T fromPtr(T)(void* ptr)
{
   return *(cast(T*)ptr) ;
}
/* --- END CODE  */



I've forget to say that toPtr can't be call with constants.


Re: Symbol undefined on interface with public getter and package setter

2009-02-21 Thread TSalm
Le Sat, 21 Feb 2009 04:00:42 +0100, Daniel Keep  
daniel.keep.li...@gmail.com a écrit:





TSalm wrote:


I'm not sure but I think package is not virtual.


:-(
So there's really no way to have a method declared package in an
interface ?


You also can't have a private function in an interface.  This once lost
me four days trying to figure out why my program wouldn't link despite
the function very obviously being there.

Stick to public functions only.


What a pity :(

Thanks.


Symbol undefined on interface with public getter and package setter

2009-02-20 Thread TSalm

Hello,

When I compile the code below, I've got the following error :
 OPTLINK (R) for Win32  Release 8.00.1
 Copyright (C) Digital Mars 1989-2004  All rights reserved.
 private_method_in_interface_file3.obj(private_method_in_interface_file3)
  Error 42: Symbol Undefined  
_D33private_method_in_interface_file31I4funcMFiZv

 --- errorlevel 1


/* - CODE -- */
interface I
{
   int func() ;
   package void func(int);
}

class A:I
{
  int i;

  package  void func(int i)
  { this.i = i; }

  int func()
  { return i; }
}

void main()
{

  I a = new A ;
  a.func = 10 ;
  Stdout(a.func).newline ;

}
/* --- END CODE  */


Thanks in advance for your help,
TSalm


Re: Symbol undefined on interface with public getter and package setter

2009-02-20 Thread TSalm

It seems this comes only from the package method.

The error is the same with this code :
/* --- CODE --- */
interface I
{
   package void setFunc(int);
}


class A:I
{
  int i;

  package  void setFunc(int i)
  { this.i = i ; }

}


void main()
{

  I a = new A;
  a.setFunc = 10;

}
/* --- END CODE --- */


Hello,

When I compile the code below, I've got the following error :
  OPTLINK (R) for Win32  Release 8.00.1
  Copyright (C) Digital Mars 1989-2004  All rights reserved.
  private_method_in_interface_file3.obj(private_method_in_interface_file3)
   Error 42: Symbol Undefined  
_D33private_method_in_interface_file31I4funcMFiZv

  --- errorlevel 1


/* - CODE -- */
interface I
{
int func() ;
package void func(int);
}

class A:I
{
   int i;

   package  void func(int i)
   { this.i = i; }

   int func()
   { return i; }
}

void main()
{

   I a = new A ;
   a.func = 10 ;
   Stdout(a.func).newline ;

}
/* --- END CODE  */


Thanks in advance for your help,
TSalm




Re: Symbol undefined on interface with public getter and package setter

2009-02-20 Thread TSalm


I'm not sure but I think package is not virtual.


:-(
So there's really no way to have a method declared package in an  
interface ?




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

2009-02-18 Thread TSalm

TSalm Wrote:




int compare(T:T[])(T[] o1,T[] o2)


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



You are right.

But despite this function, at compile time, an error is return :

.\src\tsalm\tools\Generic.d(20): Error: cannot implicitly convert  
expression (o2 - o1) of type int[3u] to int
.\src\tsalm\tools\Generic.d(33): template instance  
tsalm.tools.generic.compare!(int[3u]) error instantiating

Command C:\DMD\dsss\bin\rebuild.exe returned with code 1, aborting.
Error: Command failed, aborting.


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

2009-02-17 Thread TSalm

take a look at static if and is

http://www.digitalmars.com/d/1.0/version.html#staticif
http://www.digitalmars.com/d/1.0/expression.html#IsExpression


Thanks for this links.
But I don't see anything about how to test if it's an array or not...
Is it not possible ?



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

2009-02-17 Thread TSalm

Le Tue, 17 Feb 2009 23:03:04 +0100, BCS n...@anon.com a écrit:


Hello TSalm,


take a look at static if and is
 http://www.digitalmars.com/d/1.0/version.html#staticif
http://www.digitalmars.com/d/1.0/expression.html#IsExpression


Thanks for this links.
But I don't see anything about how to test if it's an array or not...
Is it not possible ?


look at point 5

IIRC:

static if(is(T B : B[]))
{
// if T is an array, this is used and B is the element type
}





Incredible !
Thanks a lot BCS.


Re: return *(cast(T*)vPtr) and OutOfMemoryException

2009-02-14 Thread TSalm

Excellent explication !
Thank you Frits

Le Sat, 14 Feb 2009 17:58:35 +0100, Frits van Bommel  
fvbom...@remwovexcapss.nl a écrit:



TSalm wrote:

Hello,
 In the code below, why the first Stdout throws a Exception when the  
second doesn't ?

 /*   CODE    */
import tango.io.Stdout;
  struct VoidPtr(T)
{
  void* vPtr;
   void value(T val)
  {
vPtr = val;


Here you're storing a pointer to a non-ref parameter. This is the bug;  
the parameter itself is implicitly deallocated on returning from this  
function.

Change to 'ref T val' to fix it.

Essentially, an array is a struct { size_t length; T ptr; }. This means  
'val' refers to a copy in the stack frame of this function, not to 'arr'  
in main().



  }
   T value()
  {
return *(cast(T*)vPtr);
  }
}
  void main()
{
  VoidPtr!(char[][]) vPtr ;
   char[][]  arr = [ hello , you ];
   vPtr.value =  arr ;
   Stdout( vPtr.value ).newline; // --  
[tango.core.Exception.OutOfMemoryException: Memory allocation failed


Here you're calling a new function, overwriting *vPtr with something  
else (probably vptr itself), resulting in a huge array when you try to  
read it later.



   Stdout( *(cast(char[][]*) vPtr.vPtr ) ); // -- This works good


This reads the (implicitly deallocated) 'val' parameter before it gets  
overwritten, hiding the bug. It's still there: just because the code  
doesn't crash doesn't mean it's correct.



}
 /*  -- END CODE --  */
  Thanks in advance for your help,
TSalm




Re: Internal delegate and Stack Overflow

2008-11-29 Thread tsalm

Le Sat, 29 Nov 2008 01:49:20 +0100, BCS [EMAIL PROTECTED] a écrit:


struct C(R, A...)
{
  A args;
  R function(A) dg;
  static R delegate() opCall(R function(A) dg, A a)
  {
 C!(R, A) ret;
 ret.dg=dg;
 foreach(int i,_;A)
ret.args[i] = a[i];
 return ret.fn;
  }
  R fn() { return dg(args); }
}
  // test it
 import std.stdio;
 int delegate() dg(int i)
{
  return C!(int,int)(function int(int j){return j;}, i);   // --- used  
here

}
 void main()
{
  auto d = dg(5);
  writef(%s\n, d());
}


Interesting code. Thanks!


Re: Internal delegate and Stack Overflow

2008-11-28 Thread tsalm

Le Sat, 29 Nov 2008 01:08:28 +0100, BCS [EMAIL PROTECTED] a écrit:


Reply to TSalm,


Hello,
 I would do something like this, but this return me an execution error
: object.Exception: Stack Overflow
 // CODE
class A
{
void delegate() dg;
void doIt()
{
dg();
}
}
class B
{
A a;
this()
{
a = new A;
a.dg = { doSomething(); };
}
void doSomething() { }
}
void main()
{
auto b = new B;
b.a.doIt();
}
// --END CODE--
 Is this a bug or have I do something wrong ?
Thanks in advance for your help,
TSalm


If this is d1.0 the error is that you are allowing an anon delegate to  
escape the enclosing function.


Yes, it's on D1.036.




a.dg = this.doSomething;  // this would be ok if that helps.


 if it is 2.0, I think this is correct.



Yes, you are right.
But this is an example code. The true code uses delegates with argument  
which differs from called functions, so I can't point delegate directly to  
them.
And this anonymous function's way is really fastest to code... I must  
waiting for a D2 stable version ;-)


Thanks


Compilation error when using a extern function ?

2008-11-20 Thread tsalm

Hello,


I'm on DMD 1.036.
When I'm compiling this code :

// -CODE---


enum ColorValues:uint
{
 AliceBlue  = 0xF0F8
,AntiqueWhite  = 0xF8ECD8FF
,Aqua  = 0x00FF
,Aquamarine  = 0x80FFD0FF
,Azure  = 0xF0FF
,Beige  = 0xF8F4E0FF
,Bisque  = 0xFFE4C8FF

 [...]


Color convertToColor(uint val)
{
ubyte[4] colorVal = *cast(ubyte*)val;

return Color(colorVal[0]
,colorVal[1]
,colorVal[2]
,colorVal[3]
 );
}

class DFLCalendar:Panel
{
// Graphicals objects
protected
{
// Colors default preferences :
const Color colorPanMonth  =  
convertToColor(cast(uint)ColorValues.MidnightBlue);
const Color colorPanDays   =  
convertToColor(cast(uint)ColorValues.White);

const uint  heightPanMonth = 16 ;
const uint  widthButChangeMonth = 10;

[...]
// -END CODE---

The compiler return me the error :

	tools\gui\tools\DFLCalendar.d(29): Error: cannot evaluate  
convertToColor(404254975u) at compile time


Line 29 corresponds to const Color colorPanMonth  =  
convertToColor(cast(uint)ColorValues.MidnightBlue);



Thanks in advance for your help,
TSalm