Skip to content

Introduce a real constant keyword and rename the current behaviour #992

Closed
@axic

Description

@axic

I think the following makes more sense compared to what we have now.

Now:

  • constant function should not modify the state (not fully enforced yet)
  • constant state variable (ie. the one in the class and not in a method) is evaluated every time it is called

After the change:

    1. the keyword view is introduced for functions (it replaces constant). Calling a view cannot alter the behaviour of future interactions with any contract. This means such functions cannot use SSTORE, cannot send or receive ether and can only call other view or pure functions.
    1. the keyword pure is introduced for functions, they are view functions with the additional restriction that their value only depends on the function arguments. This means they cannot use SSTORE, SLOAD, cannot send or receive ether, cannot use msg or block and can only call other pure functions.
    1. the keyword constant is invalid on functions
    1. the keyword constant on any variable means it cannot be modified (and could be placed into memory or bytecode by the optimiser)
  • 5) the keyword volatile is introduced for variables, which read their referenced value every time (this is what constant does today to variables) [agreed to remove this functionality]

Any example of the volatile keyword:

contract A {
    uint volatile number = block.number;
    function a() returns (uint) {
        return number; // this returns the *current* block.number every time it is called
    }
}

I'm not sure the volatile option is useful and thus we might decide to remove it.

Activity

changed the title [-]Rename the current constant keyword (and reserve it for the future)[/-] [+]Introduce a real constant keyword and rename the current behaviour[/+] on Sep 1, 2016
Arachnid

Arachnid commented on Sep 1, 2016

@Arachnid

the keyword stateless is introduced for functions (it replaces constant)

This seems like the wrong word; constant functions are definitely not stateless; they often depend on state. If a new name must be used, perhaps something like immutable or pure (in the sense that a pure function is side-effect-free, which is what we're really describing)?

axic

axic commented on Sep 1, 2016

@axic
MemberAuthor

I'm trying to find the word and stateless ia far from perfect. It should describe that the state is not modified.

Also additionally we could introduce real constant functions, which are indeed stateless and dont depend on the state. I.e. calculating something based on the inputs.

Arachnid

Arachnid commented on Sep 1, 2016

@Arachnid

Well, from a CS-theoretic POV, 'pure' is definitely the term you want. More informally, 'side effect free', but that's harder to condense into a keyword.

redsquirrel

redsquirrel commented on Sep 1, 2016

@redsquirrel
Contributor

I think 'immutable' will be the least ambiguous to most developers.

axic

axic commented on Sep 1, 2016

@axic
MemberAuthor

I like pure, but are we likely to find another use for it? Does view as keyword make sense?

axic

axic commented on Sep 1, 2016

@axic
MemberAuthor

@redsquirrel immutable wouldmean to me that child classes cannot ovveride it.

redsquirrel

redsquirrel commented on Sep 1, 2016

@redsquirrel
Contributor

@axic I suggest following Java and using 'final' for functions that cannot be overridden.

Arachnid

Arachnid commented on Sep 1, 2016

@Arachnid

I like pure, but are we likely to find another use for it?

I don't think so; certainly this is the only usage for the term I'm aware of.

I suggest following Java and using 'final' for functions that cannot be overridden.

I don't think that was under discussion here?

VoR0220

VoR0220 commented on Sep 1, 2016

@VoR0220
Member

I agree that we should use pure...perhaps strict? But I really like the idea of pure as a side effect free input. Rather than volatile which conjures up bad imagery, why not static?

chriseth

chriseth commented on Sep 1, 2016

@chriseth
Contributor

nonmutating?

VoR0220

VoR0220 commented on Sep 1, 2016

@VoR0220
Member

too long...overly blunt descriptive. Less is more imo.

ethernomad

ethernomad commented on Sep 1, 2016

@ethernomad
Contributor

readonly?

63 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @redsquirrel@Arachnid@axic@pirapira@ethernomad

        Issue actions

          Introduce a real constant keyword and rename the current behaviour · Issue #992 · ethereum/solidity