为什么我在下面的代码中得到" 并非所有代码路径返回值 " VeryBoolToBool()?
public enum VeryBool { VeryTrue, VeryFalse };
public bool VeryBoolToBool(VeryBool veryBool)
{
switch(veryBool)
{
case VeryBool.VeryTrue:
return true;
case VeryBool.VeryFalse:
return false;
// Un-commenting the unreachable(?) default clause will solve this
// default:
// throw new HowTheHellDidIGetHereException();
}
}
Run Code Online (Sandbox Code Playgroud)
编译器不能看到没有其他选项VeryBool吗?
虽然有很多关于COM和STA/MTA的问题(例如这里),但大多数人都在讨论具有UI的应用程序.但是,我有以下设置:
[MTAThread]属性).几个问题:
ConcurrentQueue.我有一个类似于以下的表:
date | expiry
-------------------------
2010-01-01 | 2010-02-01
2010-01-01 | 2010-03-02
2010-01-01 | 2010-04-04
2010-02-01 | 2010-03-01
2010-02-01 | 2010-04-02
Run Code Online (Sandbox Code Playgroud)
在表中,每个日期可能有多个"到期"值.我需要一个返回每个日期中第n个最小到期的查询.例如,对于n = 2,我希望:
date | expiry
-------------------------
2010-01-01 | 2010-03-02
2010-02-01 | 2010-04-02
Run Code Online (Sandbox Code Playgroud)
我的麻烦是AFAIK,没有聚合函数返回第n个最大/最小元素,所以我不能使用'GROUP BY'.更具体地说,如果我有一个神奇的MIN()聚合接受第二个参数'offset',我会写:
SELECT MIN(expiry, 1) FROM table WHERE date IN ('2010-01-01', '2010-02-01') GROUP BY date
Run Code Online (Sandbox Code Playgroud)
有什么建议?
在C#中,是否会Select()用于投影IOrderedEnumerableretain元素的元素顺序?
IEnumerable,而不是IOrderedEnumerable?foreach)?请注意,这个问题是不是重复这一个 -我只有一个Select()条款,没有Distinct().
编辑
是的,它是对象的LINQ.顺便说一句,如果我实际上在查询一些SQL DB,答案会有什么不同吗?
请考虑以下代码:
// In the interrupt handler file:
volatile uint32_t gSampleIndex = 0; // declared 'extern'
void HandleSomeIrq()
{
gSampleIndex++;
}
// In some other file
void Process()
{
uint32_t localSampleIndex = gSampleIndex; // will this be optimized away?
PrevSample = RawSamples[(localSampleIndex + 0) % NUM_RAW_SAMPLE_BUFFERS];
CurrentSample = RawSamples[(localSampleIndex + 1) % NUM_RAW_SAMPLE_BUFFERS];
NextSample = RawSamples[(localSampleIndex + 2) % NUM_RAW_SAMPLE_BUFFERS];
}
Run Code Online (Sandbox Code Playgroud)
我的本意是PrevSample,CurrentSample和NextSample是一致的,即使gSampleIndex调用期间被更新Process().
分配到这个localSampleIndex诀窍,还是有可能它会被优化掉,即使它gSampleIndex是不稳定的?
请考虑以下代码:
// MyKickAssTaskScheduler is a TaskScheduler, IDisposable
using (var scheduler = new MyKickAssTaskScheduler())
{
Task foo = new Task(() => DoSomething());
foo.ContinueWith((_) => DoSomethingElse());
foo.Start(scheduler);
foo.Wait();
}
Run Code Online (Sandbox Code Playgroud)
是否ContinueWith()保证在我的调度程序上运行任务?如果没有,它将使用哪个调度程序?
我想在某个类的每个对象中添加唯一ID(在单个会话中).一种解决方案是使用工厂函数来增加一些静态计数器.一个更简单的解决方案是将此计数器添加到类本身,例如:
class fooWithUniqueId {
public:
fooWithUniqueId() : id(next_id++) {...};
long id;
private:
static long next_id = 0;
}
Run Code Online (Sandbox Code Playgroud)
然而,一个缺陷是该id领域是公开的,并且可以由呼叫者改变,从而违反其独特性.传统的(好吧,至少在我看来)是id私有,并使用getter函数来访问它,因此:
class fooWithUniqueId {
public:
fooWithUniqueId() : id(next_id++) {...};
long getId() const { return id; };
private:
long id;
static long next_id = 0;
}
Run Code Online (Sandbox Code Playgroud)
但我正在考虑采用不同的方法.我可以将id设为const公共类字段:
class fooWithUniqueId {
public:
fooWithUniqueId() : id(next_id++) {...};
const long id;
private:
static long next_id = 0;
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢这种方式,因为getId()每次我需要id时都不需要继续调用,我可以将id用作映射中的键(因为复制构造正确初始化了复制对象的id).我能想到的一个缺点是我不能在fooWithUniqueId对象之间实现赋值,尽管目前我不需要这个功能.
谢谢,波阿斯
假设我有一个定义的类__slots__:
class Foo(object):
__slots__ = ['x']
def __init__(self, x=1):
self.x = x
# will the following work?
def __setattr__(self, key, value):
if key == 'x':
object.__setattr__(self, name, -value) # Haha - let's set to minus x
Run Code Online (Sandbox Code Playgroud)
我可以定义__setattr__()吗?
既然Foo没有__dict__,它会更新什么?
我希望模拟一个具有以下要求的课程:
__init__()方法中定义autospec=True,所以类的API将严格检查调用简化的班级样本:
class MyClass():
id = 0
def __init__(self, x=0.0, y=1.0):
self.x = x
self.y = y
self.id = MyClass._id
MyClass.id +=1
def calc_x_times_y(self):
return self.x*self.y
def calc_x_div_y(self, raise_if_y_not_zero=True):
try:
return self.x/self.y
except ZeroDivisionError:
if raise_if_y_not_zero:
raise ZeroDivisionError
else:
return float('nan')
Run Code Online (Sandbox Code Playgroud)
我需要模拟对象的行为与原始对象一样,就属性而言:
x,y属性但模拟方法调用应该被mock拦截,并验证其调用签名关于这个问题的最佳方法是什么?
编辑
我已经尝试了几种方法,包括继承Mock类,使用attach_mock()和mock_add_spec(),但总是遇到一些死胡同.
我正在使用标准的模拟库.
从派生类记录日志时,我应该从基类继承类记录器实例,还是实例化一个新实例?
即,哪个更好:
public class Base
{
private static Logger _logger = LogManager.GetCurrentClassLogger();
private void Foo()
{
_logger.Info("Hello world");
}
}
public class Derived : Base
{
new private static Logger _logger = LogManager.GetCurrentClassLogger();
private void Fee()
{
_logger.Info("Hello world");
}
}
Run Code Online (Sandbox Code Playgroud)
或者:
public class Base
{
protected static Logger _logger = LogManager.GetCurrentClassLogger();
private void Foo()
{
_logger.Info("Hello world");
}
}
public class Derived : Base
{
private void Fee()
{
_logger.Info("Hello world");
}
}
Run Code Online (Sandbox Code Playgroud) c# ×5
python ×2
aggregate ×1
attributes ×1
c ×1
c++ ×1
com-interop ×1
const ×1
embedded ×1
enums ×1
field ×1
getter ×1
group-by ×1
ienumerable ×1
mocking ×1
mta ×1
mysql ×1
nlog ×1
properties ×1
setattr ×1
slots ×1
sta ×1
subclassing ×1
task ×1
volatile ×1