In Python I can make any class support indexing by overriding __getitem__ like so:
class Test:
def __getitem__(self, key):
return self.data[key]
Does Dart have a similar construct for this?
Assuming that the __getitem__ thing lets you use the "indexing" syntax (object[index]), yes, Dart lets you do the same by defining operator []. Example:
class Test {
var data = {
"a": 1,
"b": 2
};
operator [](index) => data[index];
}
main() {
var t = new Test();
print(t["a"]);
print(t["b"]);
}
You can also define the "opposite" operator []=:
class Test {
Map data = {
"a": 1,
"b": 2
};
operator [](index) => data[index];
operator []=(index, value) { data[index] = value; }
}
main() {
var t = new Test();
print(t["a"]);
print(t["b"]);
t["c"] = 3;
print(t["c"]);
}
Related
I get an object via some 3rd party api. I use a wrapper function to get it and then return a map from its properties:
wrapperFunc() {
def myObj = someapi.getblah().getSomeObect()
return [
aaa: myObj.aaa,
bbb: myObj.bbb,
ccc: myObj.ccc
]
}
Now I could manually go through EVERY property in the object like this, but is there an elegant groovy feature to dynamically build a map from the object's properties?
You could do something like this:
class Widget {
int width
int height
static void main(args) {
def obj = new Widget(width: 7, height: 9)
List<MetaProperty> metaProperties = obj.metaClass.properties
def props = [:]
for(MetaProperty mp : metaProperties) {
props[mp.name] = mp.getProperty(obj)
}
// props will look like [width:7, class:class demo.Widget, height:9]
}
}
This is basically a variant of #jeff-scott-brown's answer.
First, create a class that contains the Object-to-Map logic that uses the Groovy MetaClass to access a type's properties. findAll filters out the "class" property, which I assume you don't care about. The collectEntries line transforms each MetaProperty object into a Map entry.
class ElegantGroovyFeature {
static Map asType(Object o, Class m) {
if (m == Map) {
o.metaClass.properties
.findAll { it.getSetter() != null }
.collectEntries { prop -> [prop.name, prop.getProperty(o)] }
} else {
o.asType(m)
}
}
}
The extension class overrides the asType method, which corresponds to the as operator, enabling you to convert arbitrary objects to Maps using obj as Map expressions:
def obj = someapi.getBlah().getSomeObject()
use (ElegantGroovyFeature) {
def mapOfProperties = obj as Map
}
const obj = { foo: 'bar', baz: 42 };
const map = new Map(Object.entries(obj));
console.log(map); // Map { foo: "bar", baenter code herez: 42 }
Is it possible to mock getter and setter of the property by Mockito? Something like this:
#Test
fun three() {
val m = mock<Ddd>() {
// on { getQq() }.doReturn("mocked!")
}
assertEquals("mocked!", m.qq)
}
open class Ddd {
var qq : String = "start"
set(value) {
field = value + " by setter"
}
get() {
return field + " by getter"
}
}
To mock getter just write:
val m = mock<Ddd>()
`when`(m.qq).thenReturn("42")
also i suggest to use mockito-kotlin, to use useful extensions and functions like whenever:
val m = mock<Ddd>()
whenever(m.qq).thenReturn("42")
Complementing IRus' answer, you could also use the following syntax:
val mockedObj = mock<SomeClass> {
on { funA() } doReturn "valA"
on { funB() } doReturn "valB"
}
or
val mockedObj = mock<SomeClass> {
on(it.funA()).thenReturn("valA")
on(it.funB()).thenReturn("valB")
}
The code says that it is perfectly fine and that there are no errors, but when I go to run the simulator, the words will include:
(Swift.LazyMapCollection < Swift.Dictionary < Swift.String, Swift.String > (_base:[ ]
I am trying to create a quote app that displays a quote.
Here is the code for the Import of the Plist:
import Foundation
struct ImportList {
let path: String
init(FileName: String) {
self.path = NSBundle.mainBundle().pathForResource("\(FileName)", ofType:"plist")!
}
var dict: Dictionary<String, String> {
return NSDictionary(contentsOfFile: path)! as! Dictionary
}
var array: Array<AnyObject> {
return [String](arrayLiteral: String(dict.keys) { $0 as String})
}
func count() -> Int {
return array.count
}
}
Thank you.
Don't use arrayLiteral in this case, just use Array():
var array: Array<AnyObject> {
return Array(dict.keys)
}
It safely converts the lazy collection to an actual array.
Let's say that I have a collection of parameters
def params = ['a','b','c']
Is there a short way to run a method that accepts a single parameter once for every element of a collection to replace this:
params.each {
foo(it)
}
with something more declarative (like a "reverse" spread operator)?
You can use collect:
def params = ['a','b','c']
def foo(param) {
'foo-' + param
}
assert ['foo-a', 'foo-b', 'foo-c'] == params.collect { foo(it) }
Or just a closure
def foo = { a -> a + 2 }
def modified = list.collect foo
You can use method pointer:
def l = [1,2,3]
l.each(new A().&lol)
class A {
def lol(l) {
println l
}
}
Or add a method that will do the task you need:
def l = [1,2,3]
List.metaClass.all = { c ->
delegate.collect(c)
}
l.all(new A().&lol)
class A {
def lol(l) {
println l
return l+2
}
}
I want to do alot of stuff with each of those example strings and return Object of some other type here Integers, later some bigger class-objects.
Here in this example I am trying something simple, how ever I get a completly wrong result.
At least for what i was hoping to get back. xD
I hoped to get: [6, 5, 6, 5]
but instead I get: [butter, bread, dragon, table]
package test
#Grab(group='org.codehaus.gpars', module='gpars', version='1.0.0')
import static groovyx.gpars.GParsPool.withPool
class Test {
List<String> strings = new ArrayList<String>([
"butter",
"bread",
"dragon",
"table"
])
def closure = { it.length() }
def doStuff() {
def results = withPool( 4 ) {
strings.eachParallel{ it.length()}
}
println results
}
static main(args) {
def test = new Test()
test.doStuff()
}
}
It would be nice if the answer could have a short explanation.
Thanks a lot!
In groovy, each (and eachParallel in GPars) returns the original collection.
What you want is collect (to return the new collection made by calling the closure)
So, change
strings.eachParallel { it.length() }
to
strings.collectParallel { it.length() }
(btw)
GPars now comes bundled with Groovy so you shouldn't need the #Grab, and I assume you meant to use your closure variable in the collect?
package test
import static groovyx.gpars.GParsPool.withPool
class Test {
List<String> strings = [ "butter", "bread", "dragon", "table" ]
def closure = { it.length() }
def doStuff() {
def results = withPool( 4 ) {
strings.collectParallel closure
}
println results
}
static main( args ) {
def test = new Test()
test.doStuff()
}
}