alternatives.dhover×99all
/// Alternative techniques for forcing named arguments — none of them work.
/// Each section demonstrates one approach and why it fails.
///
/// This file is for reference only; it is NOT expected to compile as-is
/// (the failing lines are left uncommented to show the errors).

module 
(module) alternatives

Alternative techniques for forcing named arguments — none of them work. Each section demonstrates one approach and why it fails.

This file is for reference only; it is NOT expected to compile as-is (the failing lines are left uncommented to show the errors).

alternatives
;
import
(package) std
std
.
(module) std.stdio
Category Symbols
File handles _popen File isFileHandle openNetwork stderr stdin stdout
Reading chunks lines readf readfln readln
Writing toFile write writef writefln writeln
Misc KeepTerminator LockType StdioException

Standard I/O functions that extend core.stdc.stdio. core.stdc.stdio is publically imported when importing std.stdio.

There are three layers of I/O:

  1. The lowest layer is the operating system layer. The two main schemes are Windows and Posix.

  2. C's stdio.h which unifies the two operating system schemes.

  3. std.stdio, this module, unifies the various stdio.h implementations into a high level package for D programs.

Source

std/stdio.d

@copyrightCopyright The D Language Foundation 2007-.@licenseBoost License 1.0.@authorsWalter Bright, Andrei Alexandrescu, Alex Rønne Petersen
stdio
:
(alias template) alternatives.writefln = std.stdio.writefln(alias fmt, A...)(A args) if (isSomeString!(typeof(fmt)))

Equivalent to writef(fmt, args, '\n').

