Bug 24307 – [std.meta] weak values/alias for default values

Status
NEW
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2023-12-29T23:19:56Z
Last change time
2024-12-01T16:42:12Z
Assigned to
No Owner
Creator
crazymonkyyy
Moved to GitHub: phobos#9868 →

Comments

Comment #0 by crazymonkyyy — 2023-12-29T23:19:56Z
when writing naive template code with default values those default values are required to match the types of the generated function (id argue erroneously) ```d auto lerp(I,F)(I a,I b,F per=.5){} struct myfloat{} void main(){ lerp(1,10,myfloat()); } ``` > cannot implicitly convert expression `0.5` of type `double` to `myfloat` Its possible to solve this with 5 lines of template code and an uglier header, without the trade-offs of limiting the types accepted or mandating a full argument list, but it is tricky and writing the header to use the code would probably require an explanation my code for reference: https://gist.github.com/crazymonkyyy/b8793bd95afb86092944104531eecd56 Id suggest merging `weak` either as is or under the name "WeakAlias"/"WeakValue" if you must be verbose maybe consider if its possible to discard errors, but I didnt find a way to do so
Comment #1 by b2.temp — 2023-12-30T00:04:47Z
There are language tricks that can be used too, for example "uniform construction syntax" ```d auto lerp(F = double, I)(I a,I b,F per=F(.5)){} struct myfloat{double v;} void main(){ lerp(1,10,myfloat()); lerp(1,10); lerp!(myfloat)(1,10); } ``` But at some point the fact that D does no implicit construction will still be a limitation.
Comment #2 by dominikus — 2023-12-30T12:30:06Z
If something doesn't convert implicitly, do it explicitly: ```d auto lerp(I,F)(I a,I b,F per=F(.5)){} struct myfloat{} void main(){ lerp(1,10,myfloat()); } ``` Of course then F need to have a fitting constructor. Or an opCast: ```d auto lerp(I,F)(I a,I b,F per=cast(F).5){} struct myfloat{} void main(){ lerp(1,10,myfloat()); } ```
Comment #3 by crazymonkyyy — 2023-12-30T16:52:15Z
thats a simplified example, feel free to get get a more complex version working with only languge features but I'd want some helper code ```d float lerpammount=.5; auto lerp(I,F=typeof(lerpammount))(I a,I b,F per=weak!(F,lerpammount)){ import std; writeln(a," ",b," ",per); } auto remap(T,S=float)(T v,T min1=weak!(T,T.min),T max1=weak!(T,T.max),S min2=weak!S(0.0),S max2=weak!S(1.0)){ import std; writeln(v," ",min1," ",max1," ",min2," ",max2); } struct myfloat{} struct myint{ int i; enum min=myint(0); enum max=myint(100); } void main(){ lerp(1,10); lerp(1,10,myfloat()); lerpammount=.9; lerp(1,10); lerp(myint(),myint()); remap(ubyte(128)); remap(ubyte(167),ubyte(128)); remap(5,0,10); remap(5,0,10,.5); remap(myint()); remap(5,0,10,myint(100),myint(0)); } ``` which is simplier than my use case where I finally used `void drawtext(/*T,*/I,J=int,C=typeof(text),C2=typeof(highlight)) (string text_,I x,I y,J textsize=weak!J(16),C color=weak!(C,text),C2 color2=weak!(C2,highlight)){` where these color tooltips are very very ugly types
Comment #4 by robert.schadek — 2024-12-01T16:42:12Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/9868 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB