Hypothetically speaking…

Hypothetically speaking…

… if you were working on a language which was in many ways similar to (and targets) C but added more expressive types, type inference, and polymorphism, how would you interpret the following line of code?

x = "foo";

The options are:

char x[] = "foo";


char * x = "foo";


This entry was posted in Uncategorized. Bookmark the permalink.

5 Responses to Hypothetically speaking…

  1. bal says:

    One way to interpret this is “x has type immutable, null-terminated, array (count 4) of char(8)”.

    With that type, how about const char x[4] = “foo”; // x :: ImmutableString #4

    If the intent was to have x be mutable, I’d prefer a string buffer type which is initialized to be empty with the desired count and then a function is used to write “foo” into it. Something like this (with made up syntax):

    x StringBuf #m -> ()

  2. bal says:

    Looks like the last bit got garbled. Trying again:

    do x StringBuf #n -> ()

  3. bal says:
    x  StringBuf #m -> ()
  4. bal says:

    Forgot that html won’t like Haskell’s left arrow. So, my final try 🙂 Please delete the others (and this one if it doesn’t work).

    x = new :: StringBuf #10 — mutable char8 buffer of count 10 with x[0] initialized to ”

    strcpy “foo” x

  5. Scott Vokes says:

    The problem is that “””x = “foo”;””” isn’t expressive enough – in order to pick a particular interpretation, you need more info about how it’s used. Will the bytes in “foo” be mutated, but kept at a fixed length? Okay, a char array. Does the code append to it? Some sort of dynamic string buffer. Is it only going to be used as an identifier? Some sort of atom/symbol type would be better. That one line conflates all of these use cases, and more.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s