How do I find out what a type alias is at compile time #22465
Unanswered
pd-giz-dave
asked this question in
Questions and Answers
Replies: 2 comments 7 replies
-
What about using sumtypes?. Go to https://play.vlang.io/p/3a7cb8718f and click Run. type Float = f32 | f64
fn main() {
mut fs := []Float{}
fs << f32(3.14)
fs << f64(3.141592)
for f in fs {
match f {
f32 { println('${f} simple precision') }
f64 { println('${f} double precision') }
}
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
-
My objective is the main code does not know, or care, if its f32 or f64. So
I do not want to litter the code with f32(...) but rather just Float(...)
and its constant (either everything is f32 or everything is f64). I could
do it via an env var and use -d ... but I'd rather it was all self
contained in the source. Anyway, there are rare occasions when the compiler
needs to know, e.g do I use math.sqrt() or math.sqrtf(). This is a
performance critical app so I do not want to discriminate at run time and I
want to avoid casting. Its a platform specific (Arm/Android) thing so I
could do it via that.
It would also be nice to have examples of using all those compile time
variables, e.g. the documentation says "$alias matches type aliases" but
what does that mean?
I'd rather understand V than do a workaround.
Sorry, a bit of a rant - but I really like V and I've been programming
professionally for over 50 years in many languages, so me liking V really
means something! :-)
…On Wed, 9 Oct 2024 at 19:26, jorgeluismireles ***@***.***> wrote:
Much better:
import math
type Float = f32 | f64
fn main() {
mut fs := []Float{}
fs << f32(math.pi)
fs << f64(math.pi)
for f in fs {
match f {
f32 { println('${f} simple precision') }
f64 { println('${f} double precision') }
}
}
}
—
Reply to this email directly, view it on GitHub
<#22465 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEEESAHV37ZSSPS3HBONZMDZ2VYLPAVCNFSM6AAAAABPUTL4XOVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTAOBZGU4DKMI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a type alias:
pub type alias Float = f32
- this make it easy to change to f64 by a simple 1 line edit.But I want to be able to tell at compile time if its set to f32 or f64 in a generic math function.
There is a compile time variable
$alias
that looks useful but I have no idea how to use it.All my experiments result in either a compile time or build time error.
Any ideas on how to do this will be gratefully received.
Beta Was this translation helpful? Give feedback.
All reactions