> In a discussion on D.learn, Steven Schveighoffer noted that it's only shared variables which make things complicated. > - a 'pure' member function doesn't really make sense (could only be called on an immutable class) > * has parameters that are all immutable or are implicitly convertible to immutable > Pure functions are functions that produce the same result for the same arguments. Reachable from a pointer or reference, then pure offers almost no useful guarantees. you canįreely mutate state on the heap, i.e. If you mean that the parameters don't have to be const either, i.e. This was changed a while ago to only requiring const.Ģ. The documentation that says that the parameters need to be convertible to immutable is outdated. > it easier to reason about code, would be dramatically extended.ġ. The more important benefit (IMHO) of pure, that it makes Relaxing that rule would allow much larger functions > I haven't found anything in TDPL which mentions the "only immutable > functions which have parameters which don't implicitly cast to immutable. > Actually, a clever compiler could even cache the results of pure > function bodies of every function called by that function. > globals) is viral, and requires inspection of the function body, and the > can check it just from the mangled function name). > trivial, and can be checked from the function declaration (in fact, you > This is possible because the first rule (all immutable parameters) is > implicitly convertible to immutable, then the compiler is permitted to > If a pure function has parameters that are all immutable or are > A pure function does not read or write any global mutable state. But 'cachable' is really only an implementation > The limitations exist because 'pure' is used to mean both 'no hidden > shared variables which make things complicated. > In a discussion on D.learn, Steven Schveighoffer noted that it's only > - a pure function cannot have 'out' parameters. > - a 'pure' member function doesn't really make sense (could only be > This is extremely restrictive, and not currently enforced. > * does not read or write any global mutable state > * has parameters that are all immutable or are implicitly > Pure functions are functions that produce the same result for the same The more important benefit (IMHO) of pure, that it makes it easier to reason about code, would be dramatically extended. Relaxing that rule would allow much larger functions to be cached. I haven't found anything in TDPL which mentions the "only immutable parameters" rule. The second rule (no globals) is viral, and requires inspection of the function body, and the function bodies of every function called by that function.Īctually, a clever compiler could even cache the results of pure functions which have parameters which don't implicitly cast to immutable. This is possible because the first rule (all immutable parameters) is trivial, and can be checked from the function declaration (in fact, you can check it just from the mangled function name). If a pure function has parameters that are all immutable or are implicitly convertible to immutable, then the compiler is permitted to cache the results. Only one requirement is necessary:Ī pure function does not read or write any global mutable state. But 'cachable' is really only an implementation detail.ĭrop the first requirement. The limitations exist because 'pure' is used to mean both 'no hidden state' AND 'cachable'. In a discussion on D.learn, Steven Schveighoffer noted that it's only shared variables which make things complicated. a pure function cannot have 'out' parameters. a 'pure' member function doesn't really make sense (could only be called on an immutable class) This is extremely restrictive, and not currently enforced. * does not read or write any global mutable state * has parameters that are all immutable or are implicitly convertible to immutable Pure functions are functions that produce the same result for the same arguments.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |