什么时候混合语言?

发布于 2024-07-26 06:53:17 字数 353 浏览 15 评论 0原文

哪些情况下需要混合使用语言?

我不是在谈论使用 ASP.NET 与 C# 和 HTML 或用 C 编写的应用程序通过 SQL 查询访问 SQL 数据库。 例如,我正在谈论诸如将 C++ 与 Fortran 混合或 Ada 与 Haskell 等混合的事情。

[编辑]

首先:感谢您的所有回答。

当我问这个问题时,我想到你总是读到“每种语言都有其特殊用途”。

一般来说,您可以通过使用特殊的库以任何语言完成几乎所有事情。 但是,如果您有兴趣学习不同的语言,为什么不选择最能满足您的目的的编程语言,而不是选择一个可以解决您的语言最初不是为之设计的问题的库呢?

What are some situations where languages should be mixed?

I'm not talking about using ASP.NET with C# and HTML or an application written in C accessing a SQL database through SQL queries. I'm talking about things like mixing C++ with Fortran or Ada with Haskell etc. for example.

[EDIT]

First of all: thank you for all your answers.

When I asked this question I had in mind that you always read "every language has its special purpose".

In general, you can get almost everything done in any language by using special libraries. But, if you are interested in learning different languages, why not take the programming language that serves your purpose best instead of a library that solves a problem your language wasn't originally designed for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

暖风昔人 2024-08-02 06:53:17

例如,在视频游戏中,我们出于不同的目的使用不同的语言:

  • 应用程序(游戏)代码:必须快速、有组织并且大多数时候是跨平台的(至少是win,MacOS是设想),通常在约束较多的平台(控制台)上,因此使用 C++(有时是 C 和 asm)。
  • 开发工具:关卡设计工具生成游戏代码将使用的数据。 这类工具不需要在目标平台上运行(但如果可以的话,调试起来会更容易),所以它们通常是用快速开发语言(如 C#、Python 等)编写的。
  • 脚本系统:游戏的某些部分必须由设计师使用变量或脚本进行调整。 嵌入脚本语言而不是编写脚本语言确实更容易、更便宜,因此经常使用 Lua 或其他类似的脚本语言。
  • Web 应用程序:有时游戏需要在线提供一些数据,通常是在通过 SQL 访问的数据库中。 然后,使用 C#、Ruby(ROR)、Python、PHP 或任何其他适合该工作的语言编写 Web 应用程序。 由于它是关于网络的,因此您也必须使用 HTML/Javascript。
  • 等等...

在我的游戏中我也使用 HTML/Javascript 作为 GUI。

[编辑]

回答您的编辑:您最了解的语言并不总是最有效的工作工具。 这就是为什么我在自制游戏中使用 C++,因为我最了解它(我可以使用很多其他语言,因为目标是 Win/Mac/Linux,而不是控制台),但我使用 Python 来处理与构建过程相关的所有内容、文件操作等。我对Python了解不深,但用它进行快速文件操作比用C++要容易得多。 出于显而易见的原因,我不会将 C++ 用于 Web 应用程序。

最后,您将使用对工作最有效的方法。 这就是你通过在现实世界的限制下工作,并考虑到金钱、时间和质量而学到的东西。

For example, in video games we use different languages for different purpose :

  • Application (Game) code : have to be fast, organized and most of the time cross-platform (at least win, MacOS is to be envisaged), often on constraint-heavy platforms (consoles), so C++ (and sometimes C and asm) is used.
  • Development Tools : level design tools generates data that the game code will play with. Those kind of tool don't need to run on the target platform (but if you can it's easier to debug) so often they are made with fast-development languages such as C#, Python, etc.
  • Script system : some parts of the games will have to be tweaked by the designers, using variables or scripts. It's really easier and cheap to embed a scripting language instead of writing one so Lua or other similar scripting languages are often used.
  • Web application : sometimes a game will require to provide some data online, most often in a database accessed with SQL. The web application then is written in a language that might be C#, Ruby(R.O.R.), Python, PHP or anything else that is good for the job. As it's about the web, you then have to use HTML/Javascript too.
  • etc...

In my game I use HTML/Javascript for GUI too.

[EDIT]

To answer your edit : the language you know the best is not always the most efficient tool for the work. That's why for example I use C++ for my home-made game because I know it best (I could use a lot of other languages as the targets are Win/Mac/Linux, not consoles) but I use Python for everything related to build process, file manipulation etc. I don't know Python in depth but it's fare easier to do quick file manipulation with it than with C++. I wouldn't use C++ for web application for obvious reasons.

