Skip to content

'Namespaced' Classes as types / Support Consts with :: #52

@dmcblue

Description

@dmcblue

(This is not the 'correct' usage of the work namespace for Ruby, but hopefully you know what I mean)

If I have a namespaced class (by this I mean a class within a module) like:

class My::Class
	attr_accessor :name: String

	def initialize(name: String)
		@name = name
	end
end

class MyOtherClass
	attr_accessor :data: My::Class

	def initialize(data: My::Class)
		@data = data
	end
end

I get the following errors:

src/test.trb:12:1 - error TR1002: Unexpected token after parameter type for 'data' 'My::Class'

  12 |  def initialize(data: My::Class)
     | ~

src/test.trb:12:25 - error TR1003: Expected ')'

  12 |  def initialize(data: My::Class)
     |                         ~

[10:58:12 AM] Found 2 errors. Watching for file changes.

A handwritten version of this in plain ruby code is perfectly valid:

class My::Class
	attr_accessor :name: String

	def initialize(name: String)
		@name = name
	end
end

class MyOtherClass
	attr_accessor :data

	def initialize(data)
		@data = data
	end
end

I tried to work around this by using Type Aliases:

class My::Class
	attr_accessor :name: String

	def initialize(name: String)
		@name = name
	end
end

type MyClass = My::Class

class MyOtherClass
	attr_accessor :data: MyClass

	def initialize(data: MyClass)
		@data = data
	end
end

which doesn't result in errors but produces a bad RBS file:

class My
  @name: String

  def initialize: (name: String) -> void
end

class MyOtherClass
  @data: MyClass

  def initialize: (data: MyClass) -> void
end

Though the ruby file is mostly-ish correct (except for unstripped types, which I saw in another issue)

class My::Class
	attr_accessor :name: String

	def initialize(name)
		@name = name
	end
end

class MyOtherClass
	attr_accessor :data: MyClass

	def initialize(data)
		@data = data
	end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions