Re: Creating a new type, to get strong-ish type checking and restrict usage to certain operations, using struct perhaps

2017-07-21 Thread Cecil Ward via Digitalmars-d-learn

On Friday, 21 July 2017 at 18:49:21 UTC, Cecil Ward wrote:
I was think about how to create a new type that holds packed 
bcd values, of a choice of widths, that must fit into a 
uint32_t or a uint64_t (not really long multi-byte objects). I 
am not at all sure how to do it. I thought about using a 
templated struct to simply wrap a uint of a chosen width, and 
perhaps use alias this to make things nicer.


I guess part of my question, which I didn't really highlight well 
enough, is the issue of strong typing. Example: physical units 
types, such as amps and volts, implemented as say a double or 
float or real (want to template that) but disallow evil 
assignments, comparisons, addition etc of mixed types. Another 
one would be the prevention of mixing pounds and pence by 
straight addition, or straight comparisons and blocking straight 
assignment. I'm assuming in the latter case you might use a 
machine-architecture native integral type of whatever width, 
again templating wanted. These are all really old requests, I'm 
sure, but I would appreciate a start as to how to implement the 
strong type checking in D without too much pain.


Going back to the original example of packed bcd stored in a 
uint64_t say, first thing is that I want to ban illegal mixing of 
arbitrary binary values in ordinary uint64_tmtypes with decimal 
types, again no assignment, addition, comoarisons etc across 
types at all allowed. And no friendly automagically conversions 
from packed bcd to binary on the fly either - I want to treat 
that kind of usage as a straight bug by the user, for the moment 
at least, anyway, as I don't want to encourage silent horrible 
inefficiency creeping in.


Re: find difference between two struct instances.

2017-07-21 Thread FoxyBrown via Digitalmars-d-learn

On Saturday, 22 July 2017 at 01:04:48 UTC, Nicholas Wilson wrote:

On Friday, 21 July 2017 at 23:38:51 UTC, FoxyBrown wrote:

[...]


use opCmp in conjunction with __traits(allMembers,T)

struct Example
{
int a,b,c;
string d,e,f;
}

void difference(alias func, T)(T t1, T t2) 
if(__traits(compiles, func(t1,t2)))