In the end, you use what is efficient for the job. That's what you learn by working in real world constraints, with money, time and quality in mind.

天涯离梦残月幽梦 2024-08-02 06:53:17

嗯,最明显(也是最常见)的情况是,当您使用某种高级语言来编写大部分程序时,获得快速开发和鲁棒性的好处,同时使用一些较低级语言(如 C 甚至汇编)来获得速度很重要。

此外,很多时候需要与用其他语言编写的其他软件进行交互。 这里的一个很好的例子是操作系统公开的 API - 它们通常是用 C 语言编写的(尽管我记得一些旧的 MacOS 版本使用 Pascal)。 如果您的语言编译器基础设施没有本机绑定,则必须编写一些接口代码来将您的程序与“另一端”“粘合”。

还有一些特定于领域的语言经过专门调整以有效地表达某种类型的计算。 您通常不会在其中编写整个程序,而只在其中编写适合工具的部分部分。 Prolog 就是一个很好的例子。

最后但并非最不重要的一点是,您有时手头有大量用另一种语言编写的旧且经过测试的代码,您可以从使用中受益。 您可能只想将其与新程序进行交互,而不是用一种新的更好的语言重新发明轮子。 当面向类似用途的语言混合在一起时,这可能是最常见的(如果不是唯一的)情况(是您提到的 C++ 和 Fortran 吗?)。

一般来说,不同的语言有不同的优点和缺点。 您应该尝试使用适合手头工作的工具。 如果用不同的语言表达程序的某些部分的好处大于由此带来的问题,那么最好就这样做。

Well, the most obvious (and the most common) situation would be when you use some high level language to make most of your program, reaping the benefits of fast development and robustness, while using some lower level language like C or even assembly to gain speed where it is important.

Also, many times it is necessary to interface with other software written in some other language. A good example here are APIs exposed by the operating system - they're usually written with C in mind (though I remember some old MacOS versions using Pascal). If you don't have a native binding for your language-compiler infrastructure, you have to write some interface code to "glue" your program with "the other side".

There are also some domain-specific languages that are tuned specifically to efficiently express some type of computation. You usually don't write your entire program in them, just some parts where it is the appropriate tool. Prolog is a good example.

Last but not least, you sometimes have heaps of old and tested code written in another language at hand, which you could benefit from using. Instead of reinventing the wheel in a new and better language, you may simply want to interface it to your new program. This is probably the most usual (if not the only) case when languages geared for similar uses are mixed together (was that C++ and Fortran you mentioned?).

Generally, different languages have different strengths and weaknesses. You should try to use the appropriate tool for the job at hand. If the benefits from expressing some parts of the program in a different language are greater than the problems this introduces, then it's best to go for it.

世界如花海般美丽 2024-08-02 06:53:17

我知道在你的问题中你有点排除了这一点,但不同的语言用于不同的领域。

现在我正在开发一个数据可视化工具,数据在数据库中,所以当然有一些 SQL,但这几乎不算什么,因为它很小而且需要经常使用。 数据被转换成一系列图表,我使用的是 R,它类似于 MATLAB,但是开源的。 它是一种独特的统计语言,具有一些高级绘图功能。

数据可视化工具不仅仅是一个图形生成器,因此需要有一种方法来浏览和导航这堆图像文件。 我们选择使用嵌入了 javascript 的 html 来构建一个可以轻松分发的离线“应用程序”。 它是离线的,因为它是独立的,html 是精心生成的,其中的 js 是精心设计的,允许用户浏览数千张图像,并按多种标准进行排序或过滤。

您如何根据数据库结构精心制作 javascript 和 html,而数据库结构会随着团队其他成员的进步而变化? 它们是由 Perl 程序(实际上是单遍脚本)创建的,该程序读取数据库中的一些结构和关键信息,然后输出超过 300 KB 的 html/js。 它也不完全是微不足道的 html,与 R 图和一些 onclick() javascript 仔细对齐的图像映射允许用户实际与普通图像图交互,因此整个事情感觉就像一个真正的数据浏览器/可视化应用程序。

这是四种“语言”,如果算上 SQL,则有五种“语言”,只是为了制作一个最终产品。

我认为用单一语言来做这件事不是一个好的选择,因为我们正在利用真正的网络浏览器的功能来为我们提供免费的 GUI 和前端。

I know in your question you sort of ruled this out, but different languages are used for different domains.

Right now I am working on a data visualizer, the data is in a database so of course there is some SQL, but that hardly counts because it's small and required frequently. The data is turned into a series of graphs, I'm using R, which is like MATLAB but open source. It is a unique statistical language with some advanced plotting features.

