16

I'm about to start an LFS-based linux distro just for a hobby project. I plan on doing some very non-standard tasks, and most of it will involve change almost all scripts in the distro. (mainly init scripts, but also I'll be writing a simple set of package manager scripts.) Since I'm gonna be going this far off the norm, and since I have never been a fan of dynamic-typed languages (perl, python, bash and the rest are good, but just not my forte), I was wondering if anyone knew of an interpreted language that actually has declared variables.

Cœur
  • 37,241
  • 25
  • 195
  • 267
sdellysse
  • 39,108
  • 9
  • 25
  • 24
  • Python is strongly typed. Not dynamic. You can't add a number and a string silently without explicit conversion. – George Feb 16 '09 at 02:29
  • 3
    @gdivos: Python is both strongly typed *and* dynamic. It's dynamic because you can try to call any function with any type arguments, and a mismatched parameter type won't be detected until run time. A static language would detect that problem at compile time (or at least, before run time). – Greg Hewgill Feb 16 '09 at 02:38
  • 2
    There is no such thing as an interpreted language. Every language can be implemented by either a compiler or an interpreter. And, in fact, the vast majority of programming languages have at least one implementation of each. Also, most modern language implementations have actually both a compiler and an interpreter. Since you mention Haskell: Hugs is a Haskell interpreter. V8 is a JavaScript compiler. Microsoft's PowerShell is a compiler. A couple of years ago, the Bash developers were thinking about switching to a compiled implementation. The HotSpot JVM is both an interpreter and a compiler. – Jörg W Mittag Mar 15 '10 at 08:56
  • 1
    Also, whether or not variables need to be declared is completely orthogonal to whether or not the language is statically typed and whether or not the implementation uses a compiler, an interpreter or both. Future versions of ECMAScript, for example, will require variables to be declared, yet ECMAScript is dynamically typed. – Jörg W Mittag Mar 15 '10 at 09:00
  • You probably are no longer after creating that Linux distro, but give [Ammonite](http://www.lihaoyi.com/Ammonite/) a try. It's Scala as a scripting language. – Amir Karimi May 16 '17 at 10:58

5 Answers5

6

Typically the statically typed languages are compiled languages. I guess the reason is, that statical analysis of types is rather expensive and you have to have an in depth look at all the code you're processing. After you've done that it feels like a waste to not write all that information into a file, so that you don't have to do it again next time. So you quickly end up with a compiled language.

On the other hand, to turn a compiled language in a "not-compiled" one is rather easy. You just don't store the results of the compilation anywhere but execute them directly. One compiler I know that provides such a wrapper is GHC, the standard Haskell compiler. You can add #!/usr/bin/runhaskell to your source files and then directly execute them. And since you're planning to be far off the norm, Haskell seems like a perfect fit ;). But expect some rather large startup time for your scripts, because all the "compile time" analysis and optimization isn't free.

Haskell isn't made for shell scripting and it's a functional language, so if you've never seen it before, it might take some time to get used to. But it has very little syntactical overhead and the strength of functional languages is abstraction, so I don't see why you couldn't create a library that makes shell scripting fun. There is even some experimental Haskell shell, but it does seem to be more a proof-of-concept than a real solution.

Generally I would say the overhead of all the type analysis is significant, but I would suggest you pick your favorite statically typed compiled language and look for a wrapper like runhaskell to execute scripts written in it.

sth
  • 222,467
  • 53
  • 283
  • 367
  • 7
    There is no such thing as a *compiled language*. Every language can be implemented by either a compiler or an interpreter. And, in fact, the vast majority of programming languages have at least one implementation of each. Also, most modern language implementations have actually *both* a compiler *and* an interpreter. Since you mention Haskell: Hugs is a Haskell interpreter. V8 is a JavaScript compiler. Microsoft's PowerShell is a compiler. A couple of years ago, the Bash developers were thinking about switching to a compiled implementation. The HotSpot JVM is both an interpreter and a compiler – Jörg W Mittag Mar 15 '10 at 08:55
2

quick google. F3, javaFX script, Linden Scripting Language (scripting for second life), Unlike the comment on the first answer F# can be used as a scripting language http://blogs.msdn.com/chrsmith/archive/2008/09/12/scripting-in-f.aspx

Felix, Tuga, CFGScript, Talc, Angelscript, and guessing there is more than that quick search.

Douglas

DouglasH
  • 1,216
  • 9
  • 12
2

Groovy. By default it's dynamic, duck-typed. But also supports static typing.

vartec
  • 131,205
  • 36
  • 218
  • 244
1

F# provides a combination of "type safety, succinctness, performance, expresivity and scripting".

MichaelGG
  • 9,976
  • 1
  • 39
  • 82
-1

Look in to the "typeset" command in your favorite shell. bash and ksh93 both can enforce integers and strings, use references (variable variables), etc. With ksh93, you can also do floating-point math and use objects with attributes. Static typing doesn't really buy you anything useful in init scripts and similar. You're primarily going to be reading files and running system commands - which is what the shell's really good at. Take some time with the O'Reilly "learning the Korn Shell" book before deciding that all those other Unixes are stupidly designed... ;)

dannysauer
  • 3,793
  • 1
  • 23
  • 30
  • and "set -o nounset" early on in the script... – dannysauer Feb 17 '09 at 19:49
  • 1
    This is a terrible suggestion. If the OP doesn't dynamic languages, he's going to *hate* shell scripting. They are much more dynamic than any other scripting language. – Paul Biggar Aug 09 '09 at 12:56
  • 1
    This answer provides a suggestion to enforce types within a language which the OP is already familiar with an which the init scripts are probably already written in. I guess I'd get more up votes if I had just said "this is impossible" or "here, use this language that three people have heard of," :) – dannysauer Jul 12 '15 at 21:15