Recommendation for parallelism with nested for loops?

2022-08-18 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. I want to parallelize a computation which has two for loops, one nested within another. All inner-loop-param+outer-loop-param combinations can be computed independent of one another. As I suspected, [https://forum.dlang.org/post/xysyidbkjdinclmrx...@forum.dlang.org](this forum post) sa

Can't use function with same name as module?

2017-10-16 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. fun.d: import std.stdio; void fun() { writeln("Hello"); } main.d: import fun; void main() { fun(); } $ dmd -oftest fun.d main.d main.d(2): Error: function expected before (), not module fun of type void Why can't I use a function of the same name as the module? IIUC import fun import

Re: Can't use function with same name as module?

2017-10-17 Thread Shriramana Sharma via Digitalmars-d-learn
On Tuesday, 17 October 2017 at 07:33:15 UTC, evilrat wrote: Compiler made that way so it doesn't guess or assume too much, because later on it will bite you when you don't even expect that, and in some unpredictable place too. Can you give an example for when it will bite me? It seems very na

Re: Can't use function with same name as module?

2017-10-17 Thread Shriramana Sharma via Digitalmars-d-learn
On Tuesday, 17 October 2017 at 08:26:12 UTC, Daniel Kozak wrote: You can: import fun : fun; Yes I found this but it is unnecessarily verbose. At the same time I also find that it is possible to declare a struct or class with the same name as module: str.d: struct str { int a; } strmain.d:

Re: Can't use function with same name as module?

2017-10-17 Thread Shriramana Sharma via Digitalmars-d-learn
Have reported https://issues.dlang.org/show_bug.cgi?id=17907

Re: Getting a safe path for a temporary file

2017-10-22 Thread Shriramana Sharma via Digitalmars-d-learn
On Saturday, 17 January 2015 at 17:16:41 UTC, Tobias Pankrath wrote: You're looking for core.sys.posix.stdlib : mkstemp. I think that should be used by std.stdio.File as well, care to create an enhancement request in bugzilla? Though this thread is old, I ran into the issue when wanting to c

Re: Getting a safe path for a temporary file

2017-10-22 Thread Shriramana Sharma via Digitalmars-d-learn
On Sunday, 22 October 2017 at 15:21:37 UTC, Shriramana Sharma wrote: For my program right now I'm using a souped-up version using a static array: char[20] name = "/tmp/XX"; Hmm I was wondering if I needed it to be static, and verily, substituting: char[] name = "/tmp/XX".dup;

Why is length being tested on an int?

2017-10-27 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. I want a function to be able to take any arguments like write() and print them out but quoting string arguments of length more than 1. So I write the following quote: import std.stdio; string myFunc(string arg) { return '\"' ~ arg ~ '\"'; } void myWrite(T ...)(T args) { foreach (arg;

Re: Getting a safe path for a temporary file

2017-10-27 Thread Shriramana Sharma via Digitalmars-d-learn
On Wednesday, 25 October 2017 at 00:35:29 UTC, Ali Çehreli wrote: > char[] name = "/tmp/XX".dup; remain valid. The actual issue is the missing '\0'. So, consider toStringz in this case: https://dlang.org/library/std/string/to_stringz.html Thanks for your reply, but can you clarify exa

Why isn't global operator overloading allowed in D?

2015-10-14 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. I just came upon a need in my program to make binary arithmetic operators valid between two real[] in my programs, and thought of writing a global opOpAssign, but then looked through the documentation, found nothing on operator overloading allowed at the global level (even within a single

Re: Why isn't global operator overloading allowed in D?

2015-10-14 Thread Shriramana Sharma via Digitalmars-d-learn
Shriramana Sharma wrote: > Hello. I just came upon a need in my program to make binary arithmetic > operators valid between two real[] in my programs, and thought of writing > a global opOpAssign, but then looked through the documentation, found > nothing on operator overloading allowed at the glo

Re: Why isn't global operator overloading allowed in D?

2015-10-15 Thread Shriramana Sharma via Digitalmars-d-learn
John Colvin wrote: > On Wednesday, 14 October 2015 at 15:02:02 UTC, Shriramana Sharma > wrote: > What binary arithmetic operators do you need that real[] doesn't > already support? OMG silly me! I can already do a[] /= b[]... D is great! :-D Thanks a lot!

Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. I still haven't wrapped my mind around the const/immutable thing yet and am still stuck in C/C++ mode. :-( A function that takes mutable arguments cannot be called with immutable input at the call site since it does not promise to *not* mutate the input. That's of course clear. Why can'

Why does File.byLine() return char[] and not string

2015-10-16 Thread Shriramana Sharma via Digitalmars-d-learn
Is there a particular reason that File.byLine() returns char[] and not string i.e. immutable(char)[]? Is it just to avoid being overly restrictive? It seems that having to .idup it is inefficient... -- Shriramana Sharma, Penguin #395953

Re: Why does File.byLine() return char[] and not string

2015-10-16 Thread Shriramana Sharma via Digitalmars-d-learn
Thanks people, for the replies. That's very clear now. -- Shriramana Sharma, Penguin #395953

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Shriramana Sharma via Digitalmars-d-learn
Ali Çehreli wrote: http://ddili.org/ders/d.en/const_and_immutable.html#ix_const_and_immutable.parameter, %20const%20vs.%20immutable Hi Ali – I take this chance to personally thank you sincerely for your book which provides much-needed hand-holding in my baby D-steps. I did read that chapter alr

Error in trying to use an inout(char)[] with a regex

2015-10-16 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. Please see the following code: import std.range; string textAttr(T)(T spec) if (is(ElementType!(T): const(char)[])) { return ""; // made dummy for illustration; actually this runs a foreach loop on the // individual items in spec, analyses them and passes them to // appropr

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Shriramana Sharma via Digitalmars-d-learn
Ali Çehreli wrote: > Actually, others gave the answer to that question, which was apparently > not very clear. :) Yes it was clear and I did understand it: and I posted a reply thanking the others too, but for some reason it was still sitting in my outbox... -- Shriramana Sharma, Penguin #3959

Re: Error in trying to use an inout(char)[] with a regex

2015-10-16 Thread Shriramana Sharma via Digitalmars-d-learn
Ali Çehreli wrote: > Meanwhile, can you try the following template which works at least for > the reduced code: > > import std.range; > > auto foo(S)(S text) > if (isSomeString!S) { > import std.regex; > static auto inlineRE = ctRegex!`\$\(ta (.*?)\)`; > return text.replaceAll!(m => textAttr(m[1

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Shriramana Sharma via Digitalmars-d-learn
Thanks all, for your kind explanations. Would then constString (for const(char)[]) and inoutString (for inout(char) []) be useful aliases if included in the runtime? -- Shriramana Sharma, Penguin #395953

Compiling a .d file both as library and executable

2015-10-17 Thread Shriramana Sharma via Digitalmars-d-learn
In Python there is: if __name__ == "__main__": to allow the same source file to be treated as both an importable library and as an executable script. In D is there any such mechanism to make a main() compiled selectively, i.e. by some compile-time flag or such? -- Shriramana Sharma, Penguin #

StopWatch

2015-10-20 Thread Shriramana Sharma via Digitalmars-d-learn
http://dlang.org/phobos/std_datetime.html#StopWatch shows the use of TickDuration to measure the time elapsed, but http://dlang.org/phobos/core_time.html#TickDuration says TickDuration is due to be deprecated and MonoTime should be used. It is possible to measure the duration between two points

Overloading an imported function

2015-10-21 Thread Shriramana Sharma via Digitalmars-d-learn
import std.math; real round(real val, int prec) { real pow = 10 ^^ prec; return round(val * pow) / pow; } Trying to compile this I get: foo.d(5): Error: function foo.round (real val, int prec) is not callable using argument types (real) When I've imported std.math which contains round(r

can't zip a char[5], string[5], real[5]

2015-10-21 Thread Shriramana Sharma via Digitalmars-d-learn
import std.stdio, std.range; void mywrite(char [5] chars, real[5] vals) { static string [5] fmts = ["%9.4f, ", "%9.4f; ", "%3d, ", "%3d, ", "%3d\n"]; foreach (e; zip(chars, fmts, vals)) write(e[0], " = ", e[1].format(e[2])); } Compiling gives: zip_string.d(5): Error: template std.range.

toString"z"?

2015-10-21 Thread Shriramana Sharma via Digitalmars-d-learn
std.string.toStringz – why the strange name with z instead of toString0 or toCString? -- Shriramana Sharma, Penguin #395953

Re: Overloading an imported function

2015-10-21 Thread Shriramana Sharma via Digitalmars-d-learn
Kagamin wrote: > http://dlang.org/hijack.html Thanks people, but even as per the rules: 1. Perform overload resolution independently on each overload set 2. If there is no match in any overload set, then error 3. If there is a match in exactly one overload set, then go with that 4. If there is a

Re: Overloading an imported function

2015-10-21 Thread Shriramana Sharma via Digitalmars-d-learn
anonymous wrote: > Huh. I can't find any specification on this, but apparently the local > overload set shadows any imported overload sets completely. Should I file a bug on this then? -- Shriramana Sharma, Penguin #395953

D serialization temporary fixup?

2015-10-22 Thread Shriramana Sharma via Digitalmars-d-learn
I wanted a D equivalent to: http://doc.qt.io/qt-5/qdatastream.html https://docs.python.org/3/library/pickle.html and saw that one is under construction: http://wiki.dlang.org/Review/std.serialization But till it's finalized, I'd just like to have a quick but reliable way to store real and int da

Re: D serialization temporary fixup?

2015-10-23 Thread Shriramana Sharma via Digitalmars-d-learn
Shriramana Sharma wrote: > I'd just like to have a quick but reliable way to > store real and int data types into a binary data file and read therefrom. > Is there such a solution? Wow thank you people! Nice to know I can do rawWrite and also have other options. BTW is there a reason that eith

`clear`ing a dynamic array

2015-10-24 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. I had first expected that dynamic arrays (slices) would provide a `.clear()` method but they don't seem to. Obviously I can always effectively clear an array by assigning an empty array to it, but this has unwanted consequences that `[]` actually seems to allocate a new dynamic array and

Re: `clear`ing a dynamic array

2015-10-24 Thread Shriramana Sharma via Digitalmars-d-learn
rsw0x wrote: > use std.container.array Thanks all for all the recommendations. When would one use std.array.appender with a built-in array vs std.container.array.Array? What are the pros and cons on either side? -- Shriramana Sharma, Penguin #395953

Re: `clear`ing a dynamic array

2015-10-25 Thread Shriramana Sharma via Digitalmars-d-learn
Thanks all, for your replies. Jonathan M Davis via Digitalmars-d-learn wrote: > If you want a container rather than a dynamic array - especially if you're > looking for full reference semantics - then use std.container.array.Array. Hmmm, pardon me but while I'm sure I don't specifically require

Re: `clear`ing a dynamic array

2015-10-25 Thread Shriramana Sharma via Digitalmars-d-learn
Jonathan M Davis via Digitalmars-d-learn wrote: > Appender really isn't intended to be used as a > container - just as a way to make appending more efficient or to have an > output range which is an array I get the part about Appender helping to make an output range of a regular array, but I'm n

Re: `clear`ing a dynamic array

2015-10-25 Thread Shriramana Sharma via Digitalmars-d-learn
anonymous wrote: >> I presume this means >> http://dlang.org/phobos/std_array.html#.Appender.reserve but >> how `append` is considered an operator is beyond me. > > That sentence doesn't refer to std.array.Appender. `reserve` > means . The append > op

How to get DMD to stop littering my source dir with .o files?

2015-10-26 Thread Shriramana Sharma via Digitalmars-d-learn
The subject line says it all. Every time I compile a D file to an executable I get an unwanted .o file and have to manually clean up things. I'm using DMD 2.0.68.2. -of doesn't help, and my God, it doesn't even allow a space or equal sign between itself and the desired name of the output file m

Dhee - tiny app to learn/try out D

2015-11-10 Thread Shriramana Sharma via Digitalmars-d-learn
I wrote up a small PyQt app to help me do quick coding of snippets in D to help me learn how D works. https://github.com/jamadagni/dhee I wish there was (a working) QtD so I wouldn't need to use Python, but well... Looked into GtkD a bit, but somehow Gtk never clicked with me... P.S.: I only c

why --shebang for rdmd?

2015-11-20 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. The following code works fine for me: #! /usr/bin/env rdmd import std.stdio; void main() { writeln(2); } So what is the use of the --shebang option of rdmd? http://dlang.org/rdmd.html does not shed much light on this. Thanks. -- Shriramana Sharma, Penguin #395953

`finally` is redundant?

2015-11-20 Thread Shriramana Sharma via Digitalmars-d-learn
The page http://dlang.org/exception-safe.html says: "It's try-finally that becomes redundant." IIUC this is because we have scope(exit). Does this mean that `finally` should eventually be removed from the language? -- Shriramana Sharma, Penguin #395953

D equivalent of Python's try..else

2015-11-20 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. In Python one has the syntax try..except..else.. where code in the else clause will only be executed if an exception does not occur. (Ref: http://stackoverflow.com/a/22579805/1503120) In D, is there such an idiomatic/canonical construct? The D try statement only seems to support finally

Re: D equivalent of Python's try..else

2015-11-20 Thread Shriramana Sharma via Digitalmars-d-learn
rsw0x wrote: > scope(failure) can be used to run code when an exception is > thrown inside the scope, and scope(success) only triggers if the > scope exited successfully > > http://ddili.org/ders/d.en/scope.html Thanks but I know that and it executes only at the point of scope exit. But I want

Re: D equivalent of Python's try..else

2015-11-20 Thread Shriramana Sharma via Digitalmars-d-learn
Shriramana Sharma wrote: > In Python one has the syntax try..except..else.. where code in the > else clause will only be executed if an exception does not occur. (Ref: > http://stackoverflow.com/a/22579805/1503120) Official Python documentation: https://docs.python.org/3/reference/compound_stmts

Re: D equivalent of Python's try..else

2015-11-21 Thread Shriramana Sharma via Digitalmars-d-learn
Russel Winder via Digitalmars-d-learn wrote: > else on for and while, whilst technically redundant as well, does > occasionally make for a nicer read, for very analogous reasons. It can > generally avoid the need for extra booleans and other state variables. Hmm – I forgot Python has `else` for `

%s not producing string representation of enum?

2015-12-10 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. I'm using DMD 2.069.2. As per http://ddili.org/ders/d.en/enum.html the following code is supposed to output the *names* of the suits: import std.stdio; void main() { enum Suit { spades, hearts, diamonds, clubs } foreach (suit; Suit.min .. Suit.max + 1) { writefln("%s", suit); } }

Re: %s not producing string representation of enum?

2015-12-10 Thread Shriramana Sharma via Digitalmars-d-learn
Basile B. wrote: > You should rather use std.traits.EnumMembers to iterate the > members of an enum: Wow this is great! I was thinking that enum.max + 1 is not really befitting D's elegant approach to programming. Ali should really update that section of his book to use EnumMembers. This will

Using std.math: FloatingPointControl.enableExceptions

2015-12-10 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. I'm trying to figure out how to use FloatingPointControl.enableExceptions. Upon enabling severeExceptions I would expect the division by zero to be signaled, but neither do I get a SIGFPE nor does ieeeFlags show the exception having been signaled. What am I doing wrong? import std.stdio

Re: Using std.math: FloatingPointControl.enableExceptions

2015-12-11 Thread Shriramana Sharma via Digitalmars-d-learn
rumbu wrote: > Constant folding: a is evaluated at compile time to + infinity. Hmm... I guess the compiler figures that if someone is hardcoding that expression then they don't want to see an exception. Thanks for the explanation. -- Shriramana Sharma, Penguin #395953

Re: %s not producing string representation of enum?

2015-12-11 Thread Shriramana Sharma via Digitalmars-d-learn
Ali Çehreli wrote: > http://ddili.org/ders/d.en/enum.html#ix_enum.EnumMembers,%20std.traits Ali that's great! Thank you! -- Shriramana Sharma, Penguin #395953

AliasSeq can contain template identifier too?

2015-12-12 Thread Shriramana Sharma via Digitalmars-d-learn
https://github.com/D-Programming-Language/phobos/blob/master/std/meta.d#L790 Looks like an AliasSeq can contain a template identifier too. So should I understand that AliasSeq in general can refer to any identifier and any value? Hitherto I thought it was any *type* and any value... -- Shriram

isTemplate and isValue?

2015-12-12 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. Re my posting just now re AliasSeq being able to contain a template identifier too, I wonder whether it is possible to have a std.traits template to identify whether something is a template or not? In connection with this, while is() is there to determine whether something is a type or n

Inferring an integer literal as ubyte

2015-12-14 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. I was trying to do something like this: ubyte code = to!ubyte(spec, 6) + 16; and got an error saying: cannot implicitly convert expression (cast(int)to(spec, 6) + 16) of type int to ubyte Looking at http://dlang.org/spec/lex.html#IntegerLiteral, sure enough 16 is specified to be inferr

Re: Inferring an integer literal as ubyte

2015-12-14 Thread Shriramana Sharma via Digitalmars-d-learn
Adam D. Ruppe wrote: > but yours won't because to!ubyte(spec, 6) might just be > 240. Thanks for that explanation. That's clear now. -- Shriramana Sharma, Penguin #395953

Why should file names intended for executables be valid identifiers?

2015-12-14 Thread Shriramana Sharma via Digitalmars-d-learn
I understand that module names need to be valid identifiers in that other modules would need to import them. But when a file is intended to be just an executable, why is it mandatory to give it a module declaration with a valid identifier? For instance, hyphens are often used as part of execut

Testing if a file is connected to a terminal

2015-12-16 Thread Shriramana Sharma via Digitalmars-d-learn
Is there a canonical way to test in D whether stdout/stderr (or in general, a std.stdio.File) refers to a terminal or not? https://www.google.co.in/search?q=terminal&sitesearch=dlang.org/phobos turns out nothing. I knew of C's (or rather POSIX's) isatty, and after some digging I found isatty

No documentation for core.sys?

2015-12-16 Thread Shriramana Sharma via Digitalmars-d-learn
In my recent search for D's equivalent of isatty, I found out that there is a core.sys.posix module only by rgrepping under /usr/include/dmd. Why isn't there a documentation page http://dlang.org/phobos/core_sys.html whereas lots of other core.* modules are documented? -- Shriramana Sharma, Pe

Re: No documentation for core.sys?

2015-12-16 Thread Shriramana Sharma via Digitalmars-d-learn
Jakob Ovrum wrote: > As with core.stdc, refer to the > documentation for the equivalent C header. I only know of even core.stdc's existence since I've been poking into the Phobos sources. At least the Phobos documentation should mention that such modules exist. I note that in the proposed libr

Re: Why should file names intended for executables be valid identifiers?

2015-12-16 Thread Shriramana Sharma via Digitalmars-d-learn
Adam D. Ruppe wrote: > It still has a module name that can be used in reflection, must > be used in name disambiguation (at the linker level if nothing > else, any functions are mangled with the module name so they > don't conflict with C functions with the same name), and other > things. Sorry

exit(1)?

2015-12-16 Thread Shriramana Sharma via Digitalmars-d-learn
http://dlang.org/phobos/std_getopt.html has a line in an example saying exit(1); Surely this works only if core.stdc.stdlib is imported? Should the example be modified to show the import? And is exit() the canonical way to exit the current process even in D? -- Shriramana Sharma, Penguin #395

Re: Testing if a file is connected to a terminal

2015-12-16 Thread Shriramana Sharma via Digitalmars-d-learn
Jakob Ovrum wrote: > Where's the reference documentation? There's a README: http://code.dlang.org/packages/consoled, and the source does seem to have DDoc comments... -- Shriramana Sharma, Penguin #395953

Re: exit(1)?

2015-12-17 Thread Shriramana Sharma via Digitalmars-d-learn
Jakob Ovrum wrote: > The example should be restructured to `return 1;` > from `main`. https://github.com/D-Programming-Language/phobos/pull/3875 -- Shriramana Sharma, Penguin #395953

function without "this" cannot be const?

2015-12-20 Thread Shriramana Sharma via Digitalmars-d-learn
I'm trying to interface to a C function: extern(C) const char * textAttrN(const char * specString, size_t n); and getting the error: Error: function .textAttrN without 'this' cannot be const Please advise as to what I'm doing wrong?! :-( -- Shriramana Sharma, Penguin #395953

Re: function without "this" cannot be const?

2015-12-20 Thread Shriramana Sharma via Digitalmars-d-learn
Basile B. wrote: > without the parens, 'const' means that the function doesn't > mutate the state of the object or of the struct it's declared in. > So it's meaningless for a global function. Thank you people. -- Shriramana Sharma, Penguin #395953

C string to D without memory allocation?

2015-12-20 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. I have the following code: import std.stdio, std.conv; extern(C) const(char) * textAttrN(const (char) * specString, size_t n); string textAttr(const(char)[] specString) { const(char) * ptr = textAttrN(specString.ptr, specString.length); writeln(ptr); return to!string(ptr); } voi

Re: C string to D without memory allocation?

2015-12-20 Thread Shriramana Sharma via Digitalmars-d-learn
Rikki Cattermole wrote: > string myCString = cast(string)ptr[0 .. strLen]; Thanks but does this require that one doesn't attempt to append to the returned string using ~= or such? In which case it is not safe, right? -- Shriramana Sharma, Penguin #395953

Re: C string to D without memory allocation?

2015-12-20 Thread Shriramana Sharma via Digitalmars-d-learn
Jakob Ovrum wrote: > Use std.string.fromStringz. to!string assumes that pointers to > characters are null-terminated strings which is not safe or > general I suppose what you mean is, the onus of guaranteeing that const(char)* refers to a null-terminated string is upon the person calling the to

Deimos recommendation still official?

2015-12-21 Thread Shriramana Sharma via Digitalmars-d-learn
http://dlang.org/spec/interfaceToC.html refers one to Deimos (https://github.com/D-Programming-Deimos) to look for existing bindings to C libraries. Is this recommendation still valid? I ask because less than one fourth of the repos there seem to have been active in this year 2015. Or is it jus

Template specialization using traits?

2015-12-21 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. I want to define a template specialization using traits: import std.stdio, std.traits; void func(T)(T t) { writeln(1); } void func(T)(T t) if(isIntegral!T) { writeln(2); } void main() { func(1); } But I'm getting an error saying that the called function matches both. If it were a sing

Re: C string to D without memory allocation?

2015-12-21 Thread Shriramana Sharma via Digitalmars-d-learn
Jonathan M Davis via Digitalmars-d-learn wrote: > If it isn't, all that means is that the > array's capacity will be 0, so it's going to have to reallocate So it's safe to return a string produced by fromStringz without having to worry that the user would append to it? Then why is it marked @sy

Re: Template specialization using traits?

2015-12-21 Thread Shriramana Sharma via Digitalmars-d-learn
Thanks all for your replies. One question: Jonathan M Davis wrote: > Alternatively, you can use static if, though you're only dealing > with one template in that case. e.g. But if we wanted to deprecate one of the alternatives, then we necessary need to declare two templates with the same name a

What other than a pointer can be converted implicitly to const(char)*?

2015-12-21 Thread Shriramana Sharma via Digitalmars-d-learn
https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L878 The `static if` condition here says if something is a pointer and if it is implicitly convertible to const(char)*. The isPointer! part seems superfluous. Is there something that is not a pointer yet implicitly converti

Is a type not a symbol?

2015-12-22 Thread Shriramana Sharma via Digitalmars-d-learn
http://dlang.org/spec/template.html#TemplateTupleParameter says that an AliasSeq (wording needs to be updated) "is a sequence of any mix of types, expressions or symbols." Is a type not a symbol? I mean, alias can refer to both, no? -- Shriramana Sharma, Penguin #395953

: in template specialization vs constraint

2015-12-22 Thread Shriramana Sharma via Digitalmars-d-learn
import std.stdio; void func(T)(T v) { writeln(1); } void func(T: int)(T v) { writeln(2); } void func(T)(T v) if (is(T: int)) { writeln(3); } void main() { func(100); ubyte s = 200; func(s); } The above code prints 2 twice. A fwe questions: 1) At func(100) why isn't the compiler compla

CTFE with C functions not possible?

2015-12-31 Thread Shriramana Sharma via Digitalmars-d-learn
Using DMD 2.0.69.2, the following code: extern (C) double sqrt(double x); enum q = sqrt(4.0); gives the error: Error: sqrt cannot be interpreted at compile time, because it has no available source code But if I do: import std.math; enum q = sqrt(4.0); There is no problem. So two questions:

Re: CTFE with C functions not possible?

2015-12-31 Thread Shriramana Sharma via Digitalmars-d-learn
Rikki Cattermole wrote: > It will make it very hard to split std.math up. I have no desire to split std.math up. :-) What I desire to do is be able to call a C library from a D template like octal to compute a string at compile time. Is this possible or not? -- Shriramana Sharma, Penguin #39

Re: CTFE with C functions not possible?

2015-12-31 Thread Shriramana Sharma via Digitalmars-d-learn
Rikki Cattermole wrote: >> Is this possible or not? > > No, source is not available. Why, is it because the D compiler is already linked to the C library (and hence knows where the functions are located and such), but not to my library? I mean, I even gave -L-lmylib and all that, but of course

Re: CTFE with C functions not possible?

2015-12-31 Thread Shriramana Sharma via Digitalmars-d-learn
Shriramana Sharma wrote: > What I desire to do is be able to call a C library from a D template like > octal to compute a string at compile time. To be more explicit, I wrote a library in C since it's much leaner size-wise than the D code (although admittedly much *much* more tedious to write e

Re: CTFE with C functions not possible?

2015-12-31 Thread Shriramana Sharma via Digitalmars-d-learn
Rikki Cattermole wrote: > You misunderstand, its hardcoded into the CTFE evaluator. That is what > an intrinsic is. What do you mean hardcoded into the CTFE evaluator? Surely you aren't suggesting that the D compiler contains its own implementation of the functions already implemented in libc?

@property not available for classes?

2016-01-01 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. I'm trying the following code: import std.stdio; class TimeSpan { immutable double start, end; @property double length() { return end - start; } } void main() { auto p = TimeSpan(1, 2); writeln(p.length); } ...and I'm getting the error: Error: no property 'opCall' for type

Re: CTFE with C functions not possible?

2016-01-01 Thread Shriramana Sharma via Digitalmars-d-learn
Rikki Cattermole wrote: > Either port it to D and extern(C) it so it is accesible from other > languages or not have CTFE support. I already wrote it in D, then I ported to C with much effort. The option to extern(C)-ing it didn't occur to me. :-( Also, the D version is really much too bulky.

Why can't a Regex object be immutable?

2016-01-01 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. With this code: import std.stdio, std.regex; void main() { immutable numbers = regex(r"\d+"); foreach (match; "a1b2c3d4e5".matchAll(numbers)) writeln(match[0]); } compiling gives the error: (4): Error: cannot implicitly convert expression (regex("\\d+", "")) of type Regex

Re: Why can't a Regex object be immutable?

2016-01-01 Thread Shriramana Sharma via Digitalmars-d-learn
Shriramana Sharma wrote: > Why is it impossible for a Regex object to be > `immutable`? I find that I can't declare it as `const` either... This is most curious! -- Shriramana Sharma, Penguin #395953

Re: @property not available for classes?

2016-01-01 Thread Shriramana Sharma via Digitalmars-d-learn
John wrote: > It's nothing to do with the @property attribute. So you need to > define a constructor. Also, use "new" when creating instances. Thanks Simon and John. First actual usage of D classes and mistaken assumption that C++ syntax is valid. :-) -- Shriramana Sharma, Penguin #395953

Why isn't field-wise constructor automatic for structs and not classes?

2016-01-01 Thread Shriramana Sharma via Digitalmars-d-learn
If I have: struct TimeSpan { double start, end; } Then both the following automatically work: auto s = TimeSpan(); auto t = TimeSpan(1, 2); But if I make it a class (I need to) then I have to explicitly define a field-wise constructor else only a constructor with no args is automatically defi

Re: Why can't a Regex object be immutable?

2016-01-01 Thread Shriramana Sharma via Digitalmars-d-learn
cym13 wrote: > I think it's because regex() only compiles the regex at runtime > so it needs to be modified later ; Aw come on. The immutability of the variable is *after* it has been created at runtime. > > you'll find that using > ctRegex() instead will allow you to declare it immutable for

Re: CTFE with C functions not possible?

2016-01-01 Thread Shriramana Sharma via Digitalmars-d-learn
Rikki Cattermole wrote: > How exactly is a D version more bulky then C? > After all everything C can do, D can do with a very similar syntax. Source-code wise D is much leaner than C, obviously, but object-code wise it is *huge* even with dynamically linking Phobos: The binary size of compiling

Re: Why can't a Regex object be immutable?

2016-01-01 Thread Shriramana Sharma via Digitalmars-d-learn
cym13 wrote: > Is it that you > can't make an immutable regex()? In that case it is a > runtime-related issue and those variables just have to be > mutable. Or is it that you want to be able to use an immutable or > const regex (be it from regex() or ctRegex!()) with matchAll()? > In the latter ca

immutable promise broken in unions?

2016-01-02 Thread Shriramana Sharma via Digitalmars-d-learn
import std.stdio; union EarthLocation { struct { immutable double lon, lat, alt; } double[3] data; } void main() { EarthLocation d = {data: [4, 5, 6]}; writeln(d.data); d.data = [1, 2, 3]; writeln(d.data); } I get the output: [4, 5, 6] [1, 2, 3] I thought the promise of `

Re: immutable promise broken in unions?

2016-01-02 Thread Shriramana Sharma via Digitalmars-d-learn
John Colvin wrote: > Casting away immutable can sometimes be necessary (e.g. when > talking to other languages), so I'm not sure it should be > disallowed, but it'd be great if it was somehow easier to catch > these bugs. Yes it was in the context of talking to C that I needed to make such a uni

Re: Why isn't field-wise constructor automatic for structs and not classes?

2016-01-02 Thread Shriramana Sharma via Digitalmars-d-learn
John Colvin wrote: > Strictly speaking you aren't calling a constructor there, you're > writing a struct literal. Why do you say I'm not calling a constructor? And that still doesn't answer the question of why can't we have an automatic field-wise constructor for classes... -- Shriramana Shar

mixed-in ctor not on par with explicit one?

2016-01-12 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. Compiling the following code: mixin template intCtor() { this(int i) {} } struct Test { mixin intCtor; this(string s) {} } void main() { auto a = Test("hello"); auto b = Test(1); } ...gives the error: (6): Error: constructor .Test.this (string s) is not callable using argument ty

Re: mixed-in ctor not on par with explicit one?

2016-01-13 Thread Shriramana Sharma via Digitalmars-d-learn
Hello and thanks for your reply. Jacob Carlborg wrote: > [1] http://dlang.org/spec/template-mixin.html - search for "Alias > declarations can be used to overload together functions declared in > different mixins" But I'm not able to do that with `this`: mixin template myCtors() { this(int i

Re: mixed-in ctor not on par with explicit one?

2016-01-13 Thread Shriramana Sharma via Digitalmars-d-learn
Jacob Carlborg wrote: > Looks like a limitation in the language. Apparently already reported as well: https://issues.dlang.org/show_bug.cgi?id=11500 -- Shriramana Sharma, Penguin #395953

Compiler complaining about ~ used on static array in @nogc fn

2016-01-13 Thread Shriramana Sharma via Digitalmars-d-learn
Referring to: https://auto-tester.puremagic.com/show-run.ghtml?projectid=1&runid=1915054&isPull=true, the lines in question are phobos/std/utf.d (66, 67): UnsignedStringBuf buf = void; msg ~= " (at index " ~ unsignedToTempString(index, buf, 10) ~ ")"; rgrepping through the drun

`static` symbol needs to be `immutable` for compile-time access?

2016-01-22 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. This is a minimal abstraction of a part of my program: int func(string s) { static int [] i = [5, 6, 7]; return i[2]; } template temp(string s) { enum temp = func(s); } void main() { static assert(temp!"str" == 7); } With the above code I get: (4): Error: static variable i cannot

Re: `static` symbol needs to be `immutable` for compile-time access?

2016-01-22 Thread Shriramana Sharma via Digitalmars-d-learn
Thanks to all who replied. anonymous wrote: >> Do all values which need to >> be readable at compile time need to be declared `immutable`? > > Yes, `static immutable` or `enum`. It would seem that in the case of arrays, the former is preferable to the latter, as per the para above this header:

Unable to instantiate template with same name as function

2016-03-02 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. I have a function I want to make CTFE-able as a template. string ta(string s) { return s ~ "1"; } template ta(string s) { enum ta = ta(s); } void main() { string s = ta!"s"; } Compiling the above I get the errors: (2): Error: forward reference of variable ta (3): Error: template instance

Re: Unable to instantiate template with same name as function

2016-03-03 Thread Shriramana Sharma via Digitalmars-d-learn
Hello people and thanks for your replies. Jonathan M Davis via Digitalmars-d-learn wrote: > You can't overload a function and an eponymous template like that. They > need to have distinct names. Why is it not possible for the overload to happen? After all, the compiler should be able to identi

Re: Unable to instantiate template with same name as function

2016-03-04 Thread Shriramana Sharma via Digitalmars-d-learn
cym13 wrote: > Note that parentheses are optional when no argument is provided. Yes I know that but the point is I expected the compiler to identify ta!"string" to refer to a different symbol than ta("string") where the one is obviously a template and the other is obviously a function call. The

Re: Unable to instantiate template with same name as function

2016-03-04 Thread Shriramana Sharma via Digitalmars-d-learn
ag0aep6g wrote: > On 03.03.2016 07:12, Shriramana Sharma wrote: >> string ta(string s) { return s ~ "1"; } >> template ta(string s) { enum ta = ta(s); } > > In `ta(s)` here, `ta` is the enum itself again. It's similar to `int x = > x;`. Can't do that, of course. > > Add a leading dot to refer to

Re: Unable to instantiate template with same name as function

2016-03-04 Thread Shriramana Sharma via Digitalmars-d-learn
@AliCehreli: you may consider including Jonathan's trick in your book in the para above this heading: http://ddili.org/ders/d.en/const_and_immutable.html#ix_const_and_immutable.variable, %20immutable Jonathan M Davis via Digitalmars-d-learn wrote: > yes, having > > enum s = ta("s)"; > > and u

Re: Using in as a parameter qualifier

2014-10-22 Thread Shriramana Sharma via Digitalmars-d-learn
Hello people. I'm once more looking at D since I participated here a bit last year. Since I'm still not 100% sure about committing myself to using D i.o. C++ for my work, I'd really like to resurrect this thread to clear my lingering doubts (the full thread is at http://forum.dlang.org/post/mailman

  1   2   >