writefln
;
// --------------------------------------------------------------------------- // 1. @disable this(int, int, …) — blocks BOTH positional AND named // --------------------------------------------------------------------------- struct
(struct) alternatives.Opts1
Opts1
{ int
(field) int alternatives.Opts1.x
x
,
(field) int alternatives.Opts1.y
y
,
(field) int alternatives.Opts1.width
width
,
(field) int alternatives.Opts1.height
height
;
@disable this(int, int, int, int); } void
void alternatives.test1()
test1
()
{ // auto a = Opts1(10, 20, 100, 200); // ❌ blocked // auto b = Opts1(x: 10, y: 20, width: 100, height: 200); // ❌ also blocked! } // --------------------------------------------------------------------------- // 2. Distinct wrapper types — type-safe, but doesn't force naming // --------------------------------------------------------------------------- struct
(struct) alternatives.X
X
{ int
(field) int alternatives.X.v
v
; }
struct
(struct) alternatives.Y
Y
{ int
(field) int alternatives.Y.v
v
; }
struct
(struct) alternatives.W
W
{ int
(field) int alternatives.W.v
v
; }
struct
(struct) alternatives.H
H
{ int
(field) int alternatives.H.v
v
; }
void
void alternatives.draw2(alternatives.X x, alternatives.Y y, alternatives.W w, alternatives.H h)
draw2
(
(struct) alternatives.X
X
(parameter) alternatives.X x
x
,
(struct) alternatives.Y
Y
(parameter) alternatives.Y y
y
,
(struct) alternatives.W
W
(parameter) alternatives.W w
w
,
(struct) alternatives.H
H
(parameter) alternatives.H h
h
)
{
void std.stdio.writefln!(char, int, int, int, int)(in char[] fmt, int __param_1, int __param_2, int __param_3, int __param_4) @safe

Equivalent to writef(fmt, args, '\n').

writefln
("draw2(%d, %d, %d, %d)",
(parameter) alternatives.X x
x
.
(field) int alternatives.X.v
v
,
(parameter) alternatives.Y y
y
.
(field) int alternatives.Y.v
v
,
(parameter) alternatives.W w
w
.
(field) int alternatives.W.v
v
,
(parameter) alternatives.H h
h
.
(field) int alternatives.H.v
v
);
} void
void alternatives.test2()
test2
()
{
void alternatives.draw2(alternatives.X x, alternatives.Y y, alternatives.W w, alternatives.H h)
draw2
(
(struct) alternatives.X
X
(1),
(struct) alternatives.Y
Y
(2),
(struct) alternatives.W
W
(3),
(struct) alternatives.H
H
(4)); // compiles — positional
void alternatives.draw2(alternatives.X x, alternatives.Y y, alternatives.W w, alternatives.H h)
draw2
(x:
(struct) alternatives.X
X
(1), y:
(struct) alternatives.Y
Y
(2), w:
(struct) alternatives.W
W
(3), h:
(struct) alternatives.H
H
(4)); // compiles — named
// Both work — naming is not enforced. } // --------------------------------------------------------------------------- // 3. All-default parameters — positional still works // --------------------------------------------------------------------------- void
void alternatives.draw3(int x = 0, int y = 0, int width = 0, int height = 0)
draw3
(int
(parameter) int x
x
= 0, int
(parameter) int y
y
= 0, int
(parameter) int width
width
= 0, int
(parameter) int height
height
= 0)
{
void std.stdio.writefln!(char, int, int, int, int)(in char[] fmt, int __param_1, int __param_2, int __param_3, int __param_4) @safe

Equivalent to writef(fmt, args, '\n').

writefln
("draw3(%d, %d, %d, %d)",
(parameter) int x
x
,
(parameter) int y
y
,
(parameter) int width
width
,
(parameter) int height
height
);
} void
void alternatives.test3()
test3
()
{
void alternatives.draw3(int x = 0, int y = 0, int width = 0, int height = 0)
draw3
(10, 20, 100, 200); // ✅ positional
void alternatives.draw3(int x = 0, int y = 0, int width = 0, int height = 0)
draw3
(x: 10, y: 20, width: 100, height: 200); // ✅ named
// Both work — naming is not enforced. } // --------------------------------------------------------------------------- // 4. Struct parameter wrapper — positional struct literal still works // --------------------------------------------------------------------------- struct
(struct) alternatives.DrawOpts
DrawOpts
{ int
(field) int alternatives.DrawOpts.x
x
,
(field) int alternatives.DrawOpts.y
y
,
(field) int alternatives.DrawOpts.width
width
,
(field) int alternatives.DrawOpts.height
height
;
} void
void alternatives.draw4(alternatives.DrawOpts o)
draw4
(
(struct) alternatives.DrawOpts
DrawOpts
(parameter) alternatives.DrawOpts o
o
)
{
void std.stdio.writefln!(char, int, int, int, int)(in char[] fmt, int __param_1, int __param_2, int __param_3, int __param_4) @safe

Equivalent to writef(fmt, args, '\n').

writefln
("draw4(%d, %d, %d, %d)",
(parameter) alternatives.DrawOpts o
o
.
(field) int alternatives.DrawOpts.x
x
,
(parameter) alternatives.DrawOpts o
o
.
(field) int alternatives.DrawOpts.y
y
,
(parameter) alternatives.DrawOpts o
o
.
(field) int alternatives.DrawOpts.width
width
,
(parameter) alternatives.DrawOpts o
o
.
(field) int alternatives.DrawOpts.height
height
);
} void
void alternatives.test4()
test4
()
{
void alternatives.draw4(alternatives.DrawOpts o)
draw4
(
(struct) alternatives.DrawOpts
DrawOpts
(10, 20, 100, 200)); // ✅ positional
void alternatives.draw4(alternatives.DrawOpts o)
draw4
(
(struct) alternatives.DrawOpts
DrawOpts
(x: 10, y: 20, width: 100, height: 200)); // ✅ named
// Both work — naming is not enforced. } // --------------------------------------------------------------------------- // 5. static opCall with @disable this() — opCall is unreachable // --------------------------------------------------------------------------- struct
(struct) alternatives.Opts5
Opts5
{ private int
(field) int alternatives.Opts5._x
_x
,
(field) int alternatives.Opts5._y
_y
;
@disable this(); static
(struct) alternatives.Opts5
Opts5
alternatives.Opts5 alternatives.Opts5.opCall(int x, int y)
opCall
(int
(parameter) int x
x
, int
(parameter) int y
y
)
{
(struct) alternatives.Opts5
Opts5
(local variable) alternatives.Opts5 o
o
= void;
(local variable) alternatives.Opts5 o
o
.
(field) int alternatives.Opts5._x
_x
=
(parameter) int x
x
;
(local variable) alternatives.Opts5 o
o
.
(field) int alternatives.Opts5._y
_y
=
(parameter) int y
y
;
return
(local variable) alternatives.Opts5 o
o
;
} } void
void alternatives.test5()
test5
()
{ // auto o = Opts5(x: 1, y: 2); // ❌ Error: constructor is not callable // auto p = Opts5(1, 2); // ❌ Error: expected 0 arguments, not 2 // @disable this() blocks everything — opCall is never reached. }