constraints

Restricting a generic type parameters to have a specific constructor

∥☆過路亽.° 提交于 2019-11-26 14:09:21
问题 I'd like to know why the new constraint on a generic type parameter can only be applied without parameters, that is, one may constraint the type to have the parameterless constructor, but one cannot constraint the class to have, say, a constructor that receives a int as a parameter. I know ways around this, using reflection or the factory pattern, that works fine, ok. But I'd really like to know why, because I've been thinking about it and I really can't think of a difference between a

constrained optimization in R

穿精又带淫゛_ 提交于 2019-11-26 14:08:04
问题 I am trying to use http://rss.acs.unt.edu/Rdoc/library/stats/html/constrOptim.html in R to do optimization in R with some given linear constraints but not able to figure out how to set up the problem. For example, I need to maximize $f(x,y) = log(x) + \frac{x^2}{y^2}$ subject to constraints $g_1(x,y) = x+y < 1$, $g_2(x,y) = x > 0$ and $g_3(x,y) = y > 0$. How do I do this in R? This is just a hypothetical example. Do not worry about its structure, instead I am interested to know how to set

Template Constraints C++

半世苍凉 提交于 2019-11-26 13:00:42
In C# we can define a generic type that imposes constraints on the types that can be used as the generic parameter. The following example illustrates the usage of generic constraints: interface IFoo { } class Foo<T> where T : IFoo { } class Bar : IFoo { } class Simpson { } class Program { static void Main(string[] args) { Foo<Bar> a = new Foo<Bar>(); Foo<Simpson> b = new Foo<Simpson>(); // error CS0309 } } Is there a way we can impose constraints for template parameters in C++. C++0x has native support for this but I am talking about current standard C++. luke As someone else has mentioned, C+

Creating layout constraints programmatically

一世执手 提交于 2019-11-26 12:48:01
I know that a lot people already asked tons of questions about this, but even with the answers I can't make it work. When I'm dealing with constraints on storyboard, it's easy but in code I have a hard time. I try, for example, to have a view that stays on the right side and has the height of the screen according the screen orientation. This is my code: UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 748)]; myView.backgroundColor = [UIColor redColor]; [self.view addSubview:myView]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[myView(>

Turn off constraints temporarily (MS SQL)

落爺英雄遲暮 提交于 2019-11-26 12:40:51
I'm looking for a way to temporarily turn off all DB's constraints (eg table relationships). I need to copy (using INSERTs) one DB's tables to another DB. I know I can achieve that by executing commands in proper order (to not break relationships). But it would be easier if I could turn off checking constraints temporarily and turn it back on after the operation's finish. Is this possible? gbn You can disable FK and CHECK constraints only in SQL 2005+ . See ALTER TABLE ALTER TABLE foo NOCHECK CONSTRAINT ALL or ALTER TABLE foo NOCHECK CONSTRAINT CK_foo_column Primary keys and unique constraints

Generic constraints, where T : struct and where T : class

我的梦境 提交于 2019-11-26 12:24:58
I would like to differentiate between following cases: A plain value type (e.g. int ) A nullable value type (e.g. int? ) A reference type (e.g. string ) - optionally, I would not care if this mapped to (1) or (2) above I have come up with the following code, which works fine for cases (1) and (2): static void Foo<T>(T a) where T : struct { } // 1 static void Foo<T>(T? a) where T : struct { } // 2 However, if I try to detect case (3) like this, it does not compile: static void Foo<T>(T a) where T : class { } // 3 The error message is Type 'X' already defines a member called 'Foo' with the same

trying to animate a constraint in swift

試著忘記壹切 提交于 2019-11-26 11:48:17
问题 I have an UITextField that I want to enlarge it\'s width when tapped on. I set up the constraints and made sure the constraint on the left has the lower priority then the one that I am trying to animate on the right side. Here is the code that I am trying to use. // move the input box UIView.animateWithDuration(10.5, animations: { self.nameInputConstraint.constant = 8 }, completion: { (value: Bool) in println(\">>> move const\") }) This works, but it seems to just happen instantly and there

SQL Server 2005 How Create a Unique Constraint?

时间秒杀一切 提交于 2019-11-26 11:36:51
How do I create a unique constraint on an existing table in SQL Server 2005? I am looking for both the TSQL and how to do it in the Database Diagram. The SQL command is: ALTER TABLE <tablename> ADD CONSTRAINT <constraintname> UNIQUE NONCLUSTERED ( <columnname> ) See the full syntax here . If you want to do it from a Database Diagram: right-click on the table and select 'Indexes/Keys' click the Add button to add a new index enter the necessary info in the Properties on the right hand side: the columns you want (click the ellipsis button to select) set Is Unique to Yes give it an appropriate

Can you use “where” to require an attribute in c#?

百般思念 提交于 2019-11-26 11:20:29
问题 I want to make a generic class that accepts only serializable classes, can it be done with the where constraint? The concept I\'m looking for is this: public class MyClass<T> where T : //[is serializable/has the serializable attribute] 回答1: Nope, I'm afraid not. The only things you can do with constraints are: where T : class - T must be a reference type where T : struct - T must be a non-nullable value type where T : SomeClass - T must be SomeClass or derive from it where T : ISomeInterface

Continuing a transaction after primary key violation error

笑着哭i 提交于 2019-11-26 11:18:24
问题 I am doing a bulk insert of records into a database from a log file. Occasionally (~1 row out of every thousand) one of the rows violates the primary key and causes the transaction to fail. Currently, the user has to manually go through the file that caused the failure and remove the offending row before attempting to re-import. Given that there are hundreds of these files to import it is impractical. My question: How can I skip the insertion of records that will violate the primary key