digitalmars.D.learn - UFCS from within classes
- Gyron (28/28) Sep 09 2013 Hey there, I've experimented a little with UFCS today and ran
- H. S. Teoh (19/53) Sep 09 2013 I don't think UFCS works with qualified names right now. This is a known
- Gyron (7/69) Sep 09 2013 The thing is, that I already have other classes in that module
- Jacob Carlborg (4/6) Sep 09 2013 As far as I know it's a design decision.
- Maxim Fomin (16/44) Sep 09 2013 It is not a bug, but a feature - see Decimal Literal Types table
Hey there, I've experimented a little with UFCS today and ran into a problem. My first question, which is kinda off-topic: Why does D use the int type if you give it a number started with 0x(hex), shouldn't it use uint for that ? Here comes the real question: I've extended the int by one function, which is the following (just to represent the problem): public static T read(T)(int address) { return cast(T)1; } It works perfectly if the function stands alone (is global), but it doesn't work if I put it into a class (because I want it to be a bit more organized) like that: class CMemory { public static T read(T)(int address) { return cast(T)1; } } I'm not able to write something like: 0x1212.CMemory.read!bool(); So the question is, how can I make it to be able to be used like this: 0x1212.read!bool(); but still organized within the class ?
Sep 09 2013
On Mon, Sep 09, 2013 at 07:07:58PM +0200, Gyron wrote:Hey there, I've experimented a little with UFCS today and ran into a problem. My first question, which is kinda off-topic: Why does D use the int type if you give it a number started with 0x(hex), shouldn't it use uint for that ?Good point, please file a bug on: http://d.puremagic.com/issuesHere comes the real question: I've extended the int by one function, which is the following (just to represent the problem): public static T read(T)(int address) { return cast(T)1; } It works perfectly if the function stands alone (is global), but it doesn't work if I put it into a class (because I want it to be a bit more organized) like that: class CMemory { public static T read(T)(int address) { return cast(T)1; } } I'm not able to write something like: 0x1212.CMemory.read!bool(); So the question is, how can I make it to be able to be used like this: 0x1212.read!bool(); but still organized within the class ?I don't think UFCS works with qualified names right now. This is a known issue. The best way to solve this problem is to put your function in a separate module instead of a class, then importing the module will pull it into your current namespace and you can use it as above, yet have it organized by module (but not by class -- that's unfortunately not possible right now). Something like this: ----memory.d---- module memory; T read(T)(int address) { ... } ----main.d---- import memory; void main() { 0x1212.read!bool(); } T -- Shin: (n.) A device for finding furniture in the dark.
Sep 09 2013
On Monday, 9 September 2013 at 17:17:07 UTC, H. S. Teoh wrote:On Mon, Sep 09, 2013 at 07:07:58PM +0200, Gyron wrote:The thing is, that I already have other classes in that module and I hate to mix global functions(global in the means of global in the module) with classes. I would separate them in different files, but sadly thats not possible (as far as I can see, because you can only define the module once, not like namespaces in c++).Hey there, I've experimented a little with UFCS today and ran into a problem. My first question, which is kinda off-topic: Why does D use the int type if you give it a number started with 0x(hex), shouldn't it use uint for that ?Good point, please file a bug on: http://d.puremagic.com/issuesHere comes the real question: I've extended the int by one function, which is the following (just to represent the problem): public static T read(T)(int address) { return cast(T)1; } It works perfectly if the function stands alone (is global), but it doesn't work if I put it into a class (because I want it to be a bit more organized) like that: class CMemory { public static T read(T)(int address) { return cast(T)1; } } I'm not able to write something like: 0x1212.CMemory.read!bool(); So the question is, how can I make it to be able to be used like this: 0x1212.read!bool(); but still organized within the class ?I don't think UFCS works with qualified names right now. This is a known issue. The best way to solve this problem is to put your function in a separate module instead of a class, then importing the module will pull it into your current namespace and you can use it as above, yet have it organized by module (but not by class -- that's unfortunately not possible right now). Something like this: ----memory.d---- module memory; T read(T)(int address) { ... } ----main.d---- import memory; void main() { 0x1212.read!bool(); } T
Sep 09 2013
On 2013-09-09 19:15, H. S. Teoh wrote:I don't think UFCS works with qualified names right now. This is a known issue.As far as I know it's a design decision. -- /Jacob Carlborg
Sep 09 2013
On Monday, 9 September 2013 at 17:07:59 UTC, Gyron wrote:Hey there, I've experimented a little with UFCS today and ran into a problem. My first question, which is kinda off-topic: Why does D use the int type if you give it a number started with 0x(hex), shouldn't it use uint for that ?It is not a bug, but a feature - see Decimal Literal Types table at http://dlang.org/lex.htmlHere comes the real question: I've extended the int by one function, which is the following (just to represent the problem): public static T read(T)(int address) { return cast(T)1; } It works perfectly if the function stands alone (is global), but it doesn't work if I put it into a class (because I want it to be a bit more organized) like that: class CMemory { public static T read(T)(int address) { return cast(T)1; } } I'm not able to write something like: 0x1212.CMemory.read!bool(); So the question is, how can I make it to be able to be used like this: 0x1212.read!bool(); but still organized within the class ?class CMemory { public static T read(T)(int address) { return cast(T)1; } } alias CMemory.read!int CMread; void main() { 0.CMread(); } You can also use alias CMemory.read CMread; plus 0.CMread!int();
Sep 09 2013