{
foreach(U; __traits(allMembers,T)
{
if (mixin("t1." ~ U.stringof~ ".opCmp(t2." ~ U.stringof 
~")")

func(t1,t2);
}

}

auto a =  Example(1,2,3,"foo","bar",baz");
auto a =  Example(1,2,42,"foo","bar",quux");

difference!(writeln)(a,b); // hopefully prints 343\nbazquux

Not tested but should give you an idea to adapt as needed.


thanks, I'll try it out.


Re: executeShell not working

2017-07-21 Thread FoxyBrown via Digitalmars-d-learn

On Saturday, 22 July 2017 at 02:31:45 UTC, FoxyBrown wrote:

auto sss = "sc config \""~szSvcName~"\" start= disabled";
executeShell("sc config \""~szSvcName~"\" start= disabled");

but if I copy and paste the string in to an admin console, it 
works fine:


sc config "W32Time" start= disabled
[SC] ChangeServiceConfig SUCCESS

szSvcName is W32Time.

It's not an admin issue.

I *can't* set it to other states either. Does executeShell not 
run it's process with the same rights as the app?


even spawnProcess isn't doing it

spawnProcess(["C:\\Windows\\System32\\sc.exe","config", 
szSvcName, "start=","disabled"]);


or

spawnProcess(["C:\\Windows\\System32\\sc.exe","config 
"~szSvcName~" start= disabled"]);




executeShell not working

2017-07-21 Thread FoxyBrown via Digitalmars-d-learn

auto sss = "sc config \""~szSvcName~"\" start= disabled";
executeShell("sc config \""~szSvcName~"\" start= disabled");

but if I copy and paste the string in to an admin console, it 
works fine:


sc config "W32Time" start= disabled
[SC] ChangeServiceConfig SUCCESS

szSvcName is W32Time.

It's not an admin issue.

I *can't* set it to other states either. Does executeShell not 
run it's process with the same rights as the app?






Re: How can I serialize a struct into a file in the style of C?

2017-07-21 Thread Mike Parker via Digitalmars-d-learn

On Saturday, 22 July 2017 at 02:11:27 UTC, Mike Parker wrote:

On Saturday, 22 July 2017 at 01:45:29 UTC, solidstate1991 wrote:
Due to it's convenience, I was thinking on reading and writing 
file headers by creating structs mirroring the layouts of 
actual headers I would need. I've seen many examples of this 
in C, however I' struggling using the same methods through the 
use of code.stdc.stdio, especially as I can't really trace 
bugs from fread.


struct Data {
int x;
float y;
ubyte z;
}

void main() {
import core.stdc.stdio;

Data od = Data(10, 3.0f, 5);

FILE* fp = fopen("data.dat", "wb");
size_t ret = fwrite(, od.sizeof, 1, fp);
fclose(fp);

assert(ret == 1);

Data id;
fp = fopen("data.dat", "rb");
ret = fread(, id.sizeof, 1, fp);
fclose(fp);

assert(ret == 1);

assert(id.x == 10);
assert(id.y == 3.0f);
assert(id.z == 5);
}


I should add, though, that you're better off using either 
std.stdio.File or std.file. Use the former if you need to make 
multiple reads/writes to a file, the latter if you can pull it in 
or push it out all in one go. They take arrays as arguments, so 
if you have something like Data[], you can pass it directly to 
the appropriate functions. To write a single instance, you'll 
have to take the pointer and slice it. Either way, it's less 
code, less error prone, and more idiomatic than using the C API.


Re: How can I serialize a struct into a file in the style of C?

2017-07-21 Thread Mike Parker via Digitalmars-d-learn

On Saturday, 22 July 2017 at 01:45:29 UTC, solidstate1991 wrote:
Due to it's convenience, I was thinking on reading and writing 
file headers by creating structs mirroring the layouts of 
actual headers I would need. I've seen many examples of this in 
C, however I' struggling using the same methods through the use 
of code.stdc.stdio, especially as I can't really trace bugs 
from fread.


struct Data {
int x;
float y;
ubyte z;
}

void main() {
import core.stdc.stdio;

Data od = Data(10, 3.0f, 5);

FILE* fp = fopen("data.dat", "wb");
size_t ret = fwrite(, od.sizeof, 1, fp);
fclose(fp);

assert(ret == 1);

Data id;
fp = fopen("data.dat", "rb");
ret = fread(, id.sizeof, 1, fp);
fclose(fp);

assert(ret == 1);

assert(id.x == 10);
assert(id.y == 3.0f);
assert(id.z == 5);
}


How can I serialize a struct into a file in the style of C?

2017-07-21 Thread solidstate1991 via Digitalmars-d-learn
Due to it's convenience, I was thinking on reading and writing 
file headers by creating structs mirroring the layouts of actual 
headers I would need. I've seen many examples of this in C, 
however I' struggling using the same methods through the use of 
code.stdc.stdio, especially as I can't really trace bugs from 
fread.


Re: Cannot find std.datetime when linking after upgrade to 2.075.0

2017-07-21 Thread Domain via Digitalmars-d-learn

On Friday, 21 July 2017 at 19:05:00 UTC, Jonathan M Davis wrote:
On Friday, July 21, 2017 15:33:45 Domain via 
Digitalmars-d-learn wrote:
After upgrade dmd to latest 2.075.0, my project no longer 
build:


zero.lib(core_cde_4a4f.obj) : error LNK2001: unresolved 
external symbol _D3std8d 
atetime9LocalTime6opCallFNaNbNeZyC3std8datetime9LocalTime


and many more. All about std.datetime.


Then it sounds like you need to make sure that you rebuild your 
project and all of its dependencies (which you should be doing 
with any compiler upgrade anyway, since they're not ABI 
compatible). Your code should work just fine with std.datetime 
and 2.075.0 without any changes, but it will need to be 
recompiled.


https://dlang.org/changelog/2.075.0.html#split-std-datetime

- Jonathan M Davis


I have tried dub build --force and rebuild solution in VisualD, 
but nothing changed. Maybe I have to remove the lib manually.




Re: find difference between two struct instances.

2017-07-21 Thread Nicholas Wilson via Digitalmars-d-learn

On Friday, 21 July 2017 at 23:38:51 UTC, FoxyBrown wrote:

On Friday, 21 July 2017 at 22:35:20 UTC, Era Scarecrow wrote:

On Friday, 21 July 2017 at 21:03:22 UTC, FoxyBrown wrote:
Is there a way to easily find the differences between to 
struct instances? I would like to report only the differences


e.g.,

writeln(s1 - s2);

prints only what is different between s1 and s2.




No, it isn't. It is a concept, wasn't mean to be taken as 
literal D code.


 This is entirely dependent on the structs in question, you 
can't just subtract any struct from another struct unless it 
knows how to do it.


 Depends on what the structures hold. You'll probably have to 
either make an opSub, a function to call opBinary!"-", or do 
opCmp which returns which is higher/lower (and may be as 
simple as subtraction).


Why do I want to go through all that trouble? A simple binary 
compare should suffice.


use opCmp in conjunction with __traits(allMembers,T)

struct Example
{
int a,b,c;
string d,e,f;
}

void difference(alias func, T)(T t1, T t2) if(__traits(compiles, 
func(t1,t2)))

{
foreach(U; __traits(allMembers,T)
{
if (mixin("t1." ~ U.stringof~ ".opCmp(t2." ~ U.stringof 
~")")

func(t1,t2);
}

}

auto a =  Example(1,2,3,"foo","bar",baz");
auto a =  Example(1,2,42,"foo","bar",quux");

difference!(writeln)(a,b); // hopefully prints 343\nbazquux

Not tested but should give you an idea to adapt as needed.


Re: find difference between two struct instances.

2017-07-21 Thread FoxyBrown via Digitalmars-d-learn

On Friday, 21 July 2017 at 22:35:20 UTC, Era Scarecrow wrote:

On Friday, 21 July 2017 at 21:03:22 UTC, FoxyBrown wrote:
Is there a way to easily find the differences between to 
struct instances? I would like to report only the differences


e.g.,

writeln(s1 - s2);

prints only what is different between s1 and s2.




No, it isn't. It is a concept, wasn't mean to be taken as literal 
D code.


 This is entirely dependent on the structs in question, you 
can't just subtract any struct from another struct unless it 
knows how to do it.


 Depends on what the structures hold. You'll probably have to 
either make an opSub, a function to call opBinary!"-", or do 
opCmp which returns which is higher/lower (and may be as simple 
as subtraction).


Why do I want to go through all that trouble? A simple binary 
compare should suffice.


Re: find difference between two struct instances.

2017-07-21 Thread Era Scarecrow via Digitalmars-d-learn

On Friday, 21 July 2017 at 21:03:22 UTC, FoxyBrown wrote:
Is there a way to easily find the differences between to struct 
instances? I would like to report only the differences


e.g.,

writeln(s1 - s2);

prints only what is different between s1 and s2.


 This is entirely dependent on the structs in question, you can't 
just subtract any struct from another struct unless it knows how 
to do it.


 Depends on what the structures hold. You'll probably have to 
either make an opSub, a function to call opBinary!"-", or do 
opCmp which returns which is higher/lower (and may be as simple 
as subtraction).





find difference between two struct instances.

2017-07-21 Thread FoxyBrown via Digitalmars-d-learn
Is there a way to easily find the differences between to struct 
instances? I would like to report only the differences


e.g.,

writeln(s1 - s2);

prints only what is different between s1 and s2.


Re: Creating a new type, to get strong-ish type checking and restrict usage to certain operations, using struct perhaps

2017-07-21 Thread Ali Çehreli via Digitalmars-d-learn

On 07/21/2017 11:49 AM, Cecil Ward wrote:

I was think about how to create a new type that holds packed bcd values,
of a choice of widths, that must fit into a uint32_t or a uint64_t (not
really long multi-byte objects). I am not at all sure how to do it. I
thought about using a templated struct to simply wrap a uint of a chosen
width, and perhaps use alias this to make things nicer.



Andrei's checkedint may give ideas:

  https://dlang.org/phobos/std_experimental_checkedint.html

He presented it in this talk:

  http://dconf.org/2017/talks/alexandrescu.html

... which is missing the following video link:

  https://www.youtube.com/watch?v=29h6jGtZD-U

... which has a longer version:

  https://www.youtube.com/watch?v=es6U7WAlKpQ

Ali



Re: Creating a new type, to get strong-ish type checking and restrict usage to certain operations, using struct perhaps

2017-07-21 Thread Moritz Maxeiner via Digitalmars-d-learn

On Friday, 21 July 2017 at 18:49:21 UTC, Cecil Ward wrote:
I was think about how to create a new type that holds packed 
bcd values, of a choice of widths, that must fit into a 
uint32_t or a uint64_t (not really long multi-byte objects). I 
am not at all sure how to do it. I thought about using a 
templated struct to simply wrap a uint of a chosen width, and 
perhaps use alias this to make things nicer.


That's usually how this is done. Take a look at the new 
std.experimental.checkedint for inspiration [1].


Here's a shell to start with (fill in:
---
struct BCDInteger(ubyte bitWidth) if (bitWidth <= 128)
{
private:
enum byteWidth = // Add bit to byte width conversion (or just 
take byte width as template parameter)

ubyte[byteWidth] store;
public:
// Add constructor(s)/static factory functions
// Overload operators,
// Add conversions to other integer formats
// Add alias this for conversion to two complements format
}
---

[1] 
https://github.com/dlang/phobos/blob/v2.075.0/std/experimental/checkedint.d#L213


Re: Cannot find std.datetime when linking after upgrade to 2.075.0

2017-07-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, July 21, 2017 15:33:45 Domain via Digitalmars-d-learn wrote:
> After upgrade dmd to latest 2.075.0, my project no longer build:
>
> zero.lib(core_cde_4a4f.obj) : error LNK2001: unresolved external
> symbol _D3std8d
> atetime9LocalTime6opCallFNaNbNeZyC3std8datetime9LocalTime
>
> and many more. All about std.datetime.

Then it sounds like you need to make sure that you rebuild your project and
all of its dependencies (which you should be doing with any compiler upgrade
anyway, since they're not ABI compatible). Your code should work just fine
with std.datetime and 2.075.0 without any changes, but it will need to be
recompiled.

https://dlang.org/changelog/2.075.0.html#split-std-datetime

- Jonathan M Davis



Creating a new type, to get strong-ish type checking and restrict usage to certain operations, using struct perhaps

2017-07-21 Thread Cecil Ward via Digitalmars-d-learn
I was think about how to create a new type that holds packed bcd 
values, of a choice of widths, that must fit into a uint32_t or a 
uint64_t (not really long multi-byte objects). I am not at all 
sure how to do it. I thought about using a templated struct to 
simply wrap a uint of a chosen width, and perhaps use alias this 
to make things nicer.




Re: Cannot find std.datetime when linking after upgrade to 2.075.0

2017-07-21 Thread zabruk70 via Digitalmars-d-learn

https://dlang.org/changelog/2.075.0.html#split-std-datetime


Cannot find std.datetime when linking after upgrade to 2.075.0

2017-07-21 Thread Domain via Digitalmars-d-learn

After upgrade dmd to latest 2.075.0, my project no longer build:

zero.lib(core_cde_4a4f.obj) : error LNK2001: unresolved external 
symbol _D3std8d

atetime9LocalTime6opCallFNaNbNeZyC3std8datetime9LocalTime

and many more. All about std.datetime.


Re: Check whether string value represents a type

2017-07-21 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/21/17 10:21 AM, Timoses wrote:

I'd love to check whether a string value is the name of a type at run-time.

E.g.:


string a = "int";
string b = "im no type";
assert( isStringType(a) );
assert( !isStringType(b) );

or

struct TestStruct
{
 int test;
}

string t = "TestStruct";
assert( isStringType(t) );


Is anything like this possible?

The goal is to identify whether a string represents a custom type within 
a package. I'm also trying to iterate over all modules within the 
package to get the struct name. However, that seems like a struggle...


Any ideas?..


In order to do this, the list of types must be stored somewhere to 
compare with at runtime.


At this time, this is not done. You could potentially do this with the 
RTInfo template, but it would have to be part of a custom druntime.


-Steve


Check whether string value represents a type

2017-07-21 Thread Timoses via Digitalmars-d-learn
I'd love to check whether a string value is the name of a type at 
run-time.


E.g.:


string a = "int";
string b = "im no type";
assert( isStringType(a) );
assert( !isStringType(b) );

or

struct TestStruct
{
int test;
}

string t = "TestStruct";
assert( isStringType(t) );


Is anything like this possible?

The goal is to identify whether a string represents a custom type 
within a package. I'm also trying to iterate over all modules 
within the package to get the struct name. However, that seems 
like a struggle...


Any ideas?..


Re: How to replace pairs tags with regexp

2017-07-21 Thread Antonio Corbi via Digitalmars-d-learn

On Friday, 21 July 2017 at 07:42:28 UTC, Suliman wrote:

On Friday, 21 July 2017 at 07:30:07 UTC, Antonio Corbi wrote:

On Friday, 21 July 2017 at 07:08:34 UTC, Suliman wrote:

On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:

There reason of issue above is spaces before "#".


What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56

I expect that it will select:

#Header
my header text

##SubHeader
my sub header text

Because: ^#{3}


Have you tried https://regex101.com/

It gives you results and explanations about your regex in 
realtime.


A. Corbi


I tried. But I am getting different behavior in online editor 
and in the code.


Could it be related to the regex's 'flavor' you are using?

A. Corbi


Re: How to replace pairs tags with regexp

2017-07-21 Thread Suliman via Digitalmars-d-learn

On Friday, 21 July 2017 at 07:30:07 UTC, Antonio Corbi wrote:

On Friday, 21 July 2017 at 07:08:34 UTC, Suliman wrote:

On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:

There reason of issue above is spaces before "#".


What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56

I expect that it will select:

#Header
my header text

##SubHeader
my sub header text

Because: ^#{3}


Have you tried https://regex101.com/

It gives you results and explanations about your regex in 
realtime.


A. Corbi


I tried. But I am getting different behavior in online editor and 
in the code.


Re: How to replace pairs tags with regexp

2017-07-21 Thread Antonio Corbi via Digitalmars-d-learn

On Friday, 21 July 2017 at 07:08:34 UTC, Suliman wrote:

On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:

There reason of issue above is spaces before "#".


What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56

I expect that it will select:

#Header
my header text

##SubHeader
my sub header text

Because: ^#{3}


Have you tried https://regex101.com/

It gives you results and explanations about your regex in 
realtime.


A. Corbi


Re: How to replace pairs tags with regexp

2017-07-21 Thread Suliman via Digitalmars-d-learn

On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:

There reason of issue above is spaces before "#".


What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56

I expect that it will select:

#Header
my header text

##SubHeader
my sub header text

Because: ^#{3}


Re: How to replace pairs tags with regexp

2017-07-21 Thread Suliman via Digitalmars-d-learn

There reason of issue above is spaces before "#".