A data visualizer isn't just a graph generator, so there needs to be a way to browse and navigate this pile of image files. We opted to use html with embedded javascript to build an offline "application" that can be easily distributed. It's offline in the sense that it is self contained, that html is carefully generated and the js inside it is carefully crafted to allow the user to browse thousands of images sorting or filtering by a number of criteria.

How do you carefully craft javascript and html based on a database structure that changes as the rest of my team makes progress? They are made by a perl program (single pass script really) that reads into the db for some structure and key information, and then outputs over 300 kilobytes of html/js. It's not entirely trivial html either, imagemaps that are carefully aligned with the R plots and some onclick() javascript allow the user to actually interact with a plain image plot so this whole thing feels like a real data browser/visualizer application.

That's four 'languages', five if you count SQL, just to make a single end product.

I dont think doing this in a single language would be a good choice, because we are exploiting the capabilities of a real web browser to give us a free GUI and frontend.

半仙 2024-08-02 06:53:17

当前一个很好的例子是在 VB.NET 中编写创建 XML 文档的方法,它具有 C# 所缺乏的“XML Literals”功能。 由于它们都是 .NET 语言,因此没有理由不调用另一种语言:

Public Function GetEmployeeXml (ByVal salesTerritoryKey As Integer) As XElement
    Using context As New AdventureWorksDW2008Entities
        Dim x = <x>
                <%= From s In context.DimSalesTerritory _ 
                Where s.SalesTerritoryKey = salesTerritoryKey _
                Select _
                <SalesTerritory 
                    region=<%= s.SalesTerritoryRegion %>
                    country=<%= s.SalesTerritoryCountry %>>
                    <%= From e in s.DimEmployee _
                        Select _
                    <Employee firstName=<%= e.FirstName %> lastName=<%= e.LastName %>>
                         <%= From sale in e.FactResellerSales _
                             Select _
                        <Sale 
                            orderNumber=<%= sale.SalesOrderNumber %>
                            price=<%= sale.ExtendedAmount %>/>  %>
                    </Employee> %>
                </SalesTerritory> %>
                </x>
        Return x
    End Using 
End Function  

An excellent current example would be to write methods for creating XML documents in VB.NET, which has an "XML Literals" feature, which C# lacks. Since they're both .NET languages, there's no reason not to call one from the other:

Public Function GetEmployeeXml (ByVal salesTerritoryKey As Integer) As XElement
    Using context As New AdventureWorksDW2008Entities
        Dim x = <x>
                <%= From s In context.DimSalesTerritory _ 
                Where s.SalesTerritoryKey = salesTerritoryKey _
                Select _
                <SalesTerritory 
                    region=<%= s.SalesTerritoryRegion %>
                    country=<%= s.SalesTerritoryCountry %>>
                    <%= From e in s.DimEmployee _
                        Select _
                    <Employee firstName=<%= e.FirstName %> lastName=<%= e.LastName %>>
                         <%= From sale in e.FactResellerSales _
                             Select _
                        <Sale 
                            orderNumber=<%= sale.SalesOrderNumber %>
                            price=<%= sale.ExtendedAmount %>/>  %>
                    </Employee> %>
                </SalesTerritory> %>
                </x>
        Return x
    End Using 
End Function  
焚却相思 2024-08-02 06:53:17

混合语言的最大原因是一种语言在某些领域具有优势,而另一种语言在另一种领域具有优势。 因此,您尝试通过将两者结合在一起来利用两者的功能。 一个常见的例子是使用 C 和 ASM,因为 C 更抽象,可以轻松地在程序中执行更复杂的操作,但是,您可能希望使用 ASM 来使用硬件和处理器执行基础级别的操作。 因此,鉴于 C 的性质及其在嵌入式系统中的使用,它们经常被混合在一起。

The biggest reason you would mix langauges is because one language has advantages in certain areas, where another has advantages in another. So, you try to harness the capabilities of both by throwing them together. A common example is using C and ASM, because C is more abstract and makes it easy to do the more complex stuff in a program, however, you would want to use ASM to do base level stuff with hardware and the processor. So, they are often mixed, given the nature of C and its use in embedded systems.

感性 2024-08-02 06:53:17

根据语言所属的范式,它有其优点和缺点。 例如,人们可能想要使用 C# 的 GUI 功能,但使用 Prolog 的高效回溯或人工智能功能(一种逻辑语言) >范例)在显示之前计算一些细节。

一个简化的示例

想象一下,尝试编写一个程序,允许用户与计算机下国际象棋。

