From cba6a6fdea354f7655fe1a2cae3a03b54e8ffc0a Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 30 Dec 2019 10:45:56 +0100 Subject: [PATCH] `as` casting --- examples/news_fetcher.v | 4 ++-- vlib/builtin/int_test.v | 4 ++++ vlib/compiler/expression.v | 12 ++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/examples/news_fetcher.v b/examples/news_fetcher.v index 73c226db54..4807a5dd7d 100644 --- a/examples/news_fetcher.v +++ b/examples/news_fetcher.v @@ -6,7 +6,7 @@ import json import sync const ( - NR_THREADS = 4 + nr_threads = 4 ) struct Story { @@ -73,7 +73,7 @@ fn main() { fetcher.mu = &mtx fetcher.wg = &wg fetcher.wg.add(ids.len) - for i := 0; i < NR_THREADS; i++ { + for i := 0; i < nr_threads; i++ { go fetcher.fetch() } fetcher.wg.wait() diff --git a/vlib/builtin/int_test.v b/vlib/builtin/int_test.v index d3fedf73d7..dd903d2f25 100644 --- a/vlib/builtin/int_test.v +++ b/vlib/builtin/int_test.v @@ -44,6 +44,10 @@ fn test_float_equal_operator() { assert a.gtbit(1) assert a >= 1 assert a.gebit(1) + + f := 1.2 + ab := f as int + assert ab == 1 } fn test_str_methods() { diff --git a/vlib/compiler/expression.v b/vlib/compiler/expression.v index 44ceb98511..ad23e42b55 100644 --- a/vlib/compiler/expression.v +++ b/vlib/compiler/expression.v @@ -60,6 +60,18 @@ fn (p mut Parser) bool_expression() string { p.gen('}, sizeof($typ) ), .typ = SumType_${tt} }')//${val}_type }') } } + // `as` cast + // TODO remove copypasta + if p.tok == .key_as { + p.next() + cast_typ := p.get_type() + if typ == cast_typ { + p.warn('casting `$typ` to `$cast_typ` is not needed') + } + p.cgen.set_placeholder(start_ph, '($cast_typ)(') + p.gen(')') + return cast_typ + } return typ }