我正在玩ConstraintKindsGHC 的扩展.我有以下数据类型,它只是满足一个参数约束的一个框c:
data Some (c :: * -> Constraint) where
Some :: forall a. c a => a -> Some c
Run Code Online (Sandbox Code Playgroud)
例如,我可以构造一个带有某种数字的盒子(可能不是很有用).
x :: Some Num
x = Some (1 :: Int)
Run Code Online (Sandbox Code Playgroud)
现在,只要c包含约束Show,我就可以提供一个实例Show (Some c).
instance ??? => Show (Some c) where
show (Some x) = show x -- Show dictionary for type of x should be in scope here
Run Code Online (Sandbox Code Playgroud)
但是如何在实例上下文中标记此要求(标有???)?
我不能使用等式约束(c ~ Show),因为两者不一定相等.c可能是 …
假设我在事务中执行以下步骤:
如果A在平均时间内发生变化,是否有可能使此交易失败?
简而言之:如何在持久化包中实现隔离?
我有以下模块:
{-# LANGUAGE DataKinds, KindSignatures, TypeFamilies, RoleAnnotations #-}
module Main where
import Data.Coerce (coerce)
-- logical negation for type level booleans
type family Not (x :: Bool) where
Not True = False
Not False = True
-- a 3D vector with a phantom parameter that determines whether this is a
-- column or row vector
data Vector (isCol :: Bool) = Vector Double Double Double
type role Vector phantom
-- convert column to row vector or row to column vector …Run Code Online (Sandbox Code Playgroud) 鉴于所有Agda程序都已终止,评估策略对于指称语义无关紧要,但它对性能有影响(如果您运行过Agda程序).
那么,Agda使用什么评估策略?使用codata(♯,♭)代替数据变更评估策略吗?有没有办法强制按需调用懒惰评估?
该changes函数有类型Frameworks t => Behavior t a -> Moment t (Event t (Future a)).Future是抽象的,只有一个函数使用它(reactimate').
但是,我可以轻松编写以下函数:
changes' :: Frameworks t => Behavior t a -> Moment t (Event t a)
changes' b = fmap (fmap const b <@>) (changes b)
Run Code Online (Sandbox Code Playgroud)
获得正常(非Future)事件.
这个功能有问题吗?如果没有,为什么原始changes功能有更严格的类型?
我想使用模板参数包的类型作为不同模板的参数,但切断最后一个参数.
例如:
template <class... Ts> struct some_template;
template <class... Ts> struct foo
{
using bar = some_template<magically_get_all_but_last(Ts)...>;
};
// I might be missing a few "typename"s, but you get the idea.
static_assert(std::is_same<foo<int, bool, std::string>::bar, some_template<int,bool> >::value);
Run Code Online (Sandbox Code Playgroud)
请注意,这与仅获取最后一个参数相反.
在Java中,定义通用异常类是非法的.编译器将拒绝编译以下内容:
public class Foo<T> extends Throwable {
// whatever...
}
Run Code Online (Sandbox Code Playgroud)
但是,这个Scala代码编译得很好:
class Foo[T](val foo: T) extends Throwable
Run Code Online (Sandbox Code Playgroud)
即使是奇怪的,只要我捕获原始Foo类型,我就可以在Java代码中使用这个Scala类:
public class Main {
public static void main(String[] args) {
try {
throw new Foo<String>("test");
}
catch(Foo e) {
System.out.println(e.foo());
}
}
}
Run Code Online (Sandbox Code Playgroud)
这编译,运行和打印"测试".
这是根据JLS和JVM规范很好地定义,还是偶然发生?
Java对通用异常的限制纯粹是语言限制还是也适用于字节码(在这种情况下,Scala编译器生成的字节码无效)?
编辑:这是Scala类在反编译后看到的内容:
public class Foo<T> extends java.lang.Throwable {
public T value();
Code:
0: aload_0
1: getfield #15 // Field value:Ljava/lang/Object;
4: areturn
public Foo(T);
Code:
0: aload_0
1: aload_1
2: putfield #15 // Field …Run Code Online (Sandbox Code Playgroud) 当ScalaIDE在源代码中检测到错误时,它会以与JDT相同的方式强调它.但是,要查看错误消息,我必须将鼠标移到行号左侧的红色图标上.要选择一个quickfix,我必须在光标出错时按Ctrl + 1.
在JDT中,我可以将鼠标悬停在错误上,它会显示错误消息以及所有可能的修复.
是否可以在某处激活Scala的功能?我没有在设置中找到任何内容.
这是在Eclipse 4.2和ScalaIDE 2.1.0 M3上.
你可以得到这样一个类的所有构造函数:
import scala.reflect.runtime.universe._
val ctor = typeOf[SomeClass].declaration(nme.CONSTRUCTOR).asTerm.alternatives
Run Code Online (Sandbox Code Playgroud)
有没有办法知道哪一个是主要的构造函数?它总是列表中的第一个吗?SomeClass在Java中定义的主要构造函数的概念不存在的情况下会发生什么?
您如何在Lisp中表达以下Java代码?
class Foo {
private String s;
public Foo(String s) {
this.s = s;
}
}
class Bar extends Foo {
public Bar(int i) {
super(Integer.toString(i));
}
}
Run Code Online (Sandbox Code Playgroud)
在Lisp中,是make-instance或者initialize-instance相当于构造函数?如果是,你如何调用超类构造函数?