由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 经常看到一些名词:invariant, covariant, immutable
相关主题
有没有喜欢haskell的同学Java EE这东西还有必要学吗
mutating input argument不应该鼓励吧给Java/Spring说几句好话
Haskell很难学。。模板算是纯FP吗
关于FP批判 go
scala写个loop老难了clojure和common lisp区别大么,语法上。
粉FP的人是因为把电脑想象成图灵机了对 (im)mutability 的误解和深度理解
functional programming?这么说吧,fp不是否定变量,而是控制变量的范围
学FP不是为了写代码, 而是为了优秀的架构.Scala's type system
相关话题的讨论汇总
话题: type话题: list话题: meth话题: object话题: java
进入Programming版参与讨论
1 (共1页)
b**********h
发帖数: 419
1
感觉这些概念是编程课里某个重要Topic里的
有什么书可以系统学习这些概念的, 最好是Java的书,
谢谢
g****t
发帖数: 31659
2
都是洗脑用的商品。

【在 b**********h 的大作中提到】
: 感觉这些概念是编程课里某个重要Topic里的
: 有什么书可以系统学习这些概念的, 最好是Java的书,
: 谢谢

c*********e
发帖数: 16335
3
immutable就是这个值不能变。

【在 b**********h 的大作中提到】
: 感觉这些概念是编程课里某个重要Topic里的
: 有什么书可以系统学习这些概念的, 最好是Java的书,
: 谢谢

a******i
发帖数: 6
4
see below:
For example, with the function type
A -> B // functional notation
public B meth(A arg) // how this looks in Java
we have the following:
Let C be a subtype of A, and D be a subtype of B. Then the following is
valid:
B b = meth(new C()); // B >= B, C < A
Object o = meth(new C()); // Object > B, C < A
but the follwoing are invalid:
D d = meth(new A()); // because D < B
B b = meth(new Object()); // because Object > A
hence, to check whether a call of meth is valid, we must check
The expected return type is a supertype of the declared return type.
The actual argument type is a subtype of the declared argument type.
This is all well known and intuitive. By convention we say that the return
type of a function is covariant, and the argument type of a method is
contravariant.
With parameterized types, like List, we have it that the argument type is
invariant in languages like Java, where we have mutability. We can't say
that a list of C's is a list of A's, because, if it were so, we could store
an A in a list of Cs, much to the surprise of the caller, who assumes only
Cs in the list. However, in languages where values are immutable, like
Haskell, this is not a problem. Because the data we pass to functions cannot
be mutated, a list of C actually is a list of A if C is a subtype of A. (
Note that Haskell has no real subtyping, but has instead the related notion
of "more/less polymorphic" types.)
n*w
发帖数: 3393
5
Java应该不支持covariance?除了c#, 主流的还有哪个?

【在 a******i 的大作中提到】
: see below:
: For example, with the function type
: A -> B // functional notation
: public B meth(A arg) // how this looks in Java
: we have the following:
: Let C be a subtype of A, and D be a subtype of B. Then the following is
: valid:
: B b = meth(new C()); // B >= B, C < A
: Object o = meth(new C()); // Object > B, C < A
: but the follwoing are invalid:

T*******x
发帖数: 8565
6
这个道理我还是没明白:如果C是A的subtype,为什么在Java中,list of C里不能存一
个type A的object,而在haskell里就可以。

【在 a******i 的大作中提到】
: see below:
: For example, with the function type
: A -> B // functional notation
: public B meth(A arg) // how this looks in Java
: we have the following:
: Let C be a subtype of A, and D be a subtype of B. Then the following is
: valid:
: B b = meth(new C()); // B >= B, C < A
: Object o = meth(new C()); // Object > B, C < A
: but the follwoing are invalid:

n*w
发帖数: 3393
7
Java的list中又进又出。haskell只出

【在 T*******x 的大作中提到】
: 这个道理我还是没明白:如果C是A的subtype,为什么在Java中,list of C里不能存一
: 个type A的object,而在haskell里就可以。

T*******x
发帖数: 8565
8
那为什么只出就可以呢?

【在 n*w 的大作中提到】
: Java的list中又进又出。haskell只出
n*w
发帖数: 3393
9
这样的话就是sub type关系了。

【在 T*******x 的大作中提到】
: 那为什么只出就可以呢?
T*******x
发帖数: 8565
10
list(C) 是 list(A) 的 sub type?

这样的话就是sub type关系了。

【在 n*w 的大作中提到】
: 这样的话就是sub type关系了。
相关主题
粉FP的人是因为把电脑想象成图灵机了Java EE这东西还有必要学吗
functional programming?给Java/Spring说几句好话
学FP不是为了写代码, 而是为了优秀的架构.模板算是纯FP吗
进入Programming版参与讨论
n*w
发帖数: 3393
11
yes, 反直观的是contravariant.

【在 T*******x 的大作中提到】
: list(C) 是 list(A) 的 sub type?
:
: 这样的话就是sub type关系了。

T*******x
发帖数: 8565
12
谢谢。这个地方我还是没想明白,但是我不知道该问什么问题好了。

【在 n*w 的大作中提到】
: yes, 反直观的是contravariant.
h**********c
发帖数: 4120
13
看过一个讲座,忘了,
我理解covariant是change the ceiling,contravariante是有一个floor.
实际中没用过,不过JEE里好多吗喜欢这么用。
n*w
发帖数: 3393
14
想到一个contravariant解释,假如有一个function eat,接受一个参数,类型为鱼。
那么吃动物应该是其子类。大吃一鲸不是。
l******t
发帖数: 55733
15
这是oo继承固有的问题。fp没这个问题。fp的category是mixin的。
1 (共1页)
进入Programming版参与讨论
相关主题
Scala's type systemscala写个loop老难了
1st class citizen粉FP的人是因为把电脑想象成图灵机了
我还是认为scala的程序员水平高functional programming?
go几天的一些感受学FP不是为了写代码, 而是为了优秀的架构.
有没有喜欢haskell的同学Java EE这东西还有必要学吗
mutating input argument不应该鼓励吧给Java/Spring说几句好话
Haskell很难学。。模板算是纯FP吗
关于FP批判 go
相关话题的讨论汇总
话题: type话题: list话题: meth话题: object话题: java