Crystal: Reason for weird annotation methodology preference over normal method annotations?

Created on 10 Jul 2016  Â·  12Comments  Â·  Source: crystal-lang/crystal

Hey, if this has been debated already on some other issue, please excuse me (I did try to search for it) and just refer me to the issue number.

I'm kinda confused as to why this works:

class B; end

class A
  @var : B

  def initialize
    @var = init_a
  end

  def init_a
    B.new
  end
end

a = A.new

but not the (IMHO) more intuitive:

class B; end

class A
  def initialize
    @var = init_a
  end

  def init_a : B
    B.new
  end
end

a = A.new

I don't see the value in adding a weird new kind of annotation, instead of relying on an existing mechanism in the language already in the form of method annotations. Kinda raises the barrier for Ruby crossover skills, doesn't it? :)

Will this change with the new compiler codebase? If not, I'd love to know the reason for it.

Thanks!

Most helpful comment

In Onyx commas are optional in multi line structural literals, also in LiveScript (where I stole the idea from), and it seems to work fine. If you have ideas for why it's bad, that could help me out also, removing it before I depend on it ;-)

All 12 comments

The main issue is that it needs type and method resolution. init_a could be defined there, on in the top-level namespace. The method could have arguments so we'd need to resolve their types, but their types might be in local variables, etc.

So yes, for these simple cases it _could_ be done, but in the general case it's harder, so we prefer to keep things consistent for now: it doesn't work in either case.

And no, this will probably never change.

Thanks for the explanation. I'll add this to the list of things I'd do differently like not requiring parenthesis for method arguments, and not needing commas at the end of multiline hash or array definitions :D

@sardaukar The commas are not needed, this compiles fine for me:

a = [
  1,
  2
]

h = {
  1 => 2,
  3 => 4
}

However, the formatter does add a trailing comma, because in my opinion it's so much easier to add elements and to rearrange them this way.

I meant back in Crystal 0.6 I think when you could do

a = [
  1
  2
]

@sardaukar That was a bug, it was never intended to be that way. Is there another language that allows that?

Don't think so. But can't really see why a newline can't replace a comma token for this case. Again, matter of taste I guess

@asterite CoffeeScript and MoonScript both allow newlines to replace commas. I swear there was another one, too, but I can't recall...

Newlines do look a lot cleaner. I hate looking at Rust or Java code that has a bunch of trailing commas.

public enum Direction {
    NORTH,
    EAST,
    SOUTH,
    WEST,
}

This looks ugly. The last comma is their to fit with the other ones which shouldn't be there in the first place.

public enum Direction {
    NORTH
    EAST
    SOUTH
    WEST
}

This looks clean compared. Every token has a reason for existing, and there aren't any unneeded tokens.

enum Opcode {
    Move,      // Copy a value between registers
    LoadK,     // Load a constant into a register
    LoadBool,  // Load a boolean into a register
    LoadNil,   // Load nil values into a range of registers
    GetUpVal,  // Read an upvalue into a register
    GetGlobal, // Read a global variable into a register
    GetTable,  // Read a table element into a register
    SetGlobal, // Write a register value into a global variable
    SetUpVal,  // Write a register value into an upvalue
    SetTable,  // Write a register value into a table element
    NewTable,  // Create a new table
}

Again, commas for the sake of it. If there's a newline, it should act as a comma in this context.

In Onyx commas are optional in multi line structural literals, also in LiveScript (where I stole the idea from), and it seems to work fine. If you have ideas for why it's bad, that could help me out also, removing it before I depend on it ;-)

In LISP comas are not used at all ;)
Oscar Campbell [email protected] schrieb am Di., 12. Juli 2016 um
2:04 AM:

In Onyx commas are optional in multi line structural literals, also in
LiveScript (where I stole the idea from), and it seems to work fine. If you
have ideas for why it's bad, that could help me out also, removing it
before I depend on it ;-)

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/crystal-lang/crystal/issues/2976#issuecomment-231901526,
or mute the thread
https://github.com/notifications/unsubscribe/AAlLFlq7K5mX6sPxqMk9OCJv4dntBIS3ks5qUtoRgaJpZM4JI3ux
.

@phaze enums are a bad example, there are no commas for them in Crystal:

enum Direction
  North
  East
  South
  West
end

Only literals need commas (Array, Tuple, Hash, NamedTuple), so there is only one representation for them, whatever if they're on a single line or on multiple ones.

Was this page helpful?
0 / 5 - 0 ratings