朱莉娅是动态输入的吗? [英] Is Julia dynamically typed?

查看:70
本文介绍了朱莉娅是动态输入的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多博客,以及手册本身,假设Julia是是动态键入的. 但是从我阅读手册的过程中,听起来更像是 静态键入 F#.

A lot of blogs, and the manual itself, say that Julia is dynamically typed. But from my reading of the manual, it sounds to me more like it is statically typed with type inference, like F#.

  • Julia是使用类型推断静态键入的吗?
  • 它是动态键入的吗?
  • 我假设它是动态键入的,手册似乎不太可能是错误的.
  • Julia完全涉及类型推断吗?
  • Is Julia statically typed with type inference?
  • Is it dynamically typed?
  • I'm assuming it is dynamically typed, it seems unlikely the manual is wrong.
  • Is type inference involved in Julia at all?

推荐答案

Tim Holy的答案很正确,但我会详细说明.首先,让我们定义一些术语-您可能不同意我的定义,但至少您会知道我在说什么.我认为静态和动态语言之间的主要区别在于:在静态语言中,表达式具有类型;在静态语言中,表达式具有类型.在动态语言中,值具有类型.

Tim Holy's answer is quite correct, but I'll elaborate a bit. First, let's define some terms – you may disagree with my definitions, but at least you'll know what I'm saying. The primary difference between static and dynamic languages, in my view, is this: in static languages, expressions have types; in dynamic languages, values have types.

在静态语言中,有一些规则可以确定程序中每个表达式的类型.表达式的类型决定了程序的行为.不允许为每个表达式确定一致类型的程序被认为是错误的,将无法编译.在存在多态的情况下,表达式的类型可能不是单一的具体类型:可以将参数多态性视为让同一代码描述整个具体类型的算法家族的一种方式,该类型由类型的参数索引;可以将亚型多态性视为将有限量的动态行为引入到其他静态语言中.

In a static language, there are rules for determining the type of every expression in a program. The types of expressions dictate the behavior of the program. A program that doesn't admit a consistent type to be determined for every expression is considered incorrect and will not compile. In the presence of polymorphism, the type of an expression may not be a single concrete type: parametric polymorphism can be thought of as a way of letting the same code describe a whole family of concretely typed algorithms, indexed by the parameters of the types; subtype polymorphism can be thought of as introducing a limited amount of dynamic behavior into an otherwise static language.

动态语言没有为表达式分配类型的规则:类型是由数据在程序执行时流过程序的方式隐含的.通常,表达式可能根本会产生任何类型的值.因此,类型理论家有时将动态语言描述为``单型''-即从静态角度来看,其中``类型''本质上是表达式的属性,动态语言中的所有表达式都具有类型Any.当然,这是将静态类型(仅对表达式有意义)应用于一种语言,其中类型对值仅有意义.

Dynamic languages, on the other hand do not have rules for assigning types to expressions: types are implied by the way data flows through the program as it executes. In general, expressions can potentially produce values of any type at all. Because of this, type theorists sometimes describe dynamic languages as "unityped" – i.e. from the static perspective, where a "type" is inherently a property of an expression, all expressions in a dynamic language have the type Any. Of course, that's applying the static notion of type – which is only meaningful for expressions – to a language where the notion of type is only meaningful for values.

Julia正处在动态阵营中:类型是值的属性,而不是表达式的属性.代码的结果类型取决于代码在执行时如何流过它;代码的类型取决于代码的类型.该语言不包含在执行表达式之前为表达式分配类型的任何规则.但是,与许多动态语言不同,Julia具有一种相当复杂的语言来谈论类型,并且您可以使用类型来注释表达式.例如,x::T是断言,x是类型T的值;如果为true,则x::T的计算结果为x的值,否则将引发错误,并且表达式不返回任何值.方法签名中的类型注释的含义略有不同:它们不是声明现有值的类型,而是表示仅当相应参数为所指示类型时才应用该方法.无论哪种情况,下面的代码都可以安全地假定x的值是T类型.