(可能)我会:

1.) 使用 Windows GUI 库创建视觉效果。

2.) 使用 Prolog 计算可能的走法,并选择最佳/最可行的走法。

3.) 在我的 C# 代码中从 Prolog 检索步骤 2 的结果,并渲染结果。

4.) 允许使用 C# 进行游戏玩法以及视觉效果和 UI 的快速开发,并依靠 Prolog 进行计算/回溯

Depending on what paradigm a language falls under, it has its pros and cons. For example, one might want to use the GUI capabilities of C# but use the efficient backtracking or Artificial Intelligence capabilities of Prolog (a language in the logical paradigm) to compute some details before displaying them.

A simplified example

Imagine trying to write a program that allows a user to play Chess against a computer.

(Potentially) I would:

1.) Create the visuals with Windows GUI Libraries.

2.) Calculate the possible moves using Prolog, and choose the best/most viable move.

3.) Retrieve the results from step 2 from Prolog in my C# code, and render the results.

4.) Allow for the gameplay and rapid development of visuals and UI in C# and rely on the Prolog for the calculations/backtracking

懒猫 2024-08-02 06:53:17

当您已经有用两种或多种不同语言编写的代码并且您发现组合这些程序是有意义的时,最常发生这种情况。 重写成本高昂且需要时间。

在金融界,您通常必须让程序保持(或更换)50 年。 随着技术每 10 年更新一次,新的合同(抵押贷款、人寿保险)就会以最新的语言/环境创建。 四个老员工只负责每月付款和现有合同的变更。 要了解公司的经营状况,您需要集成所有五个系统的数据。

我认为保持旧技术的生存比每次都迁移到最新技术更便宜。 从规避风险的角度来看,这是有道理的。

对于开发人员来说,Web 应用程序可能不应该是混合语言。 Smalltalk 做得很好,用 Gemstone 实现持久性,用 Seaside 作为 Web 应用程序框架。 多种语言(javascript)可以隐藏在框架中。

This most often happens when you already have code written in two or more different languages and you notice that it makes sense to combine the programs. Rewriting is expensive and takes time.

In the financial world you often have to keep programs alive (or replaced) 50 years. With technology replacement every 10 years, new contracts (mortgages, life insurance) are created in the newest language/environment. The four older ones just handle the monthly payments and changes to existing contracts. To know how the company is doing, you need to integrate data from all five systems.

I suppose keeping the old ones alive is cheaper than migrating each time to the newest technology. From a risk avoiding point of view it makes sense.

Web applications should probably not be mixed language for the developer. Smalltalk does just fine, with Gemstone for persistence and Seaside as web application framework. The multiple languages (javascript) can be hidden in the framework.

淡墨 2024-08-02 06:53:17
  • 当 2 个头比 1 个头更好时。

    我经常看到 Flash 与 C# 一起嵌入的游戏 - AI 和其他繁重的代码在 C++ DLL 上运行。

  • 当被迫

    编写新代码来增强旧框架支持的新系统

  • When 2 heads are better than one.

    I've commonly seen games where flash was embedded with C# - with the AI and other heavy code running off a C++ DLL.

  • When forced to

    Writing new code to augment a new system supported by an old framework

乖乖公主 2024-08-02 06:53:17

例如,在游戏(非常核心的应用程序)中,您通常有一个非常紧凑的 C++ 引擎来完成所有繁重的工作,还有一种易于访问且适合制作我们称为“游戏”的特殊情况集合的脚本语言(例如 Lua) ' 发生。

For instance in games (which are pretty hardcore applications) you usually have a very tight c++ engine that does all the heavy lifting and a scripting language (such as Lua) that's accessible and suited for making that collection of special cases that we call 'game' happen.

一笔一画续写前缘 2024-08-02 06:53:17

这里的人有大部分理由。 我只讲这个:

还有一种优雅降级的情况。 例如,我正在开发一个旧的 Intranet,我们正在一点一点地从一种语言更改为另一种语言,因此此时我们在同一系统中拥有不同的语言。

People here have most of the reason. I'll just have this one:

There is also the case of graceful degradation. For instance I am working on a legacy intranet, and we're changing little by little from a language to another so at this point we have different languages in the same system.

深空失忆 2024-08-02 06:53:17

设备编程 - 通常,您希望使用操作系统的本机 UI 语言(例如 Android 的 Java)对 UI 进行编程,但需要使用更深入底层的内容(例如C)。

Device Programming - typically you would want to program the UI with the OS's native UI language (Java for Android for example) but would need to program the device drivers with something that gets more into the low level (like C).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文