-1

I am looking for an adjective; one that describes the opposite of "mutating". I want to use it to mark functions, methods and method parameters (including the receiver) as "not being mutated" by the function, similar how the D language does it.

Ds approach works pretty well for parameters:

class X {
    void foo(const int x, immutable char[] y) {
        // x is readonly
        // y is readonly and can be trusted not to change
    }
}

However, both immutable and const seem ugly when using them on the function/method itself to denote that the function does not modify the receiver object:

// again D
class X {
    private int x;

    void foo() const {
        // x becomes readonly because the method is marked s such
    }

    void foo2() immutable {
        // same as foo(): x is readonly
    }
}

I am looking for a keyword with the same semantics as the above keywords in D; but as an adjective to be written before the function name:

class X {
    private x: int
    non-mutating foo() {
        // x is readonly
    }
}

Using immutable reads like the method would be immutable (such as final in Java or non-Virtual in C#/C++). I cannot use the keyword after the parenthesis closing the parameter list because thats where the return type is supposed to go:

fun x() -> Int

I'm not entirely sure whether this is the correct place to ask this question. But since i did not find a programming language design SE, i figuerd the people knowing many programming languages in this community could give me a few pointers :)

3
  • 1
    If you are making a new language, just make immutable the default...
    – Idan Arye
    Apr 30, 2017 at 21:58
  • Creating a new language is tempting, but also a whole lot of work... I might decide to turn them into Java annotations and write maven plugin to verify
    – marstato
    Apr 30, 2017 at 21:58
  • This is a valid question!
    – IceCold
    Jun 29, 2018 at 11:26

2 Answers 2

3

In OO speak, functions that are known to change data are called modifiers and those that are known to just return something without change are called selectors. I do not hear the terms very often, somehow they are not popular. More popular are getters and setters.

So you could use the qualifiers selector and modifier or getter and setter respectively.

Most people would associate getter and setter with properties. This could be a reason to choose the other, seemingly forgotten terms.

If you are only interested in the mutability of specific arguments, readonly may be appropriate.

2
  • Thank you very much! I'll see where selector fun x() takes me
    – marstato
    May 1, 2017 at 11:52
  • I think "getter and setter" came much later. The original Smalltalk terminology probably predates everything else. May 1, 2017 at 22:05
0

Your arguments about using immutable or const on method names is unconvincing. It's done in C++ and D. What exactly is your objection? Why do you feel it's ugly?

You could always put them in a different spot, such as void const funcName() or const void funcName(), but you run into problems with the return type being const. Like if you want to return a const object, it would probably be declared as const ObjectType funcName(), so that's no good.

Swift and Haskell use -> for return types, so maybe you could go with a syntax similar to that? You could do something like const funcName -> Int for a constant function that returns an Int. And then you could do const funcName :: argumentType -> Int for a constant function that takes an argument of argumentType and returns an int, or something along those lines.

1
  • Exactly that is what i am trying to do, sorry if that was not clear in the question. I want the result to be immutable fun x() -> ReturnType. The issue is that immutable function sounds like final function. Immmutable is not 100% correct, even, non-mutating is more on spot. But non-mutating cannot be a keyword for obvious reasons
    – marstato
    May 1, 2017 at 7:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.