Skip to content

New snippets #245

@WebFreak001

Description

@WebFreak001

I think it's time to update old snippets and add new useful ones. Everyone who wants to contribute, please comment with all the snippets you would want to use. They should be small and either be general purpose or do common boilerplate code in normal D code. You can also suggest snippets for dub.json, dub.sdl, vibe.d .dt files, dlangui .dml files, etc.

First of all these are the snippets I actively use a lot or think are useful: (I recommend you list what you like to use aswell)

  • main
  • refp
  • gets
  • op*
  • ex - but could be improved
  • inrange <--
  • fwrange <-- my 2 favorite ones

Here are some I start with: (<> denotes an editable cursor)

imain

int main(string[] args) {<>}

cmain - -betterC main

extern (C) void main() {<>}

cimain - -betterC main

extern(C) int main(int argc, const(char)** argv) {<>}

typedef - Creates a typesafe alias not allowing implicit casting from base type, but allows implicit conversion to the base type in most cases. Therefore the implicit casting works a lot like class/interface inheritance.

enum <1:MyType> : <2:BaseType> {<3:init = 0>}

proxy - Creates a typesafe alias not allowing implicit casting to the base type, but allows implicit conversion from the base type in most cases. Basically allows copying any base type with new properties and methods as new and separate type. Imports std.typecons : Proxy

struct <1:MyType> {
	<2:BaseType> base;
	mixin Proxy!base;
}

cominterface - Win32 COM interface without implementation to talk to other applications.

interface <1:MyType> : IUnknown {
extern(Windows):<2>
}

comobject - Win32 COM interface with implementation to serve to other applications.

class <1:MyType> : ComObject {
extern(Windows):<2>
}

Range improvements: make them only include methods.
Update inrange:

void popFront() {<4:>}
bool empty() @property const {<3:return true;>}
<1:auto> front() @property {<2:return myElement;>}

Update fwrange:

void popFront() {<5:>}
typeof(this) save() {<4:return this;>}
bool empty() @property const {<3:return true;>}
<1:auto> front() @property {<2:return myElement;>}

infrange:

enum bool empty = false;
void popFront() {<3:>}
<1:auto> front() @property {<2:return myElement;>}

outrange

void put(<2:MyElement> item) {<4:>}

birange

bool empty() @property const {<6:return true;>}
void popFront() {<4:>}
void popBack() {<5:>}
<1:auto> front() @property {<2:return myElement;>}
<1:auto> back() @property {<3:return myElement;>}

randrange

<1:auto> opIndex(<2:size_t> i) {<3:return myElement;>}
<2:size_t> length() @property const {<4:return 0;>}
<2:size_t> opDollar(size_t pos)() {<5:return length;>}

opslice

<1:size_t[2]> opSlice(size_t start, size_t end) {<2:return [start, end];>}
<3:auto> opIndex(size_t[2] slice) {<4:return base[slice[0] .. slice[1]];>}

Range snippets could maybe even be served by serve-d and automatically analyze which fields already exist and omit or update them accordingly.

diet snippets:
html

doctype html

html(lang="en")
	head
		meta(charset="utf-8")
		meta(name="viewport", content="width=device-width, initial-scale=1")
		title <1:>
	body
		<2:>

css

link(rel="stylesheet", href="<>")

js

script(src="<>")

style

:css
	<>

script

:javascript
	<>

markdown

:markdown
	<>

= diet should also support D snippets inside embedded D code. Another reason to complete them in serve-d =

I suggest renaming some of the existing snippets too:
ex should become exception
er should become error
tmpl should become template
fore should become foreach

Typing whole words are easier to remember and it looks more like keywords are auto-completed.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions