Language agnostic 动态编程语言优点的具体(代码)示例

Language agnostic 动态编程语言优点的具体(代码)示例,language-agnostic,programming-languages,type-systems,dynamic-typing,Language Agnostic,Programming Languages,Type Systems,Dynamic Typing,我目前正在设计一个受控实验,我 希望衡量动态类型编程语言的优势 与静态类型的比较 我不是在寻找另一个“哪一个更好”——这里辩论,那里辩论 关于这个话题的讨论是否足够(例如:。 或 ). 我也问了同样的关于LtU的问题,结果是另一个问题 讨论:-) 所有这些讨论都提到了一些好的观点,但几乎都没有提到 一些具体的代码(!)-例子证明了他们的观点 所以我的问题是:你们谁能给我一些示例代码 直接显示动态类型语言的一些优点或显示 静态类型系统与其说是有用的,不如说是障碍的情况 工具 提前感谢。根据定义:鸭

我目前正在设计一个受控实验,我 希望衡量动态类型编程语言的优势 与静态类型的比较

我不是在寻找另一个“哪一个更好”——这里辩论,那里辩论 关于这个话题的讨论是否足够(例如:。 或 ). 我也问了同样的关于LtU的问题,结果是另一个问题 讨论:-)

所有这些讨论都提到了一些好的观点,但几乎都没有提到 一些具体的代码(!)-例子证明了他们的观点

所以我的问题是:你们谁能给我一些示例代码 直接显示动态类型语言的一些优点或显示 静态类型系统与其说是有用的,不如说是障碍的情况 工具


提前感谢。

根据定义:鸭子打字。但您需要更具体的示例,比如说:构建对象。在静态类型语言中,您需要声明类型或使用内置方法生成运行时代码。在动态语言中,您可以只构建临时对象。例如,要在groovy中创建模拟,您只需编写一个需要模拟的方法,并将其作为对象传递:


在静态语言中,人们编写大型复杂的库以使其更简单。尽管如此,它还是很冗长,而且常常有局限性。不久前,我们曾经做出过这样的选择:我是否希望它能够快速、轻松地编写程序,但有时会有更多的运行时错误?或者我想在compiletime避免更多的bug,但代价是不得不浪费更多的时间编写更多的样板文件

现在结束了。当动态语言是避免在代码中添加更多官僚主义的唯一方法时,它们非常令人兴奋,但是动态类型现在已经被类型推断淘汰了。所以你实际上是对的——现在更强大的强类型系统正在流行,动态类型不再增加价值

嗯?哪里怎么做

Haskell、MLs、Scala、Rust等语言以及.NET家族的一些语言将强类型与类型推断结合在一起,因此您可以充分利用这两种语言:每种语言都有一个强的强制类型,但您不必实际写下它,除非编译器无法理解它,这通常是可以做到的

不要因为他们是学者而忽视他们。事情变了。在过去的几年里,那些拥有强大打字系统的语言已经越来越流行。(2) 而且,我发现现在越来越少听到一种全新的语言,它不将安全性强的类型与方便性强的类型推断相结合,至少在某种程度上是这样。今天全新的深奥语言是下一个十年的“企业标准”,所以我认为每个人最终都会使用这种两全其美的方法

甚至老爪哇也在慢慢地朝那个方向移动!(示例:菱形运算符;推断lambda表达式的函数接口)

第一次学习动态语言现在为时已晚,就好像在整个90年代,你一直在推迟用CD播放机更换磁带机,然后终于在2002年左右,你开始购买CD播放机

编辑:我重读了你的问题。我同情你对具体准则的渴望。这是一块罗塞塔石。检查列表是否已排序的通用过程,以及调用该列表的代码。调用它的代码每天都做正确的事情,在闰日做错误的事情,这是一个例子,说明当很少执行的代码中有bug时,编译时错误是如何战胜运行时错误的。代码未经测试,只是一个草图。特别是,我仍然在学习Haskell(因此新转换的…:P的热情),而且我已经有几年没有像以前那样大量使用Python了,所以请原谅

传统静态类型:

// Java 7 - strong typing but no type inference
// Notice that you get safety, because the wrong usage is caught at compile time
// But you have to type a lot :-(
  static interface Comparable { boolean compare(Comparable other); }
  ...
  boolean isListSorted( List<T extends Comparable> xs ) {
    T prev = null; for( T x: xs ) {
        if( prev!=null && !prev.compare(xs) ) return false;
        prev = x; 
    } 
    return true;
  }
  ...
  public final class String implement Comparable { ... }
  public final class Animal /* definitely does not implement Comparable! :-) */ { ... }

  ...
  // proper usage
  List<String> names = Lists.newArrayListOf("Red", "Spinelli", "Ankh", "Morporkh", "Sam");
  boolean areTheNamesSorted = isListSorted(names);
  if(todayIsALeapDay()) {
     // bad usage -- the compiler catches it, so you don't have to worry about this happening 
     // in production when a user enters data that send your app down a rarely-touched codepath
     List<Animal> pets = Lists.newArrayListOf( Animal.getCat(), Animal.getDog(), Animal.getBird() );
     boolean areTheNamesSorted = isListSorted(pets);
  }
