methods

Passing a method's code as an argument in a typesafe way

旧时模样 提交于 2019-12-07 06:27:08
问题 Passing a method as an argument is not a problem: type TSomething = class Msg: string; procedure Show; end; procedure TSomething.Show; begin ShowMessage(Msg); end; type TProc = procedure of object; procedure Test(Proc: TProc); begin Proc; end; procedure TForm1.Button1Click(Sender: TObject); var Smth: TSomething; begin Smth:= TSomething.Create; Smth.Msg:= 'Hello'; Test(Smth.Show); end; I need something tricky - to pass only a code part of a method. I know I can do it: procedure Test2(Code:

Node readline module doesn't have 'on' function?

痴心易碎 提交于 2019-12-07 06:13:37
问题 I'm trying to create a node app that reads a textfile line by line using the 'readline' module, and prints it to the console. var lineReader = require('readline'); lineReader.createInterface({ input: fs.createReadStream('./testfile') }); lineReader.on('line', function(line){ console.log(line); }); According to the module's documentation, there should be an 'on' method. However when I log the instance of the readline object I created, I don't see an 'on' method anywhere: { createInterface:

static <T extends Number & Comparable<? super Number>>

给你一囗甜甜゛ 提交于 2019-12-07 05:25:48
问题 I have following class with one static method: public class Helper { public static <T extends Number & Comparable<? super Number>> Boolean inRange(T value, T minRange, T maxRange) { // equivalent (value >= minRange && value <= maxRange) if (value.compareTo(minRange) >= 0 && value.compareTo(maxRange) <= 0) return true; else return false; } } I try to call this method: Integer value = 2; Integer min = 3; Integer max = 8; Helper.inRange(value, min, max) ; Netbeans compiler show me this error

Calling static function from instance

孤街醉人 提交于 2019-12-07 05:22:26
问题 I'm trying to call a static magic function ( __callStatic ) from a member of its child class. Problem being, it goes to the non-static __call instead. <?php ini_set("display_errors", true); class a { function __call($method, $params) { echo "instance"; } static function __callStatic($method, $params) { echo "static"; } } class b extends a { function foo() { echo static::bar(); // === echo self::bar(); // === echo a::bar(); // === echo b::bar(); } } $b = new b(); echo phpversion()."<br />"; $b

Force the order of functions in the virtual method table?

被刻印的时光 ゝ 提交于 2019-12-07 05:18:49
问题 How can I control the order of virtual functions in the virtual table? Are they laid out in the same order that they are declared in? When inheriting a class with a virtual table, is the virtual table of the inherited class an extension of the base class, or is an entirely new virtual table created with only the inherited classes virtual functions. (i.e. is the virtual table still at index +0x0 of the class?) 回答1: (a) As far as the standard is concerned, you can't, (in fact you can't even

Java overridable call in constructor

血红的双手。 提交于 2019-12-07 05:15:15
问题 I know it is a bad (security) practice to call overridable methods from an object constructor in Java. However, for example, if the constructor has to initialize some data, it seems reasonable to call the respective setter method so that I don't copy code. The setters are public and not final. Is there any standard way of dealing with this, like declaring private setter methods, that the public ones call? To illustrate, here is some code: class A { private double x,y; private privateSetX

c# regex - select class properties names, methods name and fields from class file (.cs)

走远了吗. 提交于 2019-12-07 05:06:31
问题 I want to match (select from class file) methodsname, properties name and fields name. This is example class: class Perl { string _name; public string Name { get; set; } public Perl() { // Assign this._name this._name = "Perl"; // Assign _name _name = "Sam"; // The two forms reference the same field. Console.WriteLine(this._name); Console.WriteLine(_name); } public static string doSomething(string test) { bla test; } } I got code for the methods: (?:public|private|protected)([\s\w]*)\s+(\w+)

How to identify each parameter type in a C# method?

我与影子孤独终老i 提交于 2019-12-07 04:18:58
问题 I have a C# method say: MyMethod(int num, string name, Color color, MyComplexType complex) Using reflection, how can I distinctly identify each of the parameter types of any method? I want to perform some task by parameter type. If the type is simple int, string or boolean then I do something, if it is Color, XMLDocument, etc I do something else and if it is user defined type like MyComplexType or MyCalci etc then I want to do certain task. I am able to retrieve all the parameters of a method

When to use which - multiple methods, multiple parameters, or an options parameter

南笙酒味 提交于 2019-12-07 04:09:37
问题 This question is coming from a javascript point of view, but it certainly could apply to other languages. I have been running into this more and more lately, and was wondering if there was a best practice, or at least good design standard, for when how to build your methods. The obvious options that I see are as follows, along with a trivial example for each Multiple methods: this.makeGetRequest = function(controller){...} this.makeSynchronousGetRequest = function(controller){...} this

How do you perform a left shift on a circular array of ints?

↘锁芯ラ 提交于 2019-12-07 04:00:28
Is there an existing method that performs a left shift on a circular array of ints? Specifically, given an array with 4 items {1,2,3,4} and a shift amount of 2, I would like a method that shifts the first two letters to the back of the array, making it appear like so: {3,4,1,2} . Would this algorithm work to shift a circular array by one? algShiftByOne(Array) { temp=array[0]; i=1 while(i < Array.length - 1) // Loop from 1 up to array.length == last index { // If there is no exception i assume it copies value from // initial array starting from 1 up to array.length Array[i - 1] = Array[i]; i++;