Tag Info

Hot answers tagged

24

I'll take a simple example: C++ vs Rust. Here is a function used to throw an exception in C++11: [[noreturn]] void ThrowException(char const* message, char const* file, int line, char const* function); And here is the equivalent in Rust: fn ...


19

Karl's answer is good. Here is an additional use that I don't think anyone else has mentioned. The type of if E then A else B should be a type that includes all the values in the type of A and all the values in the type of B. If the type of B is Nothing, then the type of the if expression can be the type of A. I'll often declare a routine def ...


17

There is one, very important distinction that I think that you're overlooking. The code you provided is for three things: the declaration of a variable, the instantiation of an object, and initializing that variable with that object. There is no interface implementation here. Customer needs to implement ICustomer (or do one or two other tricks) for that ...


16

Maybe it loops forever, or maybe it throws an exception. Sounds like a useful type to have in those situations, rare though they may be. Also, even though Nothing (Scala's name for the bottom type) can have no values, List[Nothing] does not have that restriction, which makes it useful as the type of an empty list. Most languages get around this by ...


13

Types form a monoid in two ways, together making a semiring. That's what's called algebraic data types. For finite types, this semiring directly relates to the semiring of natural numbers (including zero), which means you count how many possible values the type has (excluding “nonterminating values”). The bottom type (I'll call it Void) has ...


10

As long as your code looks as simple like this void ExampleFunc() { ICustomer oCustomer = new Customer(); oCustomer.Method1OfICustomer(); oCustomer.Method2OfICustomer(); // ... } there is no semantic difference - you can exchange "ICustomer" by "Customer", and the behaviour will stay identical. In this example, however, it could fulfill ...


9

In general, you use higher-rank polymorphism when you want the callee to be able to select the value of a type parameter, rather than the caller. For example: f :: (forall a. Show a => a -> Int) -> (Int, Int) f g = (g "one", g 2) Any function g that I pass to this f must be able to give me an Int from a value of some type, where the only thing g ...


6

Does the ML (SML/F#) implementations of tco in these languages differ substantially from the implementation in other languages, such as C++ and Haskell? Not as far as I know, no. The optimization of discarding the existing stack rather than saving off its current state (and possibly adjusting the execution pointer directly) on the last function call in ...


6

Higher rank polymorphism is extremely useful. In System F (the core language of typed FP languages you're familiar with), this is essential for admitting "typed Church encodings" which is actually how System F does programming. Without these, system F is completely useless. In System F, we define numbers as Nat = forall c. (c -> c) -> c -> c ...


5

"I'm incapable of imagining things like how the code to convert a string to a number or a number to a string would be written." Aw, WTF, I may as well just post it. So below is simple C code that converts binary to decimal and back again. I wrote it long ago for a project in which the target was an embedded processor and the development tools had a ...


5

The term "variable-length array" is actually specific to C, but it sounds like you're mostly interested in dynamic stack allocation. To review, VLAs are a feature added in C99 (then made optional in C11) which allow an array to be declared with a size not known until runtime. This means that the language must allocate space at runtime once it knows what the ...


5

There are few possible reasons for this sort of thing: As in JacquesB's answer it may simply be convenience for the library user to call a single method rather than two methods and keep their code more succinct. Performance may be a consideration. Calling .substring(3) will result in a new string being created, and therefore you are looping over the string ...


4

The simple answer is thats the order the binary instructions on most processors are laid out. Back when men were men and sheep were scared assembly programers spent a lot of time looking at raw binary code (and sometimes trying to infer what instruction was hanging from the on/off lights in the control panel). It was just easier if the assembly code ...


4

These are called convenience functions. They are included so users can write shorter and simpler code. Note that almost every library is "redundant" in the sense that users could write the same code themselves outside of the library. However the point of using libraries is that you save time and code, and you can reuses the knowledge of the library in ...


4

You might do well to take an assembly language class. It would clarify for you how data is actually represented in memory. Types are just an abstract construct to make working with data easier. You don't need them for programming. You just need to agree on a representation. For example, one of the simpler representations for a string is an array of ...


3

You are by far not the first person to have this need; many others have had it before you, so there has been quite a bit of research on the subject, and a solution in almost every decent language out there. For a theoretical discussion, you might want to look at Programming by Contract, preconditions, postconditions and invariants. It includes a list of ...


3

Ultimately, it depends on the specification of the particular language that you are using, but in general, pass-by-value-result (Wikipedia) means that the original value of the caller will not be modified until the function returns, so the only reasonable thing to expect is that the value should remain unchanged in the event of an exception, because when an ...


3

It is useful for static analysis to document the fact that a particular code path is not reachable. For example if you write the following in C#: int F(int arg) { if (arg != 0) return arg + 1; //some computation else Assert(false); //this throws but the compiler does not know that } void Assert(bool cond) { if (!cond) throw ...; } The compiler will ...


2

I do not know what the deal is with sigils, other than perhaps to scare the uninitiated and to annoy those wise enough to know that they could have very easily been missing. Hopefully, another answerer will have more insight on this. (As ratchet freak mentioned in the comments, it was probably done this way in order to simplify the parser, which perhaps ...


2

As a CS student, I faced the same problem before I went to university. I know how a for loop works, but how does that make an iPhone app? I never tried and never figured out. But after started university, and actually being forced to start writing stuff for assignments, things just get easier. Because when you actually start working on things, you can learn ...


2

My experience with large systems is that they stand or fall not by language choice, but by issues of design/architecture or test coverage. I'd rather have a talented Python team on my big enterprise project, than a mediocre Java one. Having said that, any language that let's you write significantly less code, has to be worth looking at (e.g. Python vs ...


2

How you create it is not better or worse in any way. Let's think about it: You either set objects or get objects (i.e. passing them into a constructor, method or returning them from a method). In either case as long as you are providing an interface for setting and getting you can new up your object in any way you want because either way you will be able ...


1

In C# there isn't much difference. The difference lies in what you can call on the reference you created. This code ICustomer customer = new Customer(); creates an interface reference named customer to the instance created by the new Customer(); call. This code Customer customer = new Customer(); creates an object reference name customer to the ...


1

Some languages (Including Delphi where I used it for this very purpose) have the concept of setting variables that can be checked at compile time so you can use an "IFDEF debug" directive to surround your scaffolding code and define / undefine debug appropriately. IIRC you would set the variables in a dialog at the program level. If your language is a ...


1

Yes this is a quite useful type; while its role would be mostly interior to the type system, there are some occasion where the bottom type would appear in openly. Consider a statically typed language in which conditionals are expressions (so the if-then-else construction doubles as the ternary operator of C and friends, and there might be a similar ...


1

In some languages, null has the bottom type, since the subtype of all types nicely defines what languages use null for (despite the mild contradiction of having null be both itself and a function that returns itself, avoiding the common arguments about why bot should be uninhabited). It can also be used as a catch-all in function types (any -> bot) to ...


1

First, many compiled implementations of programming languages have builtins or intrinsics, that is functions which are known to the compiler and which are compiled in a special way. For C or C++ compiled by GCC there are many builtin functions. Ocaml has external functions, etc.... Then, some implementations offer some way to use the underlying ...



Only top voted, non community-wiki answers of a minimum length are eligible