digitalmars.D.learn - how to test (at compilation-time) for existence of an already-declared
- someone (79/79) Jul 23 2021 Suppose I have a module with the following:
- Adam D Ruppe (9/10) Jul 23 2021 static if(is(classTickerCustomNYSE))
- someone (9/19) Jul 23 2021 is() ... faaan-tas-tic :)
Suppose I have a module with the following: ```d public class classTickerCustomNYSE : classTickerCommon { ... } public class classTickerCustomNASDAQ : classTickerCommon { ... } ``` ... and given that I also have the following in the same module: ```d public enum structureExchanges = [ r"NYSE"d : structureExchange(r"NYSE"d, r"New York Stock Exchange"d, r"USD"d, r"usa"d, r"New York"d, r"EST"d), r"NASDAQ"d : structureExchange(r"NASDAQ"d, r"National Association of Securities Dealers Automated Quotations"d, r"USD"d, r"usa"d, r"New York"d, r"EST"d), r"LSE"d : structureExchange(r"LSE"d, r"London Stock Exchange"d, r"GBP"d, r"gbr"d, r"London"d, r"UTC"d), r"XETRA"d : structureExchange(r"XETRA"d, r"Deutsche Börse"d, r"EUR"d, r"deu"d, r"Frankfurt am Main"d, r"CET"d), r"B3"d : structureExchange(r"B3"d, r"B3 formerly Bolsa de Valores de São Paulo (aka BOVESPA)"d, r"BRL"d, r"bra"d, r"São Paulo"d, r"BRT"d), r"BCBA"d : structureExchange(r"BCBA"d, r"Bolsa de Comercio de Buenos Aires"d, r"ARS"d, r"arg"d, r"Buenos Aires"d, r"ART"d) ]; public struct structureExchange { /// solely‐intended to help build code at compilation‐time; for client‐code classExchanges should be used instead public dstring ID; public dstring name; public dstring currencyID; public typeLocation location; safe this( const dstring lstrExchangeID, const dstring lstrExchangeName, const dstring lstrCurrencyID, const dstring lstrCountryID, const dstring lstrCity, const dstring lstrTZ ) { ID = lstrExchangeID; name = lstrExchangeName; currencyID = lstrCurrencyID; location = typeLocation( lstrCountryID, lstrCity, lstrTZ) ; } } ``` ... at compilation-time I can do things like: ```d static foreach (structureExchange sudtExchange; structureExchanges) { /// assume the following code as a template to build new classTickerCustom{ExchangeID} classes mixin(format! ` /// code chunk final public class classTickerCustom%1$s : classTickerCommon { ... } `(sudtExchange.ID) /// code chunk ); /// mixin ending } ``` ... now, question is: how can I check, within the static foreach code, whether a class is already-implemented or not; eg: ```d static foreach (structureExchange sudtExchange; structureExchanges) { if (! ? == r"classTickerCustom"d ~ sudtExchange.ID) { /// class is not implemented yet: do whatever here ... } } ``` ... my first thought was traits (https://dlang.org/spec/traits.html) but I don't see anything useful there; there is getMember() but the example is for already-declared objects within the module, and not the module itself.
Jul 23 2021
On Saturday, 24 July 2021 at 01:29:26 UTC, someone wrote:Suppose I have a module with the following:static if(is(classTickerCustomNYSE)) // it was a valid type else // it was not a valid type can use that to test. you can also mix in the name static if(is(mixin("class name here"))) {} There's similar tricks for testing for imports and such too but i recommend here you import the module first then can test it.
Jul 23 2021
On Saturday, 24 July 2021 at 01:44:11 UTC, Adam D Ruppe wrote:On Saturday, 24 July 2021 at 01:29:26 UTC, someone wrote:is() ... faaan-tas-tic :) I completely forgot about the is operator and went looking all over the place for something like that. Man ... leaving templates aside for a moment, conditional compilation alongside mixin is pretty powerful in D ! I can't believe how flexible code I am writing right now; and I am only a beginner ... Thanks for the tip Adam !Suppose I have a module with the following:static if(is(classTickerCustomNYSE)) // it was a valid type else // it was not a valid type can use that to test. you can also mix in the name static if(is(mixin("class name here"))) {} There's similar tricks for testing for imports and such too but i recommend here you import the module first then can test it.
Jul 23 2021