Julia is squarely in the dynamic camp: types are a property of values not expressions. The result type of code is determined by how values flow through it when it executes; the language does not include any rules for assigning types to expressions before executing them. Unlike many dynamic languages, however, Julia has a fairly sophisticated language for talking about types, and you can annotate expressions with types. For example, x::T is an assertion that x is a value of type T; if that is true, x::T evaluates to the value of x, otherwise an error is raised and the expression returns no value. Type annotations in method signatures have a slightly different meaning: instead of asserting the type of an existing value, they indicate that the method only applies if the corresponding argument is of the indicated type. In either case, the following code can safely assume that the value of x is of type T.

[另外:在某些具有渐进"或可选"类型的语言中,类型注释将语言从动态模式切换为静态模式:没有类型注释的方法是动态的;具有类型注释的方法是静态的.在静态代码中,存在为所有表达式分配类型的规则,并且代码必须满足这些规则.这不是Julia的工作方式-带类型注释的代码仍然是动态的,并且与不带类型注释的代码具有相同的语义.]

[Aside: In some languages with "gradual" or "optional" typing, type annotations switch the language from dynamic to static mode: methods without type annotations are dynamic; methods with type annotations are static. In static code, there are rules for assigning types to all expressions and the code must satisfy those. This is not the way Julia works – code with type annotations is still dynamic and has the same semantics as code without type annotations.]

使用F#,OCaml或Haskell等语言进行类型推断是确定表达式类型的一部分.如果编译器无法推断任何表达式的类型,则您的程序将损坏并且将无法编译.这些语言都使用某种形式的Hindley-Milner类型推断,这是从代码结构中导出表达式类型的一种非常聪明的方法,而无需写出显式类型(将其与动态语言进行比较,在动态语言中,类型被隐含)执行代码).在大多数情况下,根本不需要类型注释,这与在C ++,C#和Java等语言中可能需要的冗长的类型声明相比是一件令人愉快的事情.但是,这与动态语言(例如Julia和Python)完全不同,在动态语言中,不需要类型注释就完全是因为表达式没有预定的类型是完全可以接受的.在Hindley-Milner语言中,您可能不必编写与C ++或Java一样多的类型,但是每个表达式都具有编译器可以计算的预定类型.

Type inference in languages like F#, OCaml or Haskell is part of how the types of expressions are determined. If the compiler cannot infer the type of any expression, your program is broken and will not compile. These languages all use some form of Hindley-Milner type inference, which is a very clever way to derive the types of expressions from the structure of the code without having to write out explicit types (compare this to dynamic languages where the types are implied by execution of the code). Much of the time no type annotations are required at all, which is quite pleasant compared to the verbose type declarations which can be necessary in languages like C++, C# and Java. This is very different, however, from dynamic languages like Julia and Python where no type annotations are required simply because it is perfectly acceptable for expressions not to have a predetermined type. In Hindley-Milner languages, you may not have to write as many types as in C++ or Java, but every expression has a predetermined type that the compiler can compute.

Julia的编译器会进行类型推断,但有很大不同:不必每个表达式都具有可推断的类型.编译器分析代码以尝试预测表达式的类型,然后使用该信息生成更有效的机器代码.但是,如果它不能确定表达式的类型,那没什么大不了的:编译器只是使用运行时类型信息发出将始终起作用的通用代码.在Julia的大部分情况下,类型推断只是一种优化-无论有没有代码,代码都可以以相同的方式工作-但是成功进行类型推断后,它的运行速度将大大提高.

Julia's compiler does type inference, but it is very different: it is not necessary for every expression to have an inferrable type. The compiler analyzes code to try to predict the types of expressions and uses that information to generate more efficient machine code. But if it can't determine the type of an expression, it's no big deal: the compiler just emits generic code that will work anyway, using run-time type information. For the most part in Julia, type inference is just an optimization – your code will work the same way with or without it – but with successful type inference, it will run a lot faster.

这篇关于朱莉娅是动态输入的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