Skip to content

alias declarations #7090

@wizzardx

Description

@wizzardx

More or less what you have in C, with the 'define' preprocessor directive, but in a type-safe manner.

This may be possible in eg a macro or template that could be added to stdlib.

Basically what D describes over here:
https://dlang.org/spec/declaration.html#alias

Meaning that eg, code like this (from Platformer tut):

proc handleInput(game: Game) =
  var event = defaultEvent
  while pollEvent(event):
    case event.kind
    of QuitEvent:
      game.inputs[Input.quit] = true
    of KeyDown:
      game.inputs[event.key.keysym.scancode.toInput] = true
    of KeyUp:
      game.inputs[event.key.keysym.scancode.toInput] = false
    else:
      discard

Can be expressed as something like this, for instance:

proc handleInput(game: Game) =
  alias inputs = game.inputs[event.key.keysym.scancode.toInput]
  var event = defaultEvent
  while pollEvent(event):
    case event.kind
    of QuitEvent:
      game.inputs[Input.quit] = true
    of KeyDown:
      inputs = true
    of KeyUp:
      inputs = false
    else:
      discard

It's possible currently to get a lot of these kinds of effects by making use of some combination of const or template, or inlined function, but it's a bit untidy or verbose.

Here's one basic example of it from the Nim forum:

https://forum.nim-lang.org/t/1515

It's also something that Ada lets you do with its "rename" syntax, eg:

http://www.adaic.org/resources/add_content/docs/95style/html/sec_5/5-7-2.html

https://en.wikibooks.org/wiki/Ada_Programming/Basic#"Hello,_world!"_with_renames

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions