说我有以下功能:
infixr 0 <|
{-# INLINE (<|) #-}
(<|) :: (a -> b) -> a -> b
f <| x = f x
foo :: a -> (forall b. b -> b) -> a
foo x f = f x
Run Code Online (Sandbox Code Playgroud)
以下不进行类型检查:
ghci> foo 3 <| id
Couldn't match expected type `forall b. b -> b'
with actual type `a0 -> a0'
In the second argument of `(<|)', namely `id'
In the expression: f 3 <| id
In an equation for …
Run Code Online (Sandbox Code Playgroud) C#没有暴露哪些IL指令?
我指的是像sizeof和cpblk这样的指令 - 没有执行这些指令的类或命令(C#中的sizeof是在编译时计算的,而不是在运行时AFAIK计算的).
其他?
编辑:我问这个的原因(并希望这将使我的问题更有效)是因为我正在开发一个小型库,它将提供这些指令的功能.sizeof和cpblk已经实现 - 我想知道在继续之前我可能错过了什么.
编辑2:使用Eric的答案,我编写了一份说明列表:
列表中没有包含许多其他指令,我将它们分开,因为它们基本上是其他指令的快捷方式(压缩以节省时间和空间):
我正在浏览我的一个程序集的某些IL(通过ILDasm),我注意到我的所有方法都以一条 指令开头nop
.
有谁知道那是为什么?
我定义了两个接口:
// IVector.cs
public interface IVector
{
int Size { get; }
float this[int index] { get; set; }
}
// IMatrix.cs
public interface IMatrix
{
int Size { get; }
float this[int row, int column] { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
以及这些接口的扩展方法
// VectorExtensions.cs
public static T Add<T>(this T vector, T value) where T : struct, IVector
{
var output = default(T);
for (int i = 0; i < output.Size; i++)
output[i] = vector[i] + value[i];
return output;
}
// …
Run Code Online (Sandbox Code Playgroud) 我正在写一个着色器(HLSL),我需要将颜色值打包成R32格式.我发现了将浮动数据包装成R8G8B8A8格式的各种代码,但它们似乎都没有反过来.我的目标是SM3.0,因此(afaik)位操作不是一种选择.
总结一下,我需要能够做到这一点:
float4 color = ...; // Where color ranges from 0 -> 1
float packedValue = pack(color);
Run Code Online (Sandbox Code Playgroud)
有人知道怎么做吗?
更新
我已经取得了一些进展......也许这将有助于澄清这个问题.
我的临时解决方案是这样的:
const int PRECISION = 64;
float4 unpack(float value)
{
float4 color;
color.a = value % PRECISION;
value = floor(value / PRECISION);
color.b = value % PRECISION;
value = floor(value / PRECISION);
color.g = value % PRECISION;
value = floor(value / PRECISION);
color.r = value;
return color / (PRECISION - 1);
}
float pack(float4 color)
{
int4 …
Run Code Online (Sandbox Code Playgroud) 我有类型级列表的见证类型,
data List xs where
Nil :: List '[]
Cons :: proxy x -> List xs -> List (x ': xs)
Run Code Online (Sandbox Code Playgroud)
以及以下实用程序.
-- Type level append
type family xs ++ ys where
'[] ++ ys = ys
(x ': xs) ++ ys = x ': (xs ++ ys)
-- Value level append
append :: List xs -> List ys -> List (xs ++ ys)
append Nil ys = ys
append (Cons x xs) ys = Cons x (append xs ys) …
Run Code Online (Sandbox Code Playgroud) 标题或多或少都说明了一切.根据这篇文章,我想出了这个:
public static unsafe void Replace(this MethodBase destination, MethodBase source)
{
IntPtr srcHandle = source.MethodHandle.GetFunctionPointer();
IntPtr dstHandle = destination.MethodHandle.GetFunctionPointer();
int* dstPtr = (int*)dstHandle.ToPointer();
*dstPtr = srcHandle.ToInt32();
}
Run Code Online (Sandbox Code Playgroud)
这实际上有效...偶尔--.-
例如,这是有效的.
public static class Program
{
public static void Main(string[] args)
{
MethodInfo methodA = typeof(Program).GetMethod("A", BindingFlags.Public | BindingFlags.Static);
MethodInfo methodB = typeof(Program).GetMethod("B", BindingFlags.Public | BindingFlags.Static);
methodA.Replace(methodB);
A();
B();
}
public static void A()
{
Console.WriteLine("Hai World");
}
public static void B()
{
Console.WriteLine("Bai World");
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这不是(SEHException).我所做的就是改变定义函数的顺序.
public static …
Run Code Online (Sandbox Code Playgroud) 我最近遇到了一段代码,它使用Haskell otherwise
在列表上进行模式匹配.这让我很奇怪,因为:
ghci> :t otherwise
otherwise :: Bool
Run Code Online (Sandbox Code Playgroud)
所以,我尝试了以下内容:
ghci> case [] of otherwise -> "!?"
"!?"
Run Code Online (Sandbox Code Playgroud)
我也尝试了不同类型的各种其他模式,并-XNoImplicitPrelude
打开(otherwise
从范围中删除),它仍然有效.这应该发生吗?这记录在哪里?
假设我有一个叫做的类Composite
,它包含另一个类的集合Component
,所有这些类都有一个属性Name
.使用a Dictionary
来存储组件会更好吗,或者使用动态对象会更好吗?
例如,这样做会更好:
Component component = someComposite.Components["RandomComponent"];
或者:
Component component = someComposite.Components.RandomComponent;
其中someComposite.Components dynamic
在第二个例子中.
第二种情况似乎更好,但没有类型安全......
我想补充一点,在某些时候我最终会这样做:
DerivedComponent component = someComposite.Components["RandomComponent"] as DerivedComponent;
在这种情况下dynamic
可以节省我输入转换.
那么哪个更好的设计呢?