Crystal: Suggestion: Add `pdef|defp` as shortcut for `private def`

Created on 3 May 2017  Â·  8Comments  Â·  Source: crystal-lang/crystal

I love that you declare methods as private on a per-method basis, but typing private is a bit long winded for every private method. It is also harder to read the private def as you your eye has to move back and forth more, but that's just a small point. I think adding a shortcut would be wonderful

# Or maybe `defp`
pdef do_something
end

# Rather than
private def do_something
end

I like pdef because it's still easy to do a find across the project for def #{method_name and find both private and public methods.

Thoughts?

Most helpful comment

Arguments relying on amount of typing required make little sense to me: readability is much more useful than writeability. Code is read many more times than it is written. Language design should optimize for readability first, then allow tooling such as editors (in this case tab-completion) to catch up and reduce effort when writing code.

All 8 comments

Arguments relying on amount of typing required make little sense to me: readability is much more useful than writeability. Code is read many more times than it is written. Language design should optimize for readability first, then allow tooling such as editors (in this case tab-completion) to catch up and reduce effort when writing code.

I think reading pdef is much nicer to read as well as write. All the privates take up a lot of space and my eye has to jump around more. I can't just read down a column as easily anymore. I really liked this in Elixir, but I see your point

I'm against this:

  • You can already search def my_function and get the public/protected/private method.
  • How do you differentiate between private def hello & protected def hello ?
  • I find that it just add obfuscation, and it allow easy mistake => just forget/mistype 1 letter and you change the meaning of what you're doing..
  • Finally it reduce readability: reading private def hello is easier than pdef hello...

If you can't type thoses chars, configure your editor to do so, you can have snippets that does exactly that: type pdef then TAB and you'll have a snippet with placeholder, etc... (ex: Ultisnips for vim)

@bew good point. I forgot about the ambiguity of protect v. private with this approach. I almost never use protected so it wasn't on my mind. I agree the editor can do it, but I also think pdef is easier to read, though now I agree it is ambiguous so that would likely be a big problem

Closing since the ambiguity with protected v. private makes this a non-starter

It might not be prudent to "advertise" here, however there are a bunch of aspects you've mentioned in different issues that indicates you _might_ be interested in https://github.com/ozra/onyx-lang _[ed: fixed address, ty @kirbyfan64]_, (it's _entirely_ based on Crystal; just a different syntax front-end (well, and some semantics for onyx nodes) - it compiles _both Crystal and Onyx_ files within the same proj (hence, crystal stdlib is also used, of course).

A quick sample (no highlighting on GitHub yet, unfortunately):

-- This is a comment

my-pub-fn(x, y, z) -> do-something x, y, z     -- public function
my-prot-fn*(x, y, z) -> do-something x, y, z   -- `-` is interchangeable w `_`
my-priv-fn**(x, y, z) -> do-something x, y, z

type Foo‹T›
   @some-var   I32 = 47  'get 'set   -- pragmas for making getter/setter
   @other-var  Bar  'g 's            -- abbr, getters/setters are so common
   @more-stuff T = T()

   Self.foo(x) -> say "A 'Class-method': you passed {x}"

   init(@some-var I32, @other-var = Bar "A Bar", 2) ->
      say "Constructed a { this.itype }"  -- instance-type of this
   end   -- `end` is voluntary

   do-stuff(x Int) -> Bar?
      return nil if x is 1   -- `is`,`and`,`or`, etc. act _exactly_ as `==`, `&&`...
      y = x + @some-var
      Bar "A new Bar", y

   my-protected-meth*() Bool -> @some-var is 47 and @more-stuff.zero?

end  -- again, `end` is voluntary - but is significant when used: indent must match

my-foo = Foo‹F64› 42, Bar "Booyaa", 3   -- call syntax for `new`. So, parentheses ok too.
q = my-foo.do-stuff 1
say q?do-bar-thing   -- `?` _instead_ of dot acts as `q.try &.do-bar-thing`. Chainable.
my-lambda = (x Size, y Size; let q) -> x * y * Size(q?some-var-val or 0)
my-lambda 1, 2  -- lambdas are called like funcs - no need for `.call`

There's more to it. Unfortunately it will take at least a couple of months more before I can actively dig in to it again though. But _it's here to stay_ and not go away.

Highlighting plugins are avail for Sublime, Vim, Atom and Howl (_lexer still beta_) text editors.

_Apologies, if this comment was inappropriate! If so feel free to delete!_

I think you meant: https://github.com/ozra/onyx-lang (yours is a 404...)

Thanks @kirbyfan64 - updated.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Papierkorb picture Papierkorb  Â·  3Comments

cjgajard picture cjgajard  Â·  3Comments

will picture will  Â·  3Comments

lgphp picture lgphp  Â·  3Comments

asterite picture asterite  Â·  3Comments