Why this string to enum converter macro fails to compile? play
import std/macros, std/strutils
macro autoconvert(TT: type[enum]) =
let fname = "to" & $(TT)
quote do:
converter `fname`*(s: string): `TT` = parse_enum[`TT`](s)
type Tag* = enum movies, books
autoconvert Tag
Error
play.nim(12, 13) template/generic instantiation of `autoconvert` from here
play.nim(7, 3) Error: identifier expected, but found '"toTag"'
UPDATE:
The solution is to use ident "to" & $(TT), but why it doesn't work with just string or newLit?
You're using a string literal as the procedure name instead of an identifer ident("to" & $(TT)) solves the issue, but you're also using a macro instead of a template. The reason it doesnt work with a lit is that a literal is "toTag" whereas an identifer is toTag. I suggest looking at the treeRepr and repr of the result to see what is happening. The following is the afformentioned template way.
import std/strutils
template autoConvert*(TT: type[enum]) {.dirty.} =
converter toTag*(s: string): TT = parse_enum[TT](s)
type Tag* = enum movies, books
autoconvert Tag
Related
I'm trying to match an obtained property from one class with another class's same property at run time. After some research, I found out that there is such thing as "Reflection" in .net but Im using just VBA. Just for background: I'm automating an application using this code, so some objects are exposed, while others are not.
The way I'm currently doing it is using a property of obtained class "Description", and use that to search for the same property at the targeted class.
Set TargetVar = hySetOperations.Item(j).TargetVariable 'This is a RealVariable a property that refers to a class
Set SourceObj =hySetOperations.Item(j).SourceObject 'This is also a RealVariable
'In order to import variable from source object, we r gonna use TargetVariable description and truncate space, and use it (This might not work if description
'is different than actual name of the variable)
Dim RealVarString As String
RealVarString = TargetVar.Description
'Trim spaces
RealVarString = Replace (RealVarString, " ", "")
Set SourceVar = CallByName ( SourceObj, RealVarString, vbGet)
This actually works for most of the cases since "description" is usually the same as property's name, but with spaces. However, in some cases, this is not the case, in which things go south.
I have a hard time thinking about a descriptive title, so sorry for that.
I'm facing an issue here. I have a lot of code files that I have to manipulate. And I have to find some indexes in order to get a substring.
Here is an example:
public BigInteger getGenbestillingerTilbageKvantitet() {
return genbestillingerTilbageKvantitet;
}
I have to be able to locate the starting index of the above example (I have the entire code file, loaded as a string, so the method shown would be a substring of the entire code file). I'm able to get the index of getGenbestillingerTilbageKvantitet(), because I have "GenbestillingerTilbageKvantitet" in an array (along with others):
int index7 = fileContent.indexOf("get" + capitalizedProps[i])
This is how I do it.
Then I would find the end of the method like this:
int index4 = fileContent.indexOf("}", index3) + 1
The issue is that i need the index of "public". But since the type is not nessecarily "BigInteger" it gives me some trouble.
Does anyone know how to get the index from public? If the type were of a custom type, I could do:
String s = "public " + capitalizedProps[i] + "Type get" + capitalizedProps[i]
int index3 = fileContent.indexOf(s)
Which works sometimes :) But not when the type is BigInteger or even String.
If you want to get the index of the public keyword preceding getGenbestillingerTilbageKvantitet method name then you can use String.lastIndexOf(str) of the substring created from the beginning up to index of getGenbestillingerTilbageKvantitet method name. Something like this:
int pubKeyIdx = fileContent.substring(0, index7).lastIndexOf("public")
where index7 variable holds the index of the method name.
I'm trying to use a string variable as input to an xml function. When I use this command:
name2_node(i).setTextContent('truck');
there is no error. But when I replace it with:
name2_node(i).setTextContent(type(i,1));
an error occurs like this:
No method 'setTextContent' with matching signature found for class
'org.apache.xerces.dom.ElementImpl'.
The variable type is a string array. In fact when I type type(i,1) in command window the result is:
ans =
string
"truck"
What part am I doing wrong?
Two things:
use a different variable name, type is a built in function which tells you the variable type, hence why it shows "string" in the output.
Then access the cell array of strings with curly braces
vehicletypes = {'car'; 'truck'; 'van'};
name2_node(i).setTextContent(vehicletypes{i,1}); % For i=2, this passes 'truck'
I have an interesting problem in Groovy. Hope someone can help me.
Basically I am using Groovy Sql. I need to select multiple tables in different databases. My second query depends on other query's, for example: "select * from table1-in-db1 where key = ${result-of-other-query1}. Groovy works fine if I hardcode this in the function. However, my problem is the sql is defined in a xml file and after I retriev passed in into the the funciton as a string. it doesn't interperate the inline varialbe anymore, even I do have a variable called result-of-other-query1 in the scope.
Here is a piece of sudo code:
doQuery(String squery, String tquery) {
//query db1.
//squery = "select key1 from table1"
db1.rows(squery).each {row->
//query db2.
//tquery ="select * where myKey ='${row.key1}'"
println tquery
tdb.rows(tquery).each { row1 ->
.... // get error here, coz groovy did not replace ${row.key1}
}
}
}
Is there any way that I can tell Groovy to replace the inline variable even it is a passed in as a string?
Thanks a lot for your help in advance
Try
tquery = 'select * where myKey =:foo'
tdb.rows(tquery,[foo:"$row.key1"]).each
you may also want to consider using eachRow as opposed to rows.(query).each
I think what you need is the simple template engine. Sorry I am on my phone so can't give you an example. ..
OK - here is an example of what I mean. I was talking about the SimpleTemplateEngine (http://groovy.codehaus.org/api/groovy/text/SimpleTemplateEngine.html).
If you load a string from a file it will not be a gstring, it will be a String, but you can use the SimpleTemplateEngine to do a GString type substitution e.g.:
def clause='test'
String testString='this is a ${clause}'
println "As a string: " + testString
// Get an instance of the template engine
def engine = new groovy.text.SimpleTemplateEngine()
def template = engine.createTemplate(testString).make([clause:clause])
println template.toString()
How can I create a nested structure in VBA? When I try the following code in VB Editor it is saying "Statement invalid inside type block".
Type Functiondetails
Function As String
Sites(7) As String
Values(7) As Integer
End Type
Type Financialdetails
Metrics As String
Dim f(10) As Functiondetials
End Type
Remove the Dim from the 2nd Type (and spell Functiondetails correctly!);
Type Financialdetails
Metrics As String
f(10) As Functiondetails
End Type
You should be ok but it also may not like Function as an identifier.