This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python | |
def printArgs(*args, **kwargs): | |
print kwargs | |
>>> printArgs(a=1, b=2) | |
{'a': 1, 'b': 2} | |
# perl | |
use Data::Dumper; | |
sub printArgs { | |
my %args = @_; | |
print Dumper(\%args); | |
} | |
printArgs(a => 1, b => 2) | |
$VAR1 = { | |
'a' => 1, | |
'b' => 2 | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def printArgs(arg): | |
print args | |
>>> printArgs({'a': 1, 'b': 2}) | |
{'a': 1, 'b': 2} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function printArgs(args) { | |
console.log(args) | |
} | |
> printArgs({'a': 1, 'b': 2}) | |
Object {a: 1, b: 2} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function namedArgsFunc() { | |
var args = {}; | |
for (var i = 0, len = arguments.length; i < len; i += 2) { | |
args[arguments[i]] = arguments[i+1]; | |
} | |
console.log(args); | |
} | |
> namedArgsFunc('a', 1, 'b', 2) | |
Object {a: 1, b: 2} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getNamedArgs(args, opt_start) { | |
opt_start = opt_start ? opt_start : 0; | |
var obj = {}; | |
for (var i = opt_start, len = args.length; i < len; i += 2) { | |
obj[args[i]] = args[i+1]; | |
} | |
return obj; | |
} | |
function namedArgsFunc(x) { | |
var args = getNamedArgs(arguments, 1); | |
console.log('x ->', x); | |
console.log('args ->', args); | |
} | |
> namedArgsFunc('x_variable', 'a', 1, 'b', 2) | |
x -> x_variable | |
args -> Object {a: 1, b: 2} |
But this example was really a fake out. You can actually use this example with a non-scripting language, like Java, since they also allow you to use variable length arguments:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Utils { | |
public static Map<String,Object> getNamedArgs(Object... args) { | |
Map<String,Object> obj = new HashMap<String,Object>(); | |
for (int i = 0, i < args.length; i += 2) { | |
obj.put((String)args[i], args[i+1]); | |
} | |
return obj; | |
} | |
} | |
//... | |
// then you can take any variable argument function and slap this on: | |
public void doSomething(Object... args) { | |
HashMap<String,Object> namedArgs = Utils.getNamedArgs(args); | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testSomething() { | |
MyObj testobj = new MyObj(); | |
testobj.setFoo('a'); | |
testobj.setBar(123); | |
// ... | |
testobj.setBaz('z'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testSomething() { | |
MyObj testobj = TestUtils.getTestObject(MyObj.class, 'foo', 'a', 'bar', 123, 'baz', 'z'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestUtils { | |
public static <T> T getTestObject(Class<T> type, Object... args) { | |
Map<String,Object> namedArgs = Utils.getNamedArgs(args); | |
Constructor <T> constructor = type.getConstructor(); | |
T testobj = constructor.newInstance(); | |
for (Entry<String,Object> entry : namedArgs.getEntrySet()) { | |
Field field = type.getDeclaredField(entry.getKey()); | |
if (field != null) { | |
if (Modifier.isPrivate(field.getModifiers()) { | |
field.setAccessible(true); // this is only for test code | |
} | |
field.set(testobj, entry.getValue()); | |
} | |
} | |
return testobj; | |
} | |
} |
No comments:
Post a Comment