class Animal:
   ...

# Python 2.6 -- dynamic typing
# notice how little keystrokes are needed
# but the bug is now
def isListSorted(xs):
  # to keep it more similar to the Java7 version, zip is avoied
  prev = None
  for x in xs:
    if prev is not None and not prev.compare(x): return False
    prev = x
  return True
...
# usage -- beautiful, isn't it?
names = ["Raph", "Argh", "Marge"]
areNamesSorted = isListSorted(names)

if isLeapDayToday():
   # ...but here comes trouble in paradise!
   animals = [Animal.getCat(), Animal.getDog()]
   # raises a runtime exception. I hope your unit tests catch it, but unlike type 
   # systems it's hard to make absolutely sure you'll be alerted if you forget a test
   # besides, then you've just traded the Evil of Having to Write Lots of Type Signatures
   # for the Evil of Having to Write Lots of Unit Tests. What kind of terrible deal is that?
   areAnimalsSorted = isListSorted(animals)
强大的打字功能:

-- Haskell - full safety of strong typing, but easy on the fingers and eyes
class Comparable a where
  compare :: a -> a -> Bool
end
instance Comparable String where
   ...
end
data Animal = ...

isListSorted [] = True
isListSorted x:[] = True
isListSorted x1:x2:xs = (compare x1 x2) && (isListSorted xs)

names = ["Raplph", "Argh", "Blarge"]
areNamesSorted = isListSorted names



animals = [Cat, Dog, Parrot]

-- compile time error because [Animal] is not compatible with Comparable a => [a] since
-- Animal is not an instance of comparable

areAnimalsSorted = isListSorted animals
-- Same Haskell program as before, but we explicitly write down the types 
-- Just for educational purposes. Like comments, you can omit them if you don't think
-- the reader needs them because it's obvious. Unlike comments, the compiler verifies their truth!


class Comparable a where
  compare :: a -> a -> Bool
end
instance Comparable String where
   ...
end
data Animal = Cat | Dog | Parrot


isListSorted :: Ord a => [a] -> Bool
isListSorted [] = True
isListSorted x:[] = True
isListSorted x1:x2:xs = (compare x1 x2) && (isListSorted xs)

names :: [String]
names = ["Raplph", "Argh", "Blarge"]

areNamesSorted = isListSorted names


-- compile time error because [Animal] is not compatible with Comparable a => [a] since
-- Animal is not an instance of comparable

animals :: [Animal]
animals = [Cat, Dog, Parrot]

areAnimalsSorted = isListSorted animals
冗长有力的打字:

-- Haskell - full safety of strong typing, but easy on the fingers and eyes
class Comparable a where
  compare :: a -> a -> Bool
end
instance Comparable String where
   ...
end
data Animal = ...

isListSorted [] = True
isListSorted x:[] = True
isListSorted x1:x2:xs = (compare x1 x2) && (isListSorted xs)

names = ["Raplph", "Argh", "Blarge"]
areNamesSorted = isListSorted names



animals = [Cat, Dog, Parrot]

-- compile time error because [Animal] is not compatible with Comparable a => [a] since
-- Animal is not an instance of comparable

areAnimalsSorted = isListSorted animals
-- Same Haskell program as before, but we explicitly write down the types 
-- Just for educational purposes. Like comments, you can omit them if you don't think
-- the reader needs them because it's obvious. Unlike comments, the compiler verifies their truth!


class Comparable a where
  compare :: a -> a -> Bool
end
instance Comparable String where
   ...
end
data Animal = Cat | Dog | Parrot


isListSorted :: Ord a => [a] -> Bool
isListSorted [] = True
isListSorted x:[] = True
isListSorted x1:x2:xs = (compare x1 x2) && (isListSorted xs)

names :: [String]
names = ["Raplph", "Argh", "Blarge"]

areNamesSorted = isListSorted names


-- compile time error because [Animal] is not compatible with Comparable a => [a] since
-- Animal is not an instance of comparable

animals :: [Animal]
animals = [Cat, Dog, Parrot]

areAnimalsSorted = isListSorted animals