metaprogramming

Get class by name

99封情书 提交于 2021-01-27 21:11:24
问题 Can I get a class by its name? For example: class Foo { } class Bar { } let x = "Foo" classByString(x) // need to return Foo I want to use metaprogramming to reduce code maintenance. 回答1: You can use a NSClassFromString : if let anyObj : AnyObject.Type = NSClassFromString("MyAppName.MySwiftClassFoo") { //call Foo } else { //call Bar } 来源: https://stackoverflow.com/questions/42081970/get-class-by-name

Reimplemented Constructor[Symbol.hasInstance] but it still won't be called

青春壹個敷衍的年華 提交于 2021-01-27 17:02:20
问题 So, I was writing some example code implementing another function for Constructor[Symbol.hasInstance] and I noticed my new implementation just won't get called. The script below is what I expected to happen: function Pirate(name) { this.name = name; } const jackSparrow = { isPirate: true }; // Notice how `jackSparrow` is not yet considered an instance of the `Pirate` object console.log(jackSparrow instanceof Pirate); // false // Now let's assign another function for `Pirate[Symbol.hasInstance

Reimplemented Constructor[Symbol.hasInstance] but it still won't be called

ε祈祈猫儿з 提交于 2021-01-27 16:57:51
问题 So, I was writing some example code implementing another function for Constructor[Symbol.hasInstance] and I noticed my new implementation just won't get called. The script below is what I expected to happen: function Pirate(name) { this.name = name; } const jackSparrow = { isPirate: true }; // Notice how `jackSparrow` is not yet considered an instance of the `Pirate` object console.log(jackSparrow instanceof Pirate); // false // Now let's assign another function for `Pirate[Symbol.hasInstance

Python 3 super and metaprogramming

北城以北 提交于 2021-01-27 16:16:44
问题 I'm trying to duplicate and then modify a class programmatically but I'm running into problems with python 3's magic super for example the following class Base(): def __init__(self): print("Base init") class Orginal(Base): def __init__(self): super().__init__() print("Orginal init") Modified = type(Orginal.__name__, Orginal.__bases__, dict(Orginal.__dict__)) Modified.f = lambda self: print("f") m = Modified() raises TypeError: super(type, obj): obj must be an instance or subtype of type So I

Python 3 super and metaprogramming

China☆狼群 提交于 2021-01-27 16:11:08
问题 I'm trying to duplicate and then modify a class programmatically but I'm running into problems with python 3's magic super for example the following class Base(): def __init__(self): print("Base init") class Orginal(Base): def __init__(self): super().__init__() print("Orginal init") Modified = type(Orginal.__name__, Orginal.__bases__, dict(Orginal.__dict__)) Modified.f = lambda self: print("f") m = Modified() raises TypeError: super(type, obj): obj must be an instance or subtype of type So I

Python 3 super and metaprogramming

喜夏-厌秋 提交于 2021-01-27 15:45:57
问题 I'm trying to duplicate and then modify a class programmatically but I'm running into problems with python 3's magic super for example the following class Base(): def __init__(self): print("Base init") class Orginal(Base): def __init__(self): super().__init__() print("Orginal init") Modified = type(Orginal.__name__, Orginal.__bases__, dict(Orginal.__dict__)) Modified.f = lambda self: print("f") m = Modified() raises TypeError: super(type, obj): obj must be an instance or subtype of type So I

Deparse, substitute with three-dots arguments

天涯浪子 提交于 2021-01-21 09:13:36
问题 Let consider a typical deparse(substitute( R call: f1 <-function(u,x,y) {print(deparse(substitute(x)))} varU='vu' varX='vx' varY='vy' f1(u=varU,x=varX,y=varY) That results in [1] "varX" which is what we expect and we want. Then, comes the trouble, I try to get a similar behaviour using the ... arguments i.e. f2 <- function(...) { l <- list(...) x=l$x print(deparse(substitute(x))) ### this cannot work but I would like something like that } That, not surprisingly, does not work : f2(u=varU,x

Deparse, substitute with three-dots arguments

谁都会走 提交于 2021-01-21 09:12:35
问题 Let consider a typical deparse(substitute( R call: f1 <-function(u,x,y) {print(deparse(substitute(x)))} varU='vu' varX='vx' varY='vy' f1(u=varU,x=varX,y=varY) That results in [1] "varX" which is what we expect and we want. Then, comes the trouble, I try to get a similar behaviour using the ... arguments i.e. f2 <- function(...) { l <- list(...) x=l$x print(deparse(substitute(x))) ### this cannot work but I would like something like that } That, not surprisingly, does not work : f2(u=varU,x

Wrap function implementations returning a specific type into another function programatically

北战南征 提交于 2020-12-29 08:19:07
问题 I would like to wrap all the user defined functions in a scala project that return a certain type T , into a function that accepts a T and the function name as parameters. eg. given this function is in scope: def withMetrics[T](functionName: String)(f: => Try[T]): Try[T] = { f match { case _: Success[T] => println(s"send metric: success for $functionName") case _: Failure[T] => println(s"send metric: failure for $functionName") } f } the user can send metrics for their functions which return

Wrap function implementations returning a specific type into another function programatically

℡╲_俬逩灬. 提交于 2020-12-29 08:11:29
问题 I would like to wrap all the user defined functions in a scala project that return a certain type T , into a function that accepts a T and the function name as parameters. eg. given this function is in scope: def withMetrics[T](functionName: String)(f: => Try[T]): Try[T] = { f match { case _: Success[T] => println(s"send metric: success for $functionName") case _: Failure[T] => println(s"send metric: failure for $functionName") } f } the user can send metrics for their functions which return