In my example, I am creating a class Person and setting a default value for the member "Name".
I have a constructor, and a function called sayHi(). Whenever I try to call that function I get an error:
lua: test.lua:22: attempt to call a nil value (method 'sayHi')
stack traceback:
test.lua:22: in main chunk
[C]: in ?
So from what I understand I am creating a new table, setting the table's metatable (setmetatable) to the Person class, and then returning that table, which means I should be able to use its sayHi() function, but I am getting a nil value.
Person = {}
Person.Name = "No name inputted"
function Person:new(name)
local o = {}
setmetatable(o,self)
self._index = self
return o
end
function Person:sayHi()
print(self.Name .. " says hi")
end
firstPerson = Person:new("Michael")
secondPerson = Person:new("Julian")
print(firstPerson.Name)
secondPerson:sayHi()
The correct property name on metatables is __index (two leading underscores). You're assigning to _index (one leading underscore).
Related
I have a piece of code below that checks that every value of regionId (found under region.hotels.regionId) matches its property value regionid_request.
def response = messageExchange.response.responseContent
def json = new JsonSlurper().parseText(response)
def regionid_request = messageExchange.modelItem.testStep.testCase.testSuite.getPropertyValue("regionid") as Integer
region.hotels.each { hotel ->
assert hotel.regionId == regionid_request
}
I want the code above to perform slightly differently. Instead of saying each regionid needs to match its property value, I just want any of regionid to match its property value. In other words I want to ensure when I have my response, that at least one of the regionIds matches the property value.
What needs to be changed above to match this condition?
Thank you,
Just
assert region.hotels.regionId.contains(regionid_request)
I get unexpected results from the following code snippet using the eclipse ide:
class Example(String s = "init") {
shared String a() => "Func 1";
shared String b = "Attr 1";
shared String c(Integer i) { return "Func 2"; }
}
shared
void run() {
// 1.
print("getAttributes: " + `Example`.getAttributes().string);
print("getMethods: " + `Example`.getMethods().string);
// prints: []
// i.e. doesnt print any attribute or method
// 2.
value e = Example()
type(e); // error
e.type(); // error, should be replaced by the ide with the above version.
}
ad 1.) I get as a result:
getAttributes: []
getMethods: []
where I expected lists containing attributes or methods.
ad 2.) The doc says:
"The type() function will return the closed type of the given instance, which can only be a ClassModel since only classes can be instantiated.
..."
But I cant find the type() function and others related to meta programming, i.e. I dont get a tooltip, but instead I get a runtime (!) error:
Exception in thread "main" com.redhat.ceylon.compiler.java.language.UnresolvedCompilationError: method or attribute does not exist: 'type' in type 'Example'
So, where is the equivalent to the backticks `Example`.... as a function ?
`Example`.getAttributes() returns an empty list because getAttributes takes three type arguments: Container, Get, Set. When called as getAttributes(), the typechecker tries to infer them, but since there’s no information (no arguments with the appropriate type), the inferred type argument is Nothing. Since Nothing is not a container of any of the class’ members, the resulting list is empty. Use getAttributes<>() instead to use the default type arguments, or specify them explicitly (e. g. getAttributes<Example>()). Same thing with getMethods(). Try online
The type function is in ceylon.language.meta and needs to be imported: import ceylon.language.meta { type }. Once you do that (and remove the e.type() line), the compilation error will go away.
If you directly want a meta object of the function, you can write `Example.a`.
I've got a small code snippet that loops through a node and grabs all its properties.
I can get this to work if I set one variable to grab the properties values (except it has a weird [] surrounding it). But I don't want redundant code so I'm trying to set multiple properties inside the loop, except all that returns is a single value, it's not looping around all the nodes.
WORKING
String selectNodeLabel = null
selectNodeLabel = JcrUtils.getChildNodes("links").collect{
it.getProperty("label").getString()
}
SINGLE VALUE
String selectNodeLabel = null
String selectNodeMeta = null
String selectNodeFooter= null
String topicNode = null
topicNode = JcrUtils.getChildNodes("links").collect{
selectNodeLabel = it.getProperty("label").getString()
selectNodeMeta = it.getProperty("meta").getString()
selectNodeFooter = it.getProperty("footer").getString()
}
Thanks for any help!
Try:
def nodeList = JcrUtils.getChildNodes("links").collect{
[ selectNodeLabel : it.getProperty("label").getString()
selectNodeMeta : it.getProperty("meta").getString()
selectNodeFooter : it.getProperty("footer").getString() ]
}
Then, nodeList will be a list of Maps, so you could do:
println nodeList*.selectNodeLabel
To print all the selectNodeLabel values for example.
To explain the problems with your code... Collect creates a list of the elements returned by the closure. What your SINGLE VALUE code is doing is overwriting the values in the selectNode... variables, and then setting topicNode to the value returned from the closure for each element in JcrUtils.getChildNodes("links").
For this case, topicNode will contain a List of it.getProperty("footer").getString() (as it is the last line in the Closure
I'd like to be able to update a previously persisted object for which I have an id without having to retrieve it first. The main thing that I'm trying to avoid is having to copy multiple values into the object's fields when that object has been retrieved from the database. I have these values in a map with keys corresponding to the field names so it's trivial to create the object via a constructor with the map as an argument. Unfortunately, an object created this way results in a new database record when saved even though the id field is set to that of an existing record.
I'm currently using a slight variation on one of the examples shown here for copying Groovy class properties but it's not a very elegant solution for multiple reasons.
Basically I'd like to be able to do something like this:
class Foo {
int a
String b
}
def data = [id: 99, a: 11, b: "bar"] //99 is the id of an existing record
def foo = new Foo(data)
foo.update() //or some other comparable persistence mechanism
Thanks
As long as your map keys have the same name as your object properties, you can use executeUpdate without specifying the individual property names with a closure or function like the following:
def updateString = { obj, map ->
def str = ""
map.each { key, value ->
str += "${obj}.${key}=:${key},"
}
return str[0..-2]
}
def data= [foo:"bar", machoMan:"RandySavage"]
In this case, println updateString("f", data) returns "f.foo=:foo,f.machoMan=:machoMan".
Then you can do this:
Foo.executeUpdate("update Foo f set ${updateString("f", data)}", data)
Or of course you could combine that all together into one closure or function.
You can use the executeUpdate method on the GORM domain class:
Foo.executeUpdate("update Foo f set f.a=:a, f.b=:b where f.id=:id", data)
I am passing in a variable name MetabMapString to the function spectroscopy(). MetabMapString is the name of the field I want to call. But It seems as if Matlab is thiking that MetabMapString is the name of the fields cause it returns that there is no field names MetabMapString. I have included some code. Is there a way to get this to work?
function spectroscopy(MetabMapString)
spect = importdata(spectLCMI);
n = length(spect.MetabMapString);
Row = spect.Row;
Col = spect.Col;
spectOrig = spect.MetabMapString;
...
end
Here is the error
??? Reference to non-existent field 'MetabMapString'.
Error in ==> SpectDraw>spectroscopy at 1165
n = length(spect.MetabMapString);
It should be:
n = length(spect.(MetabMapString));
That should work. Same in all other places where you're using a variable which contains a string as a field name of a struct or MATLAB class, or as a method name of a class:
spectOrig = spect.(MetabMapString);