D - template instantiation (suggestion)
- Heretic (11/11) Dec 01 2003 yo again. i`ve finally read the D language specification, lol. the more ...
-
Andy Friesen
(9/20)
Dec 01 2003
The trouble with Name
is (as I understand it) that it requires - Ben Hinkle (13/33) Dec 01 2003 i read
- Sean L. Palmer (4/7) Dec 01 2003 C# 2.0 has managed to address that little problem. It's not unsolvable.
- Patrick Down (9/28) Dec 01 2003 If we are going to keep explicit instantiation I think it would be good
- Charles Sanders (14/46) Dec 01 2003 Cool idea! $ feels a little to much like 'interpolation' to me, but thi...
- davepermen (12/69) Dec 01 2003 i don't want to keep it explicit with a keyword or token.
- Sean L. Palmer (14/25) Dec 01 2003 Actually I've been wishing lately for template namespaces in C++. ;)
- Hauke Duden (59/71) Dec 01 2003 I agree completely, with all your points!
- Georg Wrede (13/28) Dec 01 2003 I think this would be a great step towards real genericity
- Antti =?iso-8859-1?Q?Syk=E4ri?= (73/136) Dec 03 2003 Maybe replacing <> with something else is not the way to go at all... at
- Antti =?iso-8859-1?Q?Syk=E4ri?= (3/6) Dec 01 2003 To me, $ feels for some reason like perl...
- Charles Sanders (6/12) Dec 01 2003 Yea, and PHP, where print "$foo"; prints the value of the variable foo (
- Mark J. Brudnak (9/24) Dec 01 2003 IMO '$' should be reserved to denote the end of an array in the slice
- Ant (3/9) Dec 01 2003 IMO you opinion is realy cool.
- jonth ccpgames.com (29/55) Dec 03 2003 IMO why not use the same syntax as Python?
- davepermen (5/11) Dec 01 2003 why? nothing is shorter than $. and i really mean NOTHING! :D
- Mark Brudnak (5/19) Dec 01 2003 What about the case:
- Ilya Minkov (6/9) Dec 03 2003 array[-2..]
- Jon Thoroddsen (24/33) Dec 03 2003 I much prefer ":" as it's more concise, and i'm allergic to Pascal, I th...
- Ilya Minkov (17/25) Dec 04 2003 These ones should all work with my proposal, as - would be a unary opera...
- Sean L. Palmer (26/51) Dec 04 2003 think.
- Mark J. Brudnak (30/53) Dec 05 2003 think.
- Hauke Duden (25/46) Dec 05 2003 No, not if it works as in Python. In Python, -1 refers to the last
- Sean L. Palmer (7/17) Dec 05 2003 $-1,
- Sean L. Palmer (4/10) Dec 01 2003 Sounds good.
- Antti =?iso-8859-1?Q?Syk=E4ri?= (21/38) Dec 03 2003 Tangenting the issue, I just recently realized (maybe it was
- brad beveridge (19/19) Dec 01 2003 Maybe slightly OT, but is there a reason that D doesn't support
- Sean L. Palmer (14/46) Dec 01 2003 "$" is better than "instance". Instance is just too wordy.
- Felix (4/36) Dec 01 2003 Why cannot use simply instancetype(newinstancetype) and the compiler wil...
- J Anderson (9/13) Dec 02 2003 This is my feeling on the matter to.
- Felix (2/16) Dec 02 2003
- J Anderson (5/34) Dec 02 2003 I do think Walter would have used the above syntax, if there wasn't some...
- Charles Sanders (7/47) Dec 03 2003 But at what cost, an cumbersome language ?
- Patrick Down (5/8) Dec 02 2003 I agree that would be best. You will have to ask Walter but I belive
- Walter (3/11) Dec 19 2003 You're right. It would not parse in a context-free manner.
- Achilleas Margaritis (35/46) Dec 01 2003 This is how templates should be declared:
- Georg Wrede (19/33) Dec 01 2003 I'd have that as
- Heretic (24/24) Dec 01 2003 quote
- Andy Friesen (5/9) Dec 02 2003 I'm all for making a few tradeoffs for the sake of compiler simplicity,
- Berin Loritsch (13/30) Dec 02 2003 Hmm. I would expect to do:
- Walter (4/6) Dec 19 2003 import std.regexp;
yo again. i`ve finally read the D language specification, lol. the more i read bout d, the better it seems. however there`s one thing that annoys me. templates have to be instantiated with the "instance Name(ARG)" expression... i dont quite like it, wouldnt it be better to use Name<ARG> ? it is still context independent and much faster to type, more comfortable and stuff. in c++ i have a function alias_cast<T>(char*) that works as a object reference. i add aliases to objects, but not aliases in teh D way. i name objects with character strings and i`m able to access them this way. in D, i`d have to write instance alias_cast(T).f(char*) whis is quite tedious to type and is less clear :/ maybe i`m missing something, pls correct me then, but consider my suggestion of using the triangular braces for template instantiation...
Dec 01 2003
Heretic wrote:yo again. i`ve finally read the D language specification, lol. the more i read bout d, the better it seems. however there`s one thing that annoys me. templates have to be instantiated with the "instance Name(ARG)" expression... i dont quite like it, wouldnt it be better to use Name<ARG> ? it is still context independent and much faster to type, more comfortable and stuff. in c++ i have a function alias_cast<T>(char*) that works as a object reference. i add aliases to objects, but not aliases in teh D way. i name objects with character strings and i`m able to access them this way. in D, i`d have to write instance alias_cast(T).f(char*) whis is quite tedious to type and is less clear :/ maybe i`m missing something, pls correct me then, but consider my suggestion of using the triangular braces for template instantiation...The trouble with Name<ARG> is (as I understand it) that it requires semantic analysis to figure out whether < and > are being used as angle brackets, or as greater-than/less-than in an expression. Plain-old parenths might cut it, but that makes what's going on less obvious. Braces already have meaning as well. I agree that template instantiation should be much less verbose; it's just a matter of working out a good syntax for it. -- andy
Dec 01 2003
"Andy Friesen" <andy ikagames.com> wrote in message news:bqfm1p$14ks$1 digitaldaemon.com...Heretic wrote:i readyo again. i`ve finally read the D language specification, lol. the moreexpression... ibout d, the better it seems. however there`s one thing that annoys me. templates have to be instantiated with the "instance Name(ARG)"contextdont quite like it, wouldnt it be better to use Name<ARG> ? it is stilli have aindependent and much faster to type, more comfortable and stuff. in c++aliases tofunction alias_cast<T>(char*) that works as a object reference. i addstrings andobjects, but not aliases in teh D way. i name objects with character:/i`m able to access them this way. in D, i`d have to write instance alias_cast(T).f(char*) whis is quite tedious to type and is less clearsuggestion ofmaybe i`m missing something, pls correct me then, but consider myPlus in C++ "foo<bar<T>>" needs to be "foo<bar<T> >" with a space between the last two >'s since >> is the right-shift operator. This is a nasty gotcha.using the triangular braces for template instantiation...The trouble with Name<ARG> is (as I understand it) that it requires semantic analysis to figure out whether < and > are being used as angle brackets, or as greater-than/less-than in an expression.Plain-old parenths might cut it, but that makes what's going on less obvious. Braces already have meaning as well. I agree that template instantiation should be much less verbose; it's just a matter of working out a good syntax for it. -- andy
Dec 01 2003
Sean "Ben Hinkle" <bhinkle4 juno.com> wrote in message news:bqfq0a$1bl9$1 digitaldaemon.com...Plus in C++ "foo<bar<T>>" needs to be "foo<bar<T> >" with a space between the last two >'s since >> is the right-shift operator. This is a nasty gotcha.
Dec 01 2003
In article <bqfm1p$14ks$1 digitaldaemon.com>, Andy Friesen says...Heretic wrote:If we are going to keep explicit instantiation I think it would be good to replace the instance keyword with a symbol like '$' template Foo(Type) { void Bar(Type a) { } } $Foo(int).Bar(12); alias $Foo(int).Bar BarInt;yo again. i`ve finally read the D language specification, lol. the more i read bout d, the better it seems. however there`s one thing that annoys me. templates have to be instantiated with the "instance Name(ARG)" expression... i dont quite like it, wouldnt it be better to use Name<ARG> ? it is still context independent and much faster to type, more comfortable and stuff. in c++ i have a function alias_cast<T>(char*) that works as a object reference. i add aliases to objects, but not aliases in teh D way. i name objects with character strings and i`m able to access them this way. in D, i`d have to write instance alias_cast(T).f(char*) whis is quite tedious to type and is less clear :/ maybe i`m missing something, pls correct me then, but consider my suggestion of using the triangular braces for template instantiation...The trouble with Name<ARG> is (as I understand it) that it requires semantic analysis to figure out whether < and > are being used as angle brackets, or as greater-than/less-than in an expression. Plain-old parenths might cut it, but that makes what's going on less obvious. Braces already have meaning as well. I agree that template instantiation should be much less verbose; it's just a matter of working out a good syntax for it.
Dec 01 2003
Cool idea! $ feels a little to much like 'interpolation' to me, but this sounds very cool ( if we want to keep the explicit , why do we want this again ? I dont use templates that much sry ) C "Patrick Down" <Patrick_member pathlink.com> wrote in message news:bqftct$1gqe$1 digitaldaemon.com...In article <bqfm1p$14ks$1 digitaldaemon.com>, Andy Friesen says...i readHeretic wrote:yo again. i`ve finally read the D language specification, lol. the moreexpression... ibout d, the better it seems. however there`s one thing that annoys me. templates have to be instantiated with the "instance Name(ARG)"contextdont quite like it, wouldnt it be better to use Name<ARG> ? it is stilli have aindependent and much faster to type, more comfortable and stuff. in c++aliases tofunction alias_cast<T>(char*) that works as a object reference. i addstrings andobjects, but not aliases in teh D way. i name objects with character:/i`m able to access them this way. in D, i`d have to write instance alias_cast(T).f(char*) whis is quite tedious to type and is less clearsuggestion ofmaybe i`m missing something, pls correct me then, but consider myIf we are going to keep explicit instantiation I think it would be good to replace the instance keyword with a symbol like '$' template Foo(Type) { void Bar(Type a) { } } $Foo(int).Bar(12); alias $Foo(int).Bar BarInt;using the triangular braces for template instantiation...The trouble with Name<ARG> is (as I understand it) that it requires semantic analysis to figure out whether < and > are being used as angle brackets, or as greater-than/less-than in an expression. Plain-old parenths might cut it, but that makes what's going on less obvious. Braces already have meaning as well. I agree that template instantiation should be much less verbose; it's just a matter of working out a good syntax for it.
Dec 01 2003
i don't want to keep it explicit with a keyword or token. oh, and i want to get rid of templates that are like namespaces. eighter we get namespaces, and can add template parameters to it, or no namespaces at all. namespaces or not, i want templates for functions, and for classes. instance Swap(int).call(a,b); is just plain stupid.. instance(int) swap(a,b); would be nice, for example.. but swap(a,b); is still unbeatable.... oh, and have i said i'd like to promote a swap operator? a <=> b; //swaps a and b now THAT was offtopic:D In article <bqfu00$1hlj$1 digitaldaemon.com>, Charles Sanders says...Cool idea! $ feels a little to much like 'interpolation' to me, but this sounds very cool ( if we want to keep the explicit , why do we want this again ? I dont use templates that much sry ) C "Patrick Down" <Patrick_member pathlink.com> wrote in message news:bqftct$1gqe$1 digitaldaemon.com...In article <bqfm1p$14ks$1 digitaldaemon.com>, Andy Friesen says...i readHeretic wrote:yo again. i`ve finally read the D language specification, lol. the moreexpression... ibout d, the better it seems. however there`s one thing that annoys me. templates have to be instantiated with the "instance Name(ARG)"contextdont quite like it, wouldnt it be better to use Name<ARG> ? it is stilli have aindependent and much faster to type, more comfortable and stuff. in c++aliases tofunction alias_cast<T>(char*) that works as a object reference. i addstrings andobjects, but not aliases in teh D way. i name objects with character:/i`m able to access them this way. in D, i`d have to write instance alias_cast(T).f(char*) whis is quite tedious to type and is less clearsuggestion ofmaybe i`m missing something, pls correct me then, but consider myIf we are going to keep explicit instantiation I think it would be good to replace the instance keyword with a symbol like '$' template Foo(Type) { void Bar(Type a) { } } $Foo(int).Bar(12); alias $Foo(int).Bar BarInt;using the triangular braces for template instantiation...The trouble with Name<ARG> is (as I understand it) that it requires semantic analysis to figure out whether < and > are being used as angle brackets, or as greater-than/less-than in an expression. Plain-old parenths might cut it, but that makes what's going on less obvious. Braces already have meaning as well. I agree that template instantiation should be much less verbose; it's just a matter of working out a good syntax for it.
Dec 01 2003
Actually I've been wishing lately for template namespaces in C++. ;) I want a swap, min, and max operator. swap: a <=> b; min: smaller = a <? b; max: larger = a >? b; And yes I want to be able to overload them. I may want an overloadable abs operator as well. abs: magnitude = |+| a; In C++ I'm overloading unary plus to do this. ;) Sean "davepermen" <davepermen_member pathlink.com> wrote in message news:bqfvdu$1jqp$1 digitaldaemon.com...i don't want to keep it explicit with a keyword or token. oh, and i want to get rid of templates that are like namespaces. eighterwe getnamespaces, and can add template parameters to it, or no namespaces atall.namespaces or not, i want templates for functions, and for classes. instance Swap(int).call(a,b); is just plain stupid.. instance(int) swap(a,b); would be nice, for example.. but swap(a,b); is still unbeatable.... oh, and have i said i'd like to promote a swap operator? a <=> b; //swaps a and b now THAT was offtopic:D
Dec 01 2003
davepermen wrote:i don't want to keep it explicit with a keyword or token.oh, and i want to get rid of templates that are like namespaces. eighter we get namespaces, and can add template parameters to it, or no namespaces at all.namespaces or not, i want templates for functions, and for classes. instance Swap(int).call(a,b); is just plain stupid.. instance(int) swap(a,b); would be nice, for example.. but swap(a,b); is still unbeatable....I agree completely, with all your points! The instance() form of instantiation is a nuisance that I never understood the need for. Is it just because the are no unambiguous parantheses "left"? I'm sure we can find some unused token to replace the <>! If you use templates a lot then all this "instance" stuff becomes starts getting on your nerves quickly. And it doesn't exactly make the code more readable either... Oh, and can't we please have implicit instantiation? This is a very powerful feature that goes a long way to enable you to enhance the built-in language facilities. Is it that hard to implement? And while I'm at it: the namespaces may sound nice in theory - making templates generic constructs that you can put almost anything into - but in practice they make the code less readable. For example, templates are most often used in collection classes. If you're writing a single collection class, then you face a naming problem: what should the name of the namespace be and then what do you call the class? If templates stay the way they are now, then I'm prediciting that we will see a lot of the following in D: template TLinkedList(T) { class TheClass { ... } } You have to give your class two names! One for that obligatory namespace and one for the class itself. And you have to specify both names when you use it ... pretty burdensome, if you ask me. And no, typedefs do not really provide much of a help here either, because it is in the nature of templates that they are written without knowing the types that they are going to be used with. So adding dozens of typedefs of the form typedef instance TLinkedList(int).TheClass TLinkedList_Int; typedef instance TLinkedList(MyClassA).TheClass TLinkedList_MyClassA; typedef instance TLinkedList(MyClassB).TheClass TLinkedList_MyClassB; is out of the question. Besides, the manual name mangling that would be required for such typedefs isn't exactly a property you'd expect from a modern language. There may be uses for template namespaces, but personally I never found myself wishing for that feature in C++. If the namespace was optional, then it would be a nice "icing on the cake" feature, but the way it is now it is a nuisance. Couldn't the template and class definition be merged? Like template class TLinkedList(T) { ... } as a synonym for template <currentnamespace>(T) { class TLinkedList { } } Hoping this post doesn't sound too negative. I just think these issues are too important for the language to just let them pass... Hauke
Dec 01 2003
In article <bqg9gu$23bd$1 digitaldaemon.com>, Hauke Duden says...davepermen wrote:I think this would be a great step towards real genericity in practice! I wonder, would this be unreasonably hard to implement?instance Swap(int).call(a,b); is just plain stupid.. instance(int) swap(a,b); would be nice, for example.. but swap(a,b); is still unbeatable....I agree completely, with all your points!The instance() form of instantiation is a nuisance that I never understood the need for. Is it just because the are no unambiguous parantheses "left"? I'm sure we can find some unused token to replace the <>! If you use templates a lot then all this "instance" stuff becomes starts getting on your nerves quickly. And it doesn't exactly make the code more readable either...For all I know, this instance() thing may have been a natural consequence of templates originally having been a preprcessor- only thing. Genericity was so separate from the language and the compiler that it may have been only natural not to come to think of this as the ultimate goal at the time.Oh, and can't we please have implicit instantiation? This is a very powerful feature that goes a long way to enable you to enhance the built-in language facilities. Is it that hard to implement?Implicit instantiation was another thing, IMHO, that just wasn't even theoretically possible when Stepanov was at it. I could bet my last penny on that it'd have been there right from the start, had they ever known how.
Dec 01 2003
In article <bqg9gu$23bd$1 digitaldaemon.com>, Hauke Duden wrote:davepermen wrote:Maybe replacing <> with something else is not the way to go at all... at syntax _different_ from <> will at any case make D an oddball in the eyes of many programmers.swap(a,b); is still unbeatable....I agree completely, with all your points! The instance() form of instantiation is a nuisance that I never understood the need for. Is it just because the are no unambiguous parantheses "left"? I'm sure we can find some unused token to replace the <>! If you use templates a lot then all this "instance" stuffAnd while I'm at it: the namespaces may sound nice in theory - making templates generic constructs that you can put almost anything into - but in practice they make the code less readable.True. At least if you're accustomed to the C++ way. In C++, you *have* to make your templates classes -- even if class wasn't the proper way to structure the code in the first place. This is similar to Java, which actually forces you to make *all* modules classes, even when they don't have generic arguments! In fact, Java is more consistent than C++ because there isn't anything (functions, enums, ...) that you *couldn't* genericize! Classes are the beginning and the end of all. We are the Class. You will be ... classimilated. This isn't to say that the D template approach is totally right. In D, you can template anything *except* the things that you usually want to template: classes, functions (with implicit instantiation, s'il vous plaît), typedefs (which are sorely missing in -- current -- C++!)...For example, templates are most often used in collection classes. If you're writing a single collection class, then you face a naming problem: what should the name of the namespace be and then what do you call the class? If templates stay the way they are now, then I'm prediciting that we will see a lot of the following in D: template TLinkedList(T) { class TheClass { ... } }True.You have to give your class two names! One for that obligatory namespace and one for the class itself. And you have to specify both names when you use it ... pretty burdensome, if you ask me.True.And no, typedefs do not really provide much of a help here either, because it is in the nature of templates that they are written without knowing the types that they are going to be used with. So adding dozens of typedefs of the form typedef instance TLinkedList(int).TheClass TLinkedList_Int; typedef instance TLinkedList(MyClassA).TheClass TLinkedList_MyClassA; typedef instance TLinkedList(MyClassB).TheClass TLinkedList_MyClassB; is out of the question. Besides, the manual name mangling that would be required for such typedefs isn't exactly a property you'd expect from a modern language.True.There may be uses for template namespaces, but personally I never found myself wishing for that feature in C++. If the namespace was optional, then it would be a nice "icing on the cake" feature, but the way it is now it is a nuisance.I found one place where it would be convenient if a module had generic arguments, using syntax not particularly C++ nor D: enum Byte_Order { little_endian, big_endian } module Endian<Byte_Order byte_order> { T fetch<T : Arithmetic_Type>(const byte* data) { // fetch result from address data assuming byte order byte_order } void store<T : Arithmetic_Type>(T value, byte* data) { // store value to address data assuming byte order byte_order } } Then I could use Endian<little_endian>.fetch<int>(address) to get a value from address assuming it's stored in little endian, and Endian<big_endian>.store(value, address) to store it in big endian form. But ah well, this is just cosmetic stuff. I can just as well use a templated class in C++, with two static functions, or two free-standing functions. It's just that the Endian module isn't really *conceptually* a class. I guess you could find a lot of modules that you'd want to templatize, if you'd start doing some serious metaprogramming stuff.Couldn't the template and class definition be merged? Like template class TLinkedList(T) { ... } as a synonym for template <currentnamespace>(T) { class TLinkedList { } }I like this proposal. It would be nice to get templatized functions, typedefs, aliases and even enums with the same syntax. Hypothetical stupid example: template class TLinkedListSynchronized(T) { ...} template class TLinkedListSingleThreaded(T) { ...} version (SingleThreaded) { template alias TLinkedListMultiThreaded(T) LinkedList(T); } version (MultiThreaded) { template alias TLinkedListSynchronized(T) LinkedList(T); } If this proves difficult to parse, then put the argument list after the keyword "template": template(T) class LinkedList { ... } template(T) T min(T a, T b) { return a < b ? a : b; } I don't know what you'd use template enums for, but why not allow them if you allow everything else?Hoping this post doesn't sound too negative. I just think these issues are too important for the language to just let them pass...True. Generic programming is an essential feature for an industry standard programming language. Metaprogramming will be the next big step. If one could make *that* leap, one might get generic programming for free as a side effect. But that won't happen in a couple of years... -Antti
Dec 03 2003
In article <bqfu00$1hlj$1 digitaldaemon.com>, Charles Sanders wrote:Cool idea! $ feels a little to much like 'interpolation' to me, but this sounds very cool ( if we want to keep the explicit , why do we want this again ? I dont use templates that much sry )To me, $ feels for some reason like perl... -Antti
Dec 01 2003
Yea, and PHP, where print "$foo"; prints the value of the variable foo ( interpolation they call it ) . C "Antti Sykäri" <jsykari gamma.hut.fi> wrote in message news:slrnbsn8c6.17n.jsykari pulu.hut.fi...In article <bqfu00$1hlj$1 digitaldaemon.com>, Charles Sanders wrote:thisCool idea! $ feels a little to much like 'interpolation' to me, butsounds very cool ( if we want to keep the explicit , why do we want this again ? I dont use templates that much sry )To me, $ feels for some reason like perl... -Antti
Dec 01 2003
IMO '$' should be reserved to denote the end of an array in the slice operation. SomeType array[ SOME_NUMBER ] ; array[0..$] ; /* slices the whole array */ array[$-2..$] /* slices the last three elements */ Mark. "Charles Sanders" <sanders-consulting comcast.net> wrote in message news:bqg79k$1vpi$1 digitaldaemon.com...Yea, and PHP, where print "$foo"; prints the value of the variable foo ( interpolation they call it ) . C "Antti Sykäri" <jsykari gamma.hut.fi> wrote in message news:slrnbsn8c6.17n.jsykari pulu.hut.fi...thisIn article <bqfu00$1hlj$1 digitaldaemon.com>, Charles Sanders wrote:thisCool idea! $ feels a little to much like 'interpolation' to me, butsounds very cool ( if we want to keep the explicit , why do we wantagain ? I dont use templates that much sry )To me, $ feels for some reason like perl... -Antti
Dec 01 2003
In article <bqgclt$2843$1 digitaldaemon.com>, Mark J. Brudnak says...IMO '$' should be reserved to denote the end of an array in the slice operation. SomeType array[ SOME_NUMBER ] ; array[0..$] ; /* slices the whole array */ array[$-2..$] /* slices the last three elements */ Mark.IMO you opinion is realy cool. Ant
Dec 01 2003
In article <bqgdrs$29u1$1 digitaldaemon.com>, Ant says...In article <bqgclt$2843$1 digitaldaemon.com>, Mark J. Brudnak says...IMO why not use the same syntax as Python? array[:] a slice of the whole array array[-5:] last five elements array[:-5] all but last five Also D should have this: In article <bqgdrs$29u1$1 digitaldaemon.com>, Ant says...IMO '$' should be reserved to denote the end of an array in the slice operation. SomeType array[ SOME_NUMBER ] ; array[0..$] ; /* slices the whole array */ array[$-2..$] /* slices the last three elements */ Mark.IMO you opinion is realy cool. AntIn article <bqgclt$2843$1 digitaldaemon.com>, Mark J. Brudnak says...IMO use the same syntax as Python, skip the perlin noise? or use .. if you insist ;-) but it's two keypresses rather than 1 array[:] // a slice of the whole array array[-5:] // last five elements array[:-5] // all but last five And D should also have this: some = array[-2] //next to last component this is absolutely beautiful if you have a circular list or something and have to do something based on other members. /* foreach(int i, inout float f; a) { //this will overstep the bounds so we need a special case for the last two f = (a[i+1] + a[i+2])*0.5 } */ foreach(int i, float f; a) { //instead we start at the next to latest a[i-2] = (a[i-1] + a[i])*0.5 } NonniIMO '$' should be reserved to denote the end of an array in the slice operation. SomeType array[ SOME_NUMBER ] ; array[0..$] ; /* slices the whole array */ array[$-2..$] /* slices the last three elements */ Mark.IMO you opinion is realy cool. Ant
Dec 03 2003
IMO '$' should be reserved to denote the end of an array in the slice operation. SomeType array[ SOME_NUMBER ] ; array[0..$] ; /* slices the whole array */ array[$-2..$] /* slices the last three elements */ Mark.why? nothing is shorter than $. and i really mean NOTHING! :D array[x..] == your array[x..$] array[..x] == your array[0..x] array[..] == your array[0..$] == array[] would be best..
Dec 01 2003
"davepermen" <davepermen_member pathlink.com> wrote in message news:bqgebg$2ank$1 digitaldaemon.com...What about the case: array[$-2..$] /* slices the last three elements */ in your notation?IMO '$' should be reserved to denote the end of an array in the slice operation. SomeType array[ SOME_NUMBER ] ; array[0..$] ; /* slices the whole array */ array[$-2..$] /* slices the last three elements */ Mark.why? nothing is shorter than $. and i really mean NOTHING! :D array[x..] == your array[x..$] array[..x] == your array[0..x] array[..] == your array[0..$] == array[] would be best..
Dec 01 2003
Mark Brudnak wrote:What about the case: array[$-2..$] /* slices the last three elements */array[-2..] with the limitation, that this minus may not be a part of the argument, but only a unary minus. Hell, make it a "[-"-operator! A negative value of arguments should be still out-of-bounds error. -eye
Dec 03 2003
In article <bqlhgf$mr7$1 digitaldaemon.com>, Ilya Minkov says...Mark Brudnak wrote:I much prefer ":" as it's more concise, and i'm allergic to Pascal, I think. array[:] // a slice of the whole array array[-5:] // last five elements array[:-5] // all but last five And contrary to what Ilya says, D should definitely steal this from Python: some = array[-2] //next to last component this is absolutely beautiful if you have a circular list or something and have to do something based on other members. Convolution type things. /* foreach(int i, inout float f; a) { //this will overstep the bounds so we need a special case for the last two f = (a[i+1] + a[i+2])*0.5 } */ foreach(int i, float f; a) { //instead we start at the next to latest a[i-2] = (a[i-1] + a[i])*0.5 } Nonni PS: sorry for (more or less) reposting, but last time got garbled, and this fit even better here.What about the case: array[$-2..$] /* slices the last three elements */array[-2..] with the limitation, that this minus may not be a part of the argument, but only a unary minus. Hell, make it a "[-"-operator! A negative value of arguments should be still out-of-bounds error. -eye
Dec 03 2003
In article <bqlvrg$1cft$1 digitaldaemon.com>, Jon Thoroddsen says...I much prefer ":" as it's more concise, and i'm allergic to Pascal, I think.Call it "much" more concise. :)array[:] // a slice of the whole array array[-5:] // last five elements array[:-5] // all but last fiveThese ones should all work with my proposal, as - would be a unary operator right within this expression. Whatever variable is here in place of your "5" should be nontheless positive.And contrary to what Ilya says, D should definitely steal this from Python:It's not exactly my opinion i was stating, i was just looking for a compromise. Walter is strongly against a forced neg check at runtime for every array access, since this is bad for performance. And we *really* want to rival not only with Java, but also with C++. ;) So i propose that whenever it is possible to derive that negativity from expression syntax, it should be done, else it is an error.some = array[-2] //next to last componentThis would work.this is absolutely beautiful if you have a circular list or something and have to do something based on other members. Convolution type things.--- 8< --- >8 --- Walter is against real circularity. It has to be exceptionally rare, whereas acessing the n-th element from the end is very common and i would wish there be some neat syntactic way to express it. However, it's not really a problem if there isn't. There ought to be quite more serious issues. -eye
Dec 04 2003
"Ilya Minkov" <Ilya_member pathlink.com> wrote in message news:bqno46$trs$1 digitaldaemon.com...In article <bqlvrg$1cft$1 digitaldaemon.com>, Jon Thoroddsen says...think.I much prefer ":" as it's more concise, and i'm allergic to Pascal, ICall it "much" more concise. :)operatorarray[:] // a slice of the whole array array[-5:] // last five elements array[:-5] // all but last fiveThese ones should all work with my proposal, as - would be a unaryright within this expression. Whatever variable is here in place of your"5"should be nontheless positive.Python:And contrary to what Ilya says, D should definitely steal this fromIt's not exactly my opinion i was stating, i was just looking for acompromise.Walter is strongly against a forced neg check at runtime for every arrayaccess,since this is bad for performance. And we *really* want to rival not onlywithJava, but also with C++. ;) So i propose that whenever it is possible toderivethat negativity from expression syntax, it should be done, else it is anerror. True true. You probably already said this, Ilya, but it would seem that in this context, there should be [- and :- tokens, or that somehow the unary minus is not lexically part of some int literal. I think this is already the case. It would not be an unreasonable restriction to have the "negativeness" be part of hte expression and not part of the variable. It might be tricky to explain to newbies though. It would have to be in the FAQ. It would be worth having though.havesome = array[-2] //next to last componentThis would work.this is absolutely beautiful if you have a circular list or something andwhereasto do something based on other members. Convolution type things.--- 8< --- >8 --- Walter is against real circularity. It has to be exceptionally rare,acessing the n-th element from the end is very common and i would wishthere besome neat syntactic way to express it. However, it's not really a problemifthere isn't. There ought to be quite more serious issues. -eyeCircularity is still best handled with modulo % or and &. You can make your own container that always does modulo on the index. Sean
Dec 04 2003
"Jon Thoroddsen" <Jon_member pathlink.com> wrote <snip>I much prefer ":" as it's more concise, and i'm allergic to Pascal, Ithink.array[:] // a slice of the whole array array[-5:] // last five elementsThis would actually slice the last *six* elements, $-5, $-4, $-3, $-2, $-1, and $array[:-5] // all but last fiveAnd contrary to what Ilya says, D should definitely steal this fromPython:some = array[-2] //next to last componentThis breaks symetry in the notation. In your scheme, first element: array[0] last element: array[-1] To be consistent, it should be: first element: array[0] last element: array[-0] second element: array[1] second to last element: array[-1] but as we know -0 = 0. It this worth the economy of saving one or two key strokes?? Especially since the current situation is int end = arrary.length -1 ; slice = array[end-4..end] ; /* slices the last five elements */ ...or... slice = array[(array.length-5)..(array.length-1)] ; /* slices the last five elements */ It would seem a huge improvment to introduce the $ as a shortcut for "array.length - 1" then slice = array[$-4..$] ; /* slices the last five elements */ We save two keystrokes by using your notation slice = array[-4..] ; /* slices the last five elements */ at the expense of (IMO) clarity.this is absolutely beautiful if you have a circular list or something andhaveto do something based on other members. Convolution type things. /* foreach(int i, inout float f; a) { //this will overstep the bounds so we need a special case for the last two f = (a[i+1] + a[i+2])*0.5 } */ foreach(int i, float f; a) { //instead we start at the next to latest a[i-2] = (a[i-1] + a[i])*0.5 } Nonni PS: sorry for (more or less) reposting, but last time got garbled, andthis fiteven better here.
Dec 05 2003
Mark J. Brudnak wrote:No, not if it works as in Python. In Python, -1 refers to the last element of the array, so a slice starting at -5 consists of the last 5 elements.array[:] // a slice of the whole array array[-5:] // last five elementsThis would actually slice the last *six* elements, $-5, $-4, $-3, $-2, $-1, and $Python:<snip>some = array[-2] //next to last componentThis breaks symetry in the notation. In your scheme, first element: array[0] last element: array[-1] To be consistent, it should be: first element: array[0] last element: array[-0]but as we know -0 = 0.That's what many programmers think when they see Python slices for the first time. I did too. But it's all just a matter of definition. If you define that indices refer to the left "border" of array slots, then the Python way is the consistent one. Consider the following array (view with fixed width font) | a | b | c | d | ^ ^ ^ 0 -1 -0 a,b,c,d are the values in the array slots, the numbers below are the indices, referring to the left border of the corresponding slot. Viewed like this, -0 would refer to the (non-existent) element following d and -1 refers to d itself. Also, as you point out, if you define -0 as the last element, then you cannot reference it, since -0 = 0. So the Python way is really the only way to do it if you want to avoid having to pass an expression of some kind (like your $-2) instead of an integer value (-2). Also, maybe surprisingly, this system actually works very well in practice. Up to now, I never got confused by the indexing scheme. And I think most Python programmers will agree with that. Hauke
Dec 05 2003
"Mark J. Brudnak" <mjbrudna oakland.edu> wrote in message news:bqq2j3$1av8$1 digitaldaemon.com..."Jon Thoroddsen" <Jon_member pathlink.com> wrote$-1,array[:] // a slice of the whole array array[-5:] // last five elementsThis would actually slice the last *six* elements, $-5, $-4, $-3, $-2,and $Icon apparently does it the same way as Python.array[:-5] // all but last fiveIt this worth the economy of saving one or two key strokes?? Especially since the current situation is int end = arrary.length -1 ; slice = array[end-4..end] ; /* slices the last five elements */No, in current DMD this would slice off the four elements just before the last element in the array, not including the last element. Sean
Dec 05 2003
Sounds good. Sean "Mark J. Brudnak" <mjbrudna oakland.edu> wrote in message news:bqgclt$2843$1 digitaldaemon.com...IMO '$' should be reserved to denote the end of an array in the slice operation. SomeType array[ SOME_NUMBER ] ; array[0..$] ; /* slices the whole array */ array[$-2..$] /* slices the last three elements */ Mark.
Dec 01 2003
Tangenting the issue, I just recently realized (maybe it was crystal-clear to everybody else, but what the heck) that representing half-open ranges, which was proposed some time earlier, actually wouldn't be completely impossible. array[a .. b) // for (int i=a; i<b; ++i) array[a .. b] // for (int i=a; i<=b; ++i) array(a .. b) // for (int i=a+1; i<b; ++i) array(a .. b] // for (int i=a+1; i<=b; ++i) I for some reason thought that the language grammar couldn't allow [ and ) to match. Silly me. Just requires adding the proper grammar productions: PostfixExpression [ Expression ) PostfixExpression [ Expression ] PostfixExpression ( Expression ) PostfixExpression ( Expression ] Ok, so it might conflict a bit with the function call syntax. And would probably cause a lot of nice off-by-one bugs because ( and [ look the same. So we may probably forget it :) -Antti In article <bqh9vd$h48$1 digitaldaemon.com>, Sean L. Palmer wrote:Sounds good.*nods*Sean "Mark J. Brudnak" <mjbrudna oakland.edu> wrote in message news:bqgclt$2843$1 digitaldaemon.com...IMO '$' should be reserved to denote the end of an array in the slice operation. SomeType array[ SOME_NUMBER ] ; array[0..$] ; /* slices the whole array */ array[$-2..$] /* slices the last three elements */ Mark.
Dec 03 2003
Maybe slightly OT, but is there a reason that D doesn't support automatic deduction of types like C++ does? ie template swap(T){ void call(T a, T b){ } } would be nice if you could call it like instance swap(*).call(int,int) Which would deduce the type of the instance from the parameters. Or better yet, as someone said - remove the templating namespace and have call(int,int). Also, I do find the current template syntax slightly clunky, but could live with it if deduction was possible :) ie if something like , alias instance swap(*).call mySwap, was possible I'd be happier. Sorry if this is old ground. Cheers Brad
Dec 01 2003
"$" is better than "instance". Instance is just too wordy. I feel like the people designing the D templates have never done much generic programming in C++. Sean "Patrick Down" <Patrick_member pathlink.com> wrote in message news:bqftct$1gqe$1 digitaldaemon.com...In article <bqfm1p$14ks$1 digitaldaemon.com>, Andy Friesen says...i readHeretic wrote:yo again. i`ve finally read the D language specification, lol. the moreexpression... ibout d, the better it seems. however there`s one thing that annoys me. templates have to be instantiated with the "instance Name(ARG)"contextdont quite like it, wouldnt it be better to use Name<ARG> ? it is stilli have aindependent and much faster to type, more comfortable and stuff. in c++aliases tofunction alias_cast<T>(char*) that works as a object reference. i addstrings andobjects, but not aliases in teh D way. i name objects with character:/i`m able to access them this way. in D, i`d have to write instance alias_cast(T).f(char*) whis is quite tedious to type and is less clearsuggestion ofmaybe i`m missing something, pls correct me then, but consider myIf we are going to keep explicit instantiation I think it would be good to replace the instance keyword with a symbol like '$' template Foo(Type) { void Bar(Type a) { } } $Foo(int).Bar(12); alias $Foo(int).Bar BarInt;using the triangular braces for template instantiation...The trouble with Name<ARG> is (as I understand it) that it requires semantic analysis to figure out whether < and > are being used as angle brackets, or as greater-than/less-than in an expression. Plain-old parenths might cut it, but that makes what's going on less obvious. Braces already have meaning as well. I agree that template instantiation should be much less verbose; it's just a matter of working out a good syntax for it.
Dec 01 2003
Why cannot use simply instancetype(newinstancetype) and the compiler will know that, if instancetype is a "instance type", then it will instantiate it; and that's all In article <bqftct$1gqe$1 digitaldaemon.com>, Patrick Down says...In article <bqfm1p$14ks$1 digitaldaemon.com>, Andy Friesen says...Heretic wrote:If we are going to keep explicit instantiation I think it would be good to replace the instance keyword with a symbol like '$' template Foo(Type) { void Bar(Type a) { } } $Foo(int).Bar(12); alias $Foo(int).Bar BarInt;yo again. i`ve finally read the D language specification, lol. the more i read bout d, the better it seems. however there`s one thing that annoys me. templates have to be instantiated with the "instance Name(ARG)" expression... i dont quite like it, wouldnt it be better to use Name<ARG> ? it is still context independent and much faster to type, more comfortable and stuff. in c++ i have a function alias_cast<T>(char*) that works as a object reference. i add aliases to objects, but not aliases in teh D way. i name objects with character strings and i`m able to access them this way. in D, i`d have to write instance alias_cast(T).f(char*) whis is quite tedious to type and is less clear :/ maybe i`m missing something, pls correct me then, but consider my suggestion of using the triangular braces for template instantiation...The trouble with Name<ARG> is (as I understand it) that it requires semantic analysis to figure out whether < and > are being used as angle brackets, or as greater-than/less-than in an expression. Plain-old parenths might cut it, but that makes what's going on less obvious. Braces already have meaning as well. I agree that template instantiation should be much less verbose; it's just a matter of working out a good syntax for it.
Dec 01 2003
Felix wrote:Why cannot use simply instancetype(newinstancetype) and the compiler will know that, if instancetype is a "instance type", then it will instantiate it; and that's allThis is my feeling on the matter to. You mean like: template Tfunc(T) { func(T) {} } ... Tfunc(int) X;
Dec 02 2003
Exactly. But I understand the current syntax if it is only experimental one. In article <bqhmes$13av$1 digitaldaemon.com>, J Anderson says...Felix wrote:Why cannot use simply instancetype(newinstancetype) and the compiler will know that, if instancetype is a "instance type", then it will instantiate it; and that's allThis is my feeling on the matter to. You mean like: template Tfunc(T) { func(T) {} } ... Tfunc(int) X;
Dec 02 2003
Felix wrote:Exactly. But I understand the current syntax if it is only experimental one. In article <bqhmes$13av$1 digitaldaemon.com>, J Anderson says...I do think Walter would have used the above syntax, if there wasn't some type of syntactical parsing difficulty. The D compiler is meant to be easy to replicate. -AndersonFelix wrote:Why cannot use simply instancetype(newinstancetype) and the compiler will know that, if instancetype is a "instance type", then it will instantiate it; and that's allThis is my feeling on the matter to. You mean like: template Tfunc(T) { func(T) {} } ... Tfunc(int) X;
Dec 02 2003
I do think Walter would have used the above syntax, if there wasn't some type of syntactical parsing difficulty. The D compiler is meant to be easy to replicate.But at what cost, an cumbersome language ? C "J Anderson" <REMOVEanderson badmama.com.au> wrote in message news:bqifev$2aef$1 digitaldaemon.com...Felix wrote:one.Exactly. But I understand the current syntax if it is only experimentalwill knowIn article <bqhmes$13av$1 digitaldaemon.com>, J Anderson says...Felix wrote:Why cannot use simply instancetype(newinstancetype) and the compilerit; andthat, if instancetype is a "instance type", then it will instantiateI do think Walter would have used the above syntax, if there wasn't some type of syntactical parsing difficulty. The D compiler is meant to be easy to replicate. -Andersonthat's allThis is my feeling on the matter to. You mean like: template Tfunc(T) { func(T) {} } ... Tfunc(int) X;
Dec 03 2003
Felix <Felix_member pathlink.com> wrote in news:bqhfsl$p7k$1 digitaldaemon.com:Why cannot use simply instancetype(newinstancetype) and the compiler will know that, if instancetype is a "instance type", then it will instantiate it; and that's allI agree that would be best. You will have to ask Walter but I belive the reason for the 'instance' keyword is to resolve some context sensitivity issue with the langauge grammer.
Dec 02 2003
"Patrick Down" <pat codemoon.com> wrote in message news:Xns944555EF3300Bpatcodemooncom 63.105.9.61...Felix <Felix_member pathlink.com> wrote in news:bqhfsl$p7k$1 digitaldaemon.com:You're right. It would not parse in a context-free manner.Why cannot use simply instancetype(newinstancetype) and the compiler will know that, if instancetype is a "instance type", then it will instantiate it; and that's allI agree that would be best. You will have to ask Walter but I belive the reason for the 'instance' keyword is to resolve some context sensitivity issue with the langauge grammer.
Dec 19 2003
This is how templates should be declared: template(A, B, C) class Foo { public A a; public B b; public C c; } And this is how templates must be used: //explicit instantiation Foo(int, float, double) foo1; //implicit instantiation Foo foo2; foo.A = 5; foo.B = 5.3; foo.C = "yo"; Some notes: 1. since the template parameters are arguments to the template class, the syntax <identifier>(<type>) is perfectly justified. 2. In C++, the compiler deduces the types of a template function from its usage. It is possible to do so in classes also. 3. A template should not accept only values and types, but any expression that when it is used, it can be compiled. Templates should be more like parameterized macros. This would enable real generics. "Heretic" <Heretic_member pathlink.com> wrote in message news:bqffep$q9u$1 digitaldaemon.com...yo again. i`ve finally read the D language specification, lol. the more ireadbout d, the better it seems. however there`s one thing that annoys me. templates have to be instantiated with the "instance Name(ARG)"expression... idont quite like it, wouldnt it be better to use Name<ARG> ? it is stillcontextindependent and much faster to type, more comfortable and stuff. in c++ ihave afunction alias_cast<T>(char*) that works as a object reference. i addaliases toobjects, but not aliases in teh D way. i name objects with characterstrings andi`m able to access them this way. in D, i`d have to write instance alias_cast(T).f(char*) whis is quite tedious to type and is less clear :/ maybe i`m missing something, pls correct me then, but consider mysuggestion ofusing the triangular braces for template instantiation...
Dec 01 2003
In article <bqg9nn$23m3$1 digitaldaemon.com>, Achilleas Margaritis says...This is how templates should be declared: template(A, B, C) class Foo { public A a; public B b; public C c; } And this is how templates must be used: //explicit instantiation Foo(int, float, double) foo1; //implicit instantiation Foo foo2; foo.A = 5; foo.B = 5.3; foo.C = "yo";I'd have that as Foo foo; foo.a = 5; foo.b = 5.3; foo.c = "yo"; The you could also have template(A, B, C) class Foo { public A aa; public A ab; public B b; public C c; } and Foo foo; foo.aa = 5; foo.ab = 49; foo.b = 5.3; foo.c = "yo";
Dec 01 2003
quote { i don't want to keep it explicit with a keyword or token. oh, and i want to get rid of templates that are like namespaces. eighter we get namespaces, and can add template parameters to it, or no namespaces at all. namespaces or not, i want templates for functions, and for classes. instance Swap(int).call(a,b); is just plain stupid.. instance(int) swap(a,b); would be nice, for example..but swap(a,b); is still unbeatable.... } quote { The instance() form of instantiation is a nuisance that I never understood the need for. Is it just because the are no unambiguous parantheses "left"? I'm sure we can find some unused token to replace the <>! If you use templates a lot then all this "instance" stuff becomes starts getting on your nerves quickly. And it doesn't exactly make the code more readable either... } return true; Doesnt this seem like the D standard is kinda oriented towards easy compiler generation and fast compilation ? It isn`t always a good thing... dont get me wrong, i have never coded a compiler so i dont know how hard it is to code this stuff... but this template instantiation syntax pisses me off :/
Dec 01 2003
Heretic wrote:Doesnt this seem like the D standard is kinda oriented towards easy compiler generation and fast compilation ? It isn`t always a good thing... dont get me wrong, i have never coded a compiler so i dont know how hard it is to code this stuff... but this template instantiation syntax pisses me off :/I'm all for making a few tradeoffs for the sake of compiler simplicity, but I think good, convenient generic programming support is one of those things that's worth suffering a few contortions for. -- andy
Dec 02 2003
Andy Friesen wrote:Heretic wrote:Hmm. I would expect to do: list<int> intList = new list<int>; or even list<list<int>> listOfIntLists = new list<list<int>>; Granted, I might (for my own convenience sake) do a typedef for these types: typedef list<int> intlist; typedef list<intlist> intcartesian; But I would expect those types of decisions to be my perogative as a developer. Is there regexp support in D? It might simplify checking for certain types of matches.Doesnt this seem like the D standard is kinda oriented towards easy compiler generation and fast compilation ? It isn`t always a good thing... dont get me wrong, i have never coded a compiler so i dont know how hard it is to code this stuff... but this template instantiation syntax pisses me off :/I'm all for making a few tradeoffs for the sake of compiler simplicity, but I think good, convenient generic programming support is one of those things that's worth suffering a few contortions for. -- andy
Dec 02 2003
"Berin Loritsch" <bloritsch d-haven.org> wrote in message news:bqielr$29c4$1 digitaldaemon.com...Is there regexp support in D? It might simplify checking for certain types of matches.import std.regexp; at your service!
Dec 